Reading file properties without loading file
Reading file properties without loading file
Reading file properties without loading file
Reading file properties without loading file
Reading file properties without loading file
Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file
Reading file properties without loading file Reading file properties without loading file
Reading file properties without loading file
Go Back  Xtreme Visual Basic Talk > > > Reading file properties without loading file


Reply
 
Thread Tools Display Modes
  #1  
Old 09-19-2010, 07:52 PM
CodeHater CodeHater is offline
Newcomer
 
Join Date: Sep 2010
Posts: 6
Default Reading file properties without loading file


Hello Everyone,

Noob here.

I want to be able to able to read the dimensions of a TIF image without loading the entire file.

Any thoughts?

CH

PS Using Visual Basic 2008 on Vista64.
Reply With Quote
  #2  
Old 09-19-2010, 08:15 PM
TRSDOSBasic79's Avatar
TRSDOSBasic79 TRSDOSBasic79 is offline
Contributor
 
Join Date: Oct 2003
Location: Pennsylvania
Posts: 422
Default

Reply With Quote
  #3  
Old 09-20-2010, 01:41 AM
CodeHater CodeHater is offline
Newcomer
 
Join Date: Sep 2010
Posts: 6
Default

Thanks for the prompt reply.

The first link is to a VB6 application which is too complicated for me to follow. I did search the rtf description for mentions of image width etc, but couldn't see what I wanted.

The second link is dead, but thanks anyway.
Reply With Quote
  #4  
Old 09-20-2010, 05:35 AM
TRSDOSBasic79's Avatar
TRSDOSBasic79 TRSDOSBasic79 is offline
Contributor
 
Join Date: Oct 2003
Location: Pennsylvania
Posts: 422
Default

Weird. It did work.

try it here ...

http://www.planet-source-code.com/vb...46469&lngWId=1
Reply With Quote
  #5  
Old 09-20-2010, 07:07 AM
OnErr0r's Avatar
OnErr0rReading file properties without loading file OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

I doubt you'll save much by attempting to read just the header(s) of a TIFF image. This is already wrapped nicely in GDI+, so you might consider using it. Btw, TIFFs have the capability to contain more than one image, so you might have to read multiple headers to handle them. This adds even more complexity to what could be a simple problem.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #6  
Old 09-20-2010, 08:16 AM
CodeHater CodeHater is offline
Newcomer
 
Join Date: Sep 2010
Posts: 6
Default

Quote:
Originally Posted by TRSDOSBasic79 View Post
Weird. It did work.

try it here ...

http://www.planet-source-code.com/vb...46469&lngWId=1
Thanks for taking the time and trouble to correct the link (which now works) but the link is to another VB6 application.

I know VB6 shares much in common with VB.NET but I don't have the experience of VB6 or VB.NET to know what's different and what's the same. If we are talking about a small code snippet then I can *sometimes* work out why things don't work, but in a complete application I get lost easily.
Reply With Quote
  #7  
Old 09-20-2010, 08:22 AM
CodeHater CodeHater is offline
Newcomer
 
Join Date: Sep 2010
Posts: 6
Default

Quote:
Originally Posted by OnErr0r View Post
I doubt you'll save much by attempting to read just the header(s) of a TIFF image. This is already wrapped nicely in GDI+, so you might consider using it. Btw, TIFFs have the capability to contain more than one image, so you might have to read multiple headers to handle them. This adds even more complexity to what could be a simple problem.
Thanks for those thoughts. First, let me check an assumption: it is possible to determine the dimensions of an image without reading the entire file, right?

If that's true then I imagine I will save a great deal of time because I am dealing with extremely large images e.g. 200,000 pixels wide/2GB file size (with LZW compression).

I had a quick look at GDI+ but it seems to me you have to load your image before you can access the .width and .height properties. Am I missing something?

Finally, the tifs are generated by me and they don't have multiple layers so I won't have to consider that case.

CH
Reply With Quote
  #8  
Old 09-20-2010, 01:26 PM
OnErr0r's Avatar
OnErr0rReading file properties without loading file OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Wow, that is a large image. In that case I can see why you'd want to just read the header to get the info.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #9  
Old 09-20-2010, 02:58 PM
AtmaWeapon's Avatar
AtmaWeaponReading file properties without loading file AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

