Using the JPEG.DLL

by Alyce Watson [Alyce's Restaurant]

Home

Game Tutorial

Pseudo Menubar

Binary Numbers

Designing Games

API Corner

JPEG.DLL

Graphics Text

Tip Corner

Installers

Demos

Submission Guide

Newsletter help

Index


Images in Liberty BASIC

Liberty BASIC provides a command to load a bitmap into memory: LOADBMP. It provides a command to display the bitmap in a graphics window or graphicbox: DRAWBMP. For most purposes, the LOADBMP command suffices. There might be a problem if a program requires many large bitmap images. Bitmaps can be very large files, especially if they are in 24-bit format or 32-bit format. This makes for a large distribution packet, which can consume server space and take a long time to download.

JPGs are Smaller

The file size of a jpg image is much smaller than the same image in bitmap format. One such image is 500 pixels wide by 375 pixels high. As a bitmap, the file size is 550 KB. As a jpg, it is only 40 KB. That is a huge difference for a single file, and if several images are to be distributed with a program, the distribution packet size can become unacceptably large. Clearly, the use of jpgs would be desirable.

Bad Things About JPGs

Jpg is a "lossy" format. In order to compress the image into a small file size, algorithms are used to store the image information in fewer bytes. These algorithms cause the loss of some of the image information. The greater the compression, the smaller the file size, and the more loss of data. Jpgs can appear blurry, or they can have "cast off" pixels. For instance, a white area around a dark figure may be sprinkled with dark pixels near the figure because of the compression algorithm. Because of this loss of crispness, jpgs are not suitable for some applications. They are not a good choice for sprite images, which need well-defined borders. Jpgs are a good choice for game backgrouns, though.

The other bad thing about jpgs is that Liberty BASIC 3 has no native commands to handle jpg images.

The JPEG.DLL

The free jpeg.dll provides a really easy way to load jpg images in Liberty BASIC. The DLL is only 24 KB, so it adds little to the size of a distribution packet, while allowing images to be included as jpgs, making for much smaller packets.

Image Formats with the JPEG.DLL

The DLL will load jpg, bmp, gif, ico, wmf and emf formats. Benchmark tests show that loading a bmp with this DLL is nearly three times as fast as loading it with LOADBMP.

Functions in the DLL

The main function in the DLL is the LoadImageFile function. It requires a window handle and a disk filename for the image, and it returns the handle of the image in memory. Be sure to make this argument a "ulong"!

open "jpeg.dll" for DLL as #j
    calldll #j, "LoadImageFile",_
    hWnd as ulong,_         'window handle
    file$ as ptr,_          'disk filename of image
    ImageHandle as ulong    'handle of image in memory
close #j

The image can then be loaded with a Liberty BASIC LOADBMP command. Once it has been loaded by LB, it can be drawn with the DRAWBMP command. It should also be unloaded with the UNLOADBMP command. (See issue #100 for more on bitmaps in LB.)

'to load:
loadbmp "myPic", ImageHandle
'to unload:
unloadbmp "myPic"

The other two functions provided by the JPEG.DLL are ImageWidth and ImageHeight that require a handle to the loaded image and return the width or height of the image as measured in pixels.

open "jpeg.dll" for dll as #j
    'input handle of memory bmp
    'returns width of image
    calldll #j, "ImageWidth",_
    hImage as long,_		'handle of image in memory
    ImageWidth as ulong     'width of image

    'input handle of memory bmp
    'returns height of image
    calldll #j, "ImageHeight",_
    hImage as long,_        'handle of image in memory
    ImageHeight as ulong    'height of image
close #j

Sample Program

A complete sample program and the JPEG.DLL are included in the archive for this newsletter. Here is a quick demo that uses only the image loading function.

nomainwin
filedialog "Open","*.jpg",file$
if file$="" then end

open "jpeg.dll" for DLL as #j
    calldll #j, "LoadImageFile",_
    hWnd as ulong,_         'window handle
    file$ as ptr,_          'disk filename of image
    ImageHandle as ulong    'handle of image in memory
close #j

'to load:
loadbmp "myPic", ImageHandle

open "JPG" for graphics as #1
#1 "trapclose [quit]"
#1 "down; drawbmp myPic 0 0;flush"

wait

[quit]
'to unload:
unloadbmp "myPic"
close #1:end


Home

Game Tutorial

Pseudo Menubar

Binary Numbers

Designing Games

API Corner

JPEG.DLL

Graphics Text

Tip Corner

Installers

Demos

Submission Guide

Newsletter help

Index