On January 18, 2005 6:01 pm, ye vicky wrote:
> Hi, Matthew, thanks for your kindly help!
> I have downloaded your JPEGView source code. I think I need some time
> to understand it as I am very new to explore Palm programming.
For me, learning by example works best. I explain my code below...
> Just went through your code roughly, tring to find the part of the
> code where you used the library. But strangly, I can't find much
> places you use any structures or functions derived from the library.
> Almost all the functions in your code are from Palm APIs.
> To be frankly, your code is not very long, do I miss anything?
drawJPEG is the function you should look at. The rest is simply user
interface and user interaction code. In JPEGView, I use an enhanced gadget
to simplify drawing and resizing of the display.
The functions directly used in decoding the JPEG are:
MainForm.c:14 myPtrFree() Armlet callback to free memory.
MainForm.c:19 myFileReader() Armlet callback to read the next data chunk.
MainForm.c:32 errorMessager() Armlet callback to handle errors.
MainForm.c:36 drawJPEG() The function that calls the ARMlet.
The three callback functions must have the same prototype in all applications
which use this armlet. The file reader callback loads the next chunk from
VFS and displays a pretty progress bar at the same time. Stripped-down
versions of the callbacks (and global variables) would be:
FileRef FileHandle;
void myPtrFree(void *p) { if ( p ) MemPtrFree(p); }
// You can use VFS, the DB stream system, or in-memory JPEG data.
void myFileReader(void *bufP, UInt32 numBytes, UInt32 *numBytesReadP) {
VFSFileRead(FileHandle, numBytes, bufP, numBytesReadP);
}
void errorMessager(char *messageP) { SysFatalAlert(messageP); }
The drawJPEG function is the interface between 68k and the armlet. It sets up
pointers to the above functions, the requested width and height, and actually
calls the armlet (or .dll if running under the Simulator).
A simpler (and more robust) version of this function would be:
Err drawJPEG(Char *fileNameP, RectangleType *drawAreaP) {
typedef struct _MyUserData {
void *imageDataP;
void *myPtrFree;
void *unusedP;
void *myFileReader;
void *errorMessager;
UInt16 width;
UInt16 height;
UInt16 unused[3];
} MyUserData;
MyUserData myUserData;
UInt32 imageDataSize;
Err error;
// Open and retrieve size information about the selected file.
error = VFSFileOpen(Preferences.Volume, fileNameP, vfsModeRead,
&FileHandle);
if ( error ) return error;
// Calculate the amount of memory required to store the raw bitmap.
imageDataSize = drawAreaP->extent.x * drawAreaP->extent.y * sizeof(UInt16);
// Allocate and clear enough memory for the raw bitmap.
// I have not tested the following line. The final argument might not work.
error = FtrPtrNew('CUIj', 0, imageDataSize,
(void **)&myUserData.imageDataP);
if ( error ) return error;
DmSet(myUserData.imageDataP, 0, imageDataSize, 0x00);
// Strangely, 0x00 defaults to black, 0xFF defaults to white.
// But not for the whole image... the ARMlet has its own default.
// Set up pointers to the callback functions.
myUserData.myPtrFree = &myPtrFree;
myUserData.myFileReader = &myFileReader;
myUserData.errorMessager = &errorMessager;
myUserData.width = drawAreaP->extent.x;
myUserData.height = drawAreaP->extent.y;
{ // Call the ARMlet with the appropriate data.
UInt32 processor;
FtrGet(sysFtrCreator, sysFtrNumProcessorID, &processor);
if ( sysFtrNumProcessorIsARM(processor) ) {
MemHandle armletH = DmGetResource('ARMC', 1);
NativeFuncType *armletP = (NativeFuncType *)MemHandleLock(armletH);
PceNativeCall(armletP, myUserData);
MemHandleUnlock(armletH);
DmReleaseResource(armletH);
}
}
VFSFileClose(FileHandle);
{ // Create a usable V3 bitmap from the raw image data and draw to screen.
BitmapType bmpP = BmpCreate(drawAreaP->extent.x, drawAreaP->extent.y, 16,
NULL, &error);
BitmapTypeV3 bmp3 = BmpCreateBitmapV3(bmpP, kDensityDouble,
myUserData.imageDataP, NULL);
if ( bmp3 ) {
WinDrawBitmap((BitmapType *)bmp3, drawAreaP->topLeft.x,
drawAreaP->topLeft.y);
BmpDelete((BitmapType *)bmp3);
BmpDelete(bmpP);
}
}
// Free the substantial amount of memory allocated to the raw bitmap.
FtrPtrFree( appCreator, 0);
return errNone;
}
I hope the above code is a little clearer. I'll test it tonight or tomorrow
and make a 2.0 version of JPEGView. ;) *grabs another glass of Coke-a-Cola*
Remember to replace 'CUIj' with your application's creator ID. This code was
long overdue for a clean-up! (Now to re-write it in Pascal...)
> The only part of the code I found have somthing from the library is as
> below,
> ---------------------------------------------------------------------------
>----------------------- result = PceNativeCall(
> (NativeFuncType*)"ARMlet.dll\0NativeFunction", // default dll path
> is the location of PalmSim.exe
> myUserData);
> ---------------------------------------------------------------------------
>-------------------------- A quite strange usage, my first time to see.
Because of the length of the line and the comment, I split the statement
across multiple lines. My apologies. The comment referrs to the fact that
if no path is given, the PalmOS Simulator will look for, in this example,
ARMlet.dll in the same directory as the PalmSim.exe file. This makes little
difference as there is no ARMlet.dll file to go with this ARMlet.
> Also, I am very curious about this function: FrmVFSOpen() in the
> following code, as it looks so like a standard API of form or VFS
> operation from Palm. But I cannot find it from Palm APIs. Also cannot
> find definition from your code and JEPG lib. Did you get it from
> anywhere else?
I use the following functions throughout my code. Unfortunately, they are
part of an in-house development library of common code and not available for
public release. Commercial licensing is available. ;)
- FrmVFSOpen() provides a pop-up file selection dialog.
- ErrDisplayError() pops up an error dialog with the actual error name.
- FrmDebugAlert() is similar to ErrDisplayError() but for non-errors.
- VFSConstructPath() builds a full path from parts.
- Collapse...() are functions to handle tall/wide displays.
- A large number of other functions in files other than MainForm.c...
> > The code for JPEGView is available from:
> > http://www.marginsoftware.com/attachments/JPEGView-1.2.zip
--
Matthew Bevan, Margin Software
- Re-inventing the wheel, every time.
Greener's Law:
Never argue with a man who buys ink by the barrel.
--
For information on using the Palm Developer Forums, or to unsubscribe, please
see http://www.palmos.com/dev/support/forums/