It doesn't look like it'd be super-difficult, but I have no images with which to make an attack. I base my assessment on this document, which led to the TIFF specification. A quick skim revealed that most of the known image formats have size information in their headers.

If you're relatively familiar with binary I/O it shouldn't be too tough to hack it out.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #10  
Old 09-21-2010, 06:15 AM
CodeHater CodeHater is offline
Newcomer
 
Join Date: Sep 2010
Posts: 6
Default

Thanks AtmaWeapon. Now that you mention it, I have already seen a similar approach that didn't quite work for my files - it read the width but not the height. The offsets must have been wrong. I will have a hack and post a solution if I get one.

CH
Reply With Quote
  #11  
Old 09-24-2010, 03:42 PM
CodeHater CodeHater is offline
Newcomer
 
Join Date: Sep 2010
Posts: 6
Default

Code:
Imports System.IO
Public Class Form1
	' Code to read the dimensions of a tif image file without loading the entire image file.
	' Based on page 13 of http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
	'
	' The code is not well-written or well-tested but it is good enough for me.

	Private Function short_at(ByRef buff As Byte(), ByVal ptr As Integer) As Integer
		' Ignores possibility of (intentionally) negative values.
		Return buff(ptr) + 256 * buff(ptr + 1)
	End Function

	Private Function long_at(ByRef buff As Byte(), ByVal ptr As Integer) As Integer
		' Ignores possibility of negative values/overflow.
		Return buff(ptr) + 256 * (buff(ptr + 1) + 256 * (buff(ptr + 2) + 256 * buff(ptr + 3)))
	End Function

	Private Function tif_dimensions(ByVal filename As String) As Integer()
		' Open the file and create a reader.
		Dim tif_str As FileStream = New FileStream(filename, FileMode.Open)
		Dim br As New BinaryReader(tif_str)

		' Create a buffer to hold four bytes
		Dim buff() As Byte = {0, 0, 0, 0}

		' Start reading the file header
		tif_str.Seek(4, SeekOrigin.Begin)

		' Get offset to block of IFDs
		tif_str.Read(buff, 0, 4)
		Dim off As Integer = long_at(buff, 0)
		tif_str.Seek(off, SeekOrigin.Begin)

		' Get number of IFDs then loop through them
		tif_str.Read(buff, 0, 2)
		Dim ifd_count As Integer = buff(0) + 265 * buff(1)

		Dim width As Integer = Nothing
		Dim height As Integer = Nothing

		' Loop thru IFDS looking for width and height
		For i As Integer = 1 To ifd_count
			tif_str.Read(buff, 0, 4)
			Dim tag As Integer = short_at(buff, 0)
			Dim typ As Integer = short_at(buff, 2)
			tif_str.Read(buff, 0, 4)
			tif_str.Read(buff, 0, 4)
			Dim value As Integer = long_at(buff, 0)
			If tag = 256 Then
				width = value
			ElseIf tag = 257 Then
				height = value
			End If
			If width <> Nothing And height <> Nothing Then
				Exit For
			End If
		Next

		tif_str.Close()

		Dim ans As Integer() = {width, height}
		Return ans
	End Function
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim z As Integer() = tif_dimensions("C:\Documents and Settings\661\My Documents\My Pictures\WASPS\III.tif")
		ListBox1.Items.Add(CStr(z(0)) + " " + CStr(z(1)))
	End Sub
End Class
Reply With Quote
  #12  
Old 09-24-2010, 04:07 PM
OnErr0r's Avatar
OnErr0rReading file properties without loading file OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

I'd recommend using Option Strict On and Option Explicit On too.

short_at will fail if the high byte has the high bit set. Unless overflows are being ignored.

long_at will too and will also return incorrect information for bytes 2 and 3. Those need to be shifted into the proper positions. Since VB.NET now has shifting operators and unsigned variables, I'd suggest using them instead of multiplication by powers of two. Unless you're still using VS 2002.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Reading file properties without loading file
Reading file properties without loading file
Reading file properties without loading file Reading file properties without loading file
Reading file properties without loading file
Reading file properties without loading file
Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file Reading file properties without loading file
Reading file properties without loading file
Reading file properties without loading file
 
Reading file properties without loading file
Reading file properties without loading file
 
-->