Send to Printer

st4u

ST 4U 378: Determining the Image File Format

May 3, 2013 3:27:52.716

Today's Smalltalk 4 You looks at figuring out which image reader to use on a given file you want to deal with. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Image Reading.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Last time we looked at reading images in from the file system into VA Smaltalk - and we noted that you need to line up the specific reader with the correct format. However, with a bit of glue code of your own, it's easy to determine which reader (if any) can read a given file, and select it. Try this on a JPG, for instance:


| file |
file := CfsFileDescriptor open: 'thuum.jpg' oflag: ORDONLY.
(CgPCXFileFormat formatMatchesFileHandle: file atOffset: 0)
    ifTrue: [Transcript cr; show: 'Format matches.']
    ifFalse: [Transcript cr; show: 'Format does not match.'].
file close.

You should see a negative in the Transcript. Now, try the same thing using the JPG reader:


| file |
file := CfsFileDescriptor open: 'thuum.jpg' oflag: ORDONLY.
(CgJPEGFileFormat formatMatchesFileHandle: file atOffset: 0)
    ifTrue: [Transcript cr; show: 'Format matches.']
    ifFalse: [Transcript cr; show: 'Format does not match.'].
file close.


And you'll see that it works. Given that, and a bit of glue code of your own, it would be simple to create a "find the right reader" class - and to then create readers for formats that are not already supported, and fit them into the existing framework.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: , ,

Enclosures:
[st4u378-iPhone.m4v ( Size: 1430136 )]

posted by James Robertson

Comments

Re: ST 4U 378: Determining the Image File Format

[Tom K] May 3, 2013 11:01:55.647

You might instead want to use CgFileFormat class>>#fileFormatClassFor:, and just let it tell you what class you want.

Re: ST 4U 378: Determining the Image File Format

[Tom K] May 3, 2013 11:10:49.329

Sorry, gave you a method that's part of TestMentor. You could go for #formatMatchingFile:ifNone: instead.

 Share Tweet This