home *** CD-ROM | disk | FTP | other *** search
-
-
- #include <Types.h>
- #include <Files.h>
- #include <Quickdraw.h>
- #include <Packages.h>
- #include <Memory.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <OSUtils.h>
- #include <ToolUtils.h>
- #include <Menus.h>
- #include <Dialogs.h>
- #include <stdio.h>
- #include <Errors.h>
- #include <string.h>
-
- #ifndef THINK_C
- #include <Strings.h>
- #endif
-
-
- #include <Image Compression.h>
- #include <CompressionDialog.h>
-
-
-
- /************************************************
- *
- * Set up application environment.
- *
- ************************************************/
-
-
-
- Initialize()
-
- {
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- InitDialogs(nil);
- InitCursor();
-
- }
-
- /************************************************
- *
- * Make a new name for the compressed file ( illustrative only, this is not the recommended method ).
- *
- ************************************************/
-
- GetNewName(char *newName,char *oldName)
-
- {
-
- int cnum = 1;
- char bname[32];
- int olen = oldName[0];
- int c;
-
- strcpy(newName+1,"Compy of ");
- newName[0] = strlen(newName+1);
- if ( olen > 31 )
- olen = 31;
- BlockMove(oldName+1,bname,olen);
- if ( ( (c=strncmp(oldName+1,newName+1,newName[0])) == 0 ) || (sscanf(oldName+1,"Compy %d of %31c",&cnum,bname) == 2) ) {
- if ( c == 0 ) {
- olen -= newName[0];
- BlockMove(bname+newName[0],bname,olen);
- }
- cnum++;
- sprintf(newName+1,"Compy %d of ",cnum);
- newName[0] = strlen(&newName[1]+1);
- }
- if ( olen > (31-newName[0]) )
- olen = 31-newName[0];
- BlockMove(bname,newName+1+newName[0],olen);
- newName[0] += olen;
- }
-
-
-
- pascal OSErr
- Progress(short progressMsg,Fixed progressPercent,long refcon)
-
- {
-
- OSErr result = noErr;
- short kind;
- Handle h;
- Rect r;
- GrafPtr savePort;
- static DialogPtr progressDialog = 0;
-
-
- switch (progressMsg) {
- case codecProgressOpen:
-
- progressDialog = 0;
- if ( (progressDialog = GetNewDialog(5002, 0, (WindowPtr)-1)) != nil ) {
- ShowWindow((WindowPtr)progressDialog);
- SetPort((GrafPtr)progressDialog);
- DrawDialog(progressDialog);
- GetDItem(progressDialog, 1, &kind, &h, &r);
- InsetRect(&r, -1, -1);
- FrameRect(&r);
- InsetRect(&r, 1, 1);
- }
- break;
-
- case codecProgressUpdatePercent:
-
- GetPort(&savePort);
- if ( progressDialog ) {
- SetPort((GrafPtr)progressDialog);
- GetDItem(progressDialog, 1, &kind, &h, &r);
- r.right = r.left + FixRound(FixMul(progressPercent, FixRatio(r.right-r.left, 1)));
- FillRect(&r, qd.gray);
- }
-
- /***** it would be cool to look for Cmd Period here ******/
-
- SetPort(savePort);
- break;
-
- case codecProgressClose:
-
- if ( progressDialog )
- DisposDialog(progressDialog);
- progressDialog = 0;
- break;
- }
- return(result);
- }
-
- CompressPictures()
-
- {
-
- /* for original picture */
-
- short originalFile;
- PicHandle originalPicture = nil;
- Rect pictureFrame;
- OpenCPicParams header;
-
- /* for compressed picture */
-
- short compressedFile;
- PicHandle compressedPicture = nil;
-
-
- /* for sepecifying compression */
-
- short cDepth = 0;
- CodecType cMethod = 0;
- CodecQ cQuality = codecNormalQuality;
-
-
- /* user interface stuff */
-
- char newName[32];
- CWindowPtr window;
- Rect windRect;
- Cursor **watch;
-
- /* StdFile stuff */
-
- Point pt = {100,100};
- SFReply sfr;
- SFTypeList types = { 'PICT',0 };
-
- /* generic variables */
-
- OSErr result;
- ProgressProcRecord *progP,progressRec;
-
-
- progressRec.progressProc = Progress;
- progressRec.progressRefCon = 0;
- progP = &progressRec;
-
-
- watch = GetCursor(watchCursor);
-
-
-
- /************************************************
- *
- * Ask for a pict file to compress
- *
- ************************************************/
-
- SFGetFile(pt,(ConstStr255Param)"",nil,1,types,nil,&sfr);
- if ( !sfr.good ) {
- result = -1;
- goto done;
- }
-
- /************************************************
- *
- * Open the file.
- *
- ************************************************/
-
- SetCursor(*watch);
- if ((result=FSOpen(sfr.fName,sfr.vRefNum,&originalFile)) != noErr ) {
- goto done;
- }
-
-
- /************************************************
- *
- * Get the picture frame, to see how big of a window to make.
- *
- ************************************************/
-
- if ( (result=GetPictureFileHeader(originalFile,&pictureFrame,&header)) != noErr ) {
- goto done;
- }
-
-
- /************************************************
- *
- * Create a window the size of the picture, and set our port to it.
- *
- ************************************************/
-
- windRect = pictureFrame;
- OffsetRect(&windRect,40,40);
- if ( (window = (CWindowPtr)NewCWindow(nil,&windRect,sfr.fName,true,0,
- (WindowPtr)-1,false,0)) == nil ) {
- result = -1;
- goto done;
- }
- SetPort((GrafPtr)window);
-
- /************************************************
- *
- * Draw the picture in the window, slid up to the top left corner.
- *
- ************************************************/
-
- OffsetRect(&pictureFrame,-pictureFrame.left,-pictureFrame.top);
- if ( (result=DrawPictureFile(originalFile,&pictureFrame,progP)) != noErr ) {
- goto done;
- }
-
-
- /************************************************
- *
- * Ask how the user wants to compress it.
- *
- ************************************************/
-
- SetCursor(&qd.arrow);
- if ( (result=CompressionDialog(window->portPixMap,&window->portRect,&cQuality,
- &cMethod,nil,&cDepth,nil)) != noErr ) {
- goto done;
- }
-
-
- /************************************************
- *
- * Ask her for the name of the new file.
- *
- ************************************************/
-
-
- GetNewName(newName,(char *)sfr.fName);
- SFPutFile(pt,(ConstStr255Param)"",(ConstStr255Param)newName,NULL,&sfr);
- if ( !sfr.good ) {
- result = -1;
- goto done;
- }
- BlockMove(sfr.fName,newName,32);
- SetCursor(*watch);
-
- /************************************************
- *
- * Create the new file, if we can.
- *
- ************************************************/
-
- FSDelete(sfr.fName,sfr.vRefNum);
- if ( (result=Create(sfr.fName,sfr.vRefNum,'ppxi','PICT')) != noErr ) {
- FSClose(originalFile);
- goto done;
- }
- if ( (result=FSOpen(sfr.fName,sfr.vRefNum,&compressedFile)) != noErr ) {
- FSClose(originalFile);
- goto done;
- }
-
- /************************************************
- *
- * Now the hard part: Compress the PICT and put it into the new file.
- *
- ************************************************/
-
-
- if ( (result=FCompressPictureFile(originalFile,compressedFile,cDepth,nil,cQuality,
- false,false,progP,cMethod,anyCodec)) != noErr ) {
- FSClose(originalFile);
- FSDelete(sfr.fName,sfr.vRefNum);
- FlushVol(nil,sfr.vRefNum);
- goto done;
- }
-
- whew:
-
- /************************************************
- *
- * Change the window name, and draw the compressed PICT in it.
- *
- ************************************************/
-
- EraseRect(&pictureFrame);
- SetWTitle((WindowPtr)window,(ConstStr255Param)newName);
- if ( compressedPicture )
- DrawPicture(compressedPicture,&pictureFrame);
- else
- DrawPictureFile(compressedFile,&pictureFrame,progP);
- SetCursor(&qd.arrow);
-
-
- /************************************************
- *
- * Close the files and wait for click to exit.
- *
- ************************************************/
-
- FSClose(compressedFile);
- FSClose(originalFile);
- if ( originalPicture )
- KillPicture(originalPicture);
- if ( compressedPicture )
- KillPicture(compressedPicture);
- FlushVol(nil,sfr.vRefNum);
- while (!Button() )
- ;
- CloseWindow((WindowPtr)window);
- done:
- return(result);
- }
-
-
-
-
-
- /************************************************
- *
- * Our program.
- *
- ************************************************/
-
-
- main()
-
-
- {
-
- /************************************************
- *
- * Initialize everything, and then do it. Notice
- * the wonderful HIG conformant user interface.
- *
- ************************************************/
-
-
- Initialize();
-
- #ifdef LINKED_CODEC_DEBUG
-
- InstallDebugCodecMgr();
-
- #endif
-
- while ( 1 ) {
-
-
- if ( CompressPictures() )
- break;
- }
-
- #ifdef LINKED_CODEC_DEBUG
-
- DeinstallDebugCodecMgr();
- #endif
- }
-