Send to Printer


ST 4U 375: Reading Image Files in VA Smalltalk

April 26, 2013 13:41:31.171

Today's Smalltalk 4 You looks at reading inage files (such as JPG) into VA Smalltalk. 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:


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:

It's pretty easy to read in image (graphic) files in VA Smalltalk (so long as they are in a supported format). Have a look at class CgImageFileFormat and its subclasses - each concrete subclass reads in a particular kind of file. For instance - to read a JPG, you can do one of two things:

file := CfsFileDescriptor open: 'thuum.jpg' oflag: ORDONLY.
image := format loadFromFileHandle: file atOffset: 0.
file close.

| format image |
format := CgJPEGFileFormat new.
image := format loadFromFile: 'thuum.jpg'.

Notice how we don't need to deal with opening/closing the file in the second example? That's because the framework handles those details. You don't need to worry about catching exceptions for unhanded formats or non-existent files, either - say you try this:

| format image |
format := CgJPEGFileFormat new.
image := format loadFromFile: 'someimage.jpg'.

| format image |
format := CgJPEGFileFormat new.
image := format loadFromFile: 'someimage.png'.

In the first case, the file does not exist - in the second, the format (PNG) is not supported. Either way, the VA code hands you back nil. So, whenever you read an image file, you'll want to check against nil. Finally, what do you get back?

You get an instance of CgDeviceIndependentImage.

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: , ,

[st4u375-iPhone.m4v ( Size: 1129163 )]

posted by James Robertson

 Share Tweet This