' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.IO
Imports System.Runtime.InteropServices

Namespace E
    Public Class TiffDim
        Public Structure HWDim
            Public H As Integer
            Public W As Integer
        End Structure
        Public Const TIF_ImageWidth As Integer = 256
        Public Const TIF_ImageHeigth As Integer = 257
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure TIFHEAD
            Public Identifier As Short
            Public Version As Short
            Public IFDOffset As Integer
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure TIFIFD_FIXTOP
            Public NumDirEntries As Short
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure TIFTAG
            Public TagId As Short
            Public DataType As Short
            Public DataCount As Integer
            Public DataOffset As Integer
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure TIFIFD_FIXBOT
            Public NextIDFOffset As Integer
        End Structure
        Public Shared Function ReadStruct(Of T)(stm As Stream) As T
            Dim siz As Integer = Marshal.SizeOf(GetType(T))
            Dim b As Byte() = New Byte(siz - 1) {}
            stm.Read(b, 0, siz)
            Dim buf As IntPtr = Marshal.AllocHGlobal(siz)
            Marshal.Copy(b, 0, buf, siz)
            Dim res As T = DirectCast(Marshal.PtrToStructure(buf, GetType(T)), T)
            Marshal.FreeHGlobal(buf)
            Return res
        End Function
        Public Shared Function ReadStruct(Of T)(stm As Stream, ix As Integer) As T
            stm.Seek(ix, SeekOrigin.Begin)
            Return ReadStruct(Of T)(stm)
        End Function
        Public Shared Function Find(fnm As String) As HWDim
            Dim res As New HWDim()
            Using stm As Stream = New FileStream(fnm, FileMode.Open, FileAccess.Read)
                Dim head As TIFHEAD = ReadStruct(Of TIFHEAD)(stm, 0)
                Dim ix As Integer = head.IFDOffset
                While True
                    Dim top As TIFIFD_FIXTOP = ReadStruct(Of TIFIFD_FIXTOP)(stm, ix)
                    For i As Integer = 0 To top.NumDirEntries - 1
                        Dim tag As TIFTAG = ReadStruct(Of TIFTAG)(stm)
                        If tag.TagId = TIF_ImageHeigth Then
                           res.H = tag.DataOffset
                        ElseIf tag.TagId = TIF_ImageWidth Then
                           res.W = tag.DataOffset
                        End If
                    Next
                    Dim bot As TIFIFD_FIXBOT = ReadStruct(Of TIFIFD_FIXBOT)(stm)
                    ix = bot.NextIDFOffset
                    If ix = 0 Then
                        Exit While
                    End If
                End While
            End Using
            Return res
        End Function
        Public Shared Sub Main(args As String())
            Dim hw As HWDim = TiffDim.Find("C:\test.tif")
            Console.WriteLine(hw.H & " " & hw.W)
        End Sub
    End Class
End Namespace
