home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / Kludges.subproj / GIFWriting.m < prev    next >
Encoding:
Text File  |  1995-02-15  |  3.3 KB  |  97 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //    FILENAME:    GIFWriting.m
  3. //    SUMMARY:    Implementation of a GIFWriting category to NXImage
  4. //    SUPERCLASS:    Object:NXImage(GIFWriting)
  5. //    INTERFACE:    None
  6. //    AUTHOR:        Rohit Khare
  7. //    COPYRIGHT:    (c) 1994 California Institure of Technology, eText Project
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //  IMPLEMENTATION COMMENTS
  10. //        This code uses the netPBM distribution. Currently, it creates a temp
  11. //    tiff (uncompressed) in /tmp and then opens a pipe to a string of netPBM
  12. //    filters assumed to be located in [NXApp mainBundle], then copies those
  13. //    bytes to the target stream.
  14. //        In the future, we should rework the code to directly access the netPBM
  15. //    and libTIFF libraries. Transparency support would also be mondo cool.
  16. ///////////////////////////////////////////////////////////////////////////////
  17. //    HISTORY
  18. //    10/30/94:    Extended to use OmniImageFilter as first resort (for HP-PA).
  19. //    07/16/94:    Created. Derived from netPBM.
  20. ///////////////////////////////////////////////////////////////////////////////
  21.  
  22. #import "eTextKernel.h"
  23. #import "GIFWriting.h"
  24. #import "_FormatWritingImageView.h"
  25. #import <machkit/NXData.h>
  26.  
  27. @implementation NXImage (GIFWriting)
  28. - writeGIFtoPath_Poskanzer:(const char *)destPath
  29. {
  30.     char    tempPath[MAXPATHLEN];
  31.     char    cmd[4*MAXPATHLEN];
  32.     const char    *appPath;
  33.     id    iv;
  34.  
  35.     iv = [_FormatWritingImageView new];
  36.     [iv useImage:self];
  37.     sprintf(tempPath, "/tmp/eTextGifConversion.%x.tiff", [NXApp uniqueID]);
  38.     [iv dumpTIFF:tempPath];
  39.     
  40.     // no fs flag; I don't think ppmquant improves colors with it.
  41.     // Ideally, we'd run ppmquant iff we really needed to. but for simplicity
  42.     // we always call it. Does this cause aesthetic errors?
  43.     NXLogError("GIFWriting: generating %s", destPath);
  44.     appPath = [[NXBundle mainBundle] directory];
  45.     sprintf(cmd,"\"%s/tifftopnm\" \"%s\" | \"%s/ppmquant\" 256 | \"%s/ppmtogif\" > \"%s\"",
  46.             appPath, tempPath, appPath, appPath, destPath);
  47.     system(cmd);
  48.     unlink(tempPath);
  49.     return self;
  50. }
  51. - writeGIFtoPath:(const char *)destPath
  52. {
  53.     NXAtom *OIFtypes;
  54.     static NXAtom gifType = NULL;
  55.     if (!gifType) gifType = NXUniqueString("image format gif");
  56.     
  57.     // Can OIF handle this?
  58.     OIFtypes = [Pasteboard typesFilterableTo:gifType];
  59.     
  60.     if (OIFtypes && *OIFtypes) {
  61.         id conversionPB,data;
  62.         NXStream *t;
  63.         char *tiffbytes; int len, maxlen;
  64.         
  65.         // Add TIFF data to a PBoard
  66.         t = NXOpenMemory(NULL,0,NX_READWRITE);
  67.         [self writeTIFF:t];
  68.         NXGetMemoryBuffer(t, &tiffbytes, &len, &maxlen);
  69.         data = [[NXData alloc] initWithData:(void *)tiffbytes size:maxlen dealloc:YES];
  70.         conversionPB = [Pasteboard newByFilteringData:data ofType:NXTIFFPboardType];
  71.         
  72.         if ([conversionPB findAvailableTypeFrom:&gifType num:1]) {
  73.             NXStream *s = [conversionPB readTypeToStream:gifType];
  74.             if (s) {
  75.                 NXSaveToFile(s, destPath);
  76.                 NXCloseMemory(s, NX_FREEBUFFER);
  77.             } else {
  78.                 // OK, try the Poskanzer pipeline
  79.                 [self writeGIFtoPath_Poskanzer:destPath];
  80.             }
  81.         } else {
  82.             // OK, try the Poskanzer pipeline
  83.             [self writeGIFtoPath_Poskanzer:destPath];
  84.         }
  85.  
  86.         // Attempt to free the data from VM.
  87.         [conversionPB free];
  88.         [data free];
  89.         NXCloseMemory(t, NX_FREEBUFFER);
  90.         free(OIFtypes);
  91.     } else {
  92.         // OK, try the Poskanzer pipeline
  93.         [self writeGIFtoPath_Poskanzer:destPath];
  94.     }
  95.     return self;
  96. }
  97. @end