home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / ToyWinEPS.m < prev    next >
Encoding:
Text File  |  1995-11-12  |  6.1 KB  |  251 lines

  1. #import "ToyWinEPS.h"
  2. #import <appkit/TextField.h>
  3. #import <appkit/NXImage.h>
  4. #import <appkit/NXEPSImageRep.h>
  5. #import <appkit/NXBitmapImageRep.h>
  6. #import <libc.h>
  7. #import <string.h>
  8. #import "ToyView.h"
  9. #import "common.h"
  10.  
  11. @implementation ToyWinEPS
  12.  
  13. /* Overload */
  14. - makeComment:(commonInfo *)cinf
  15. {
  16.     sprintf(cinf->memo, "%d x %d  EPS", cinf->width, cinf->height);
  17.     return self;
  18. }
  19.  
  20. /*
  21.     EPS⁄ùTIFF⁄¸˚±·„⁄„⁄º⁄¿⁄Æ¡¢image⁄ù⁄⁄⁄ˆ⁄¿⁄ústream⁄¸‰æ⁄›‰—⁄•⁄˘”˘¯ä˘²⁄ë„⁄ë
  22.     ⁄„⁄º¡£®ò¨³⁄¸˝±⁄⁄⁄Ø⁄ò⁄˘⁄⁄⁄º Sharon Zakhour ⁄˛ "TIFFandEPS" ⁄˛˚ÿ¸¡¡¢´¤⁄ˋ¡¢
  23.     [epsimg lockFocus];
  24.     tiffrep = [[NXBitmapImageRep alloc] initData: NULL fromRect: &rect];
  25.     [epsimg unlockFocus];
  26.     [tiffrep getDataPlanes: map];
  27.     ⁄˙⁄ˇ¡¢mesh¥˙¡…¥¿⁄˛RGBˆ˝⁄˛‚ï⁄¸˝¬˚‹⁄˛ˆ˝⁄‹£–⁄˜²à⁄⁄⁄˘⁄•⁄ê⁄ƒ¡£⁄‡⁄ò⁄ˇ¥²¥›¥ï
  28.     ¥Æ¥ú¥¨⁄¸⁄í¦›‰¼⁄¦⁄ò⁄˘⁄“⁄Ø⁄”¡¢¥½¥ˆ¥¨¥ê¥ˆ¥â¥˙¡…¥¿⁄ù®•⁄ƒ¬ï⁄˙³æ†ö⁄˙⁄¢⁄º⁄¿⁄Æ
  29.     ”˛˝±⁄•⁄˚⁄«⁄ˆ⁄¿¡£
  30. */
  31. static NXBitmapImageRep *tiffrep = nil;
  32. static commonInfo common_info;
  33.  
  34. /* Local */
  35. - (commonInfo *)EPStoBitmap: (unsigned char **)map
  36. {
  37.     commonInfo *ip;
  38.     NXStream *stream;
  39.     NXSize cSize;
  40.  
  41.     [[[self toyView] image] getSize: &cSize];
  42.     if ((cSize.width >= MAXWidth)
  43.     || (stream = NXOpenMemory(NULL, 0, NX_READWRITE)) == NULL)
  44.         return NULL;
  45.     ip = &common_info;
  46.     [[[self toyView] image] writeTIFF:stream];
  47.         NXFlush(stream);   
  48.     NXSeek(stream, 0L, NX_FROMSTART);
  49.     tiffrep = [[NXBitmapImageRep alloc] initFromStream: stream];
  50.     NXClose(stream);
  51.  
  52.     ip->width    = [tiffrep pixelsWide];
  53.     ip->height    = [tiffrep pixelsHigh];
  54.     ip->xbytes    = [tiffrep bytesPerRow];
  55.     ip->type    = Type_tiff;
  56.     ip->bits    = [tiffrep bitsPerSample];
  57.     ip->numcolors    = [tiffrep numColors]; /* without alpha */
  58.     ip->alpha    = [tiffrep hasAlpha];
  59.     ip->isplanar    = [tiffrep isPlanar];
  60.     ip->cspace    = [tiffrep colorSpace];
  61.     ip->palette    = NULL;
  62.     ip->palsteps    = 0;
  63.     ip->memo[0]    = 0;
  64.     [tiffrep getDataPlanes: map];
  65.     return ip;
  66. }
  67.  
  68. /* Overload */
  69. - freeTempBitmap
  70. {
  71.     if (tiffrep != nil) {
  72.         [tiffrep free];
  73.         tiffrep = nil;
  74.     }
  75.     return self;
  76. }
  77.  
  78. /* Overload */
  79. - (int)getBitmap:(unsigned char **)map info:(commonInfo **)infp
  80. {
  81.     *infp = [self EPStoBitmap: map];
  82.     return (*infp) ? 0 : Err_MEMORY;
  83. }
  84.  
  85. /* Overload */
  86. - saveAsTiff: sender
  87. {
  88.     NXStream *stream;
  89.     int    fd = -1;
  90.     char    *sav;
  91.     int    type;
  92.     float    factor;
  93.  
  94.     sav = [super getSaveTiffname: imageFilename jpeg: NO
  95.         compress: &type by: &factor];
  96.     if (sav == NULL) /* canceled */
  97.         return self;
  98.     if ((fd = open(sav, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0
  99.     || (stream = NXOpenFile(fd, NX_WRITEONLY)) == NULL) {
  100.         if (fd >= 0) (void) close(fd);
  101.         errAlert(sav, Err_SAVE);
  102.         return self;
  103.     }
  104.     /* type = none/lzw */
  105.     [[[self toyView] image] writeTIFF:stream
  106.         allRepresentations:NO usingCompression:type andFactor:0.0];
  107.     NXClose(stream);
  108.     (void)close(fd);
  109.     return self;
  110. }
  111.  
  112. /* Overload */
  113. - saveAsEPS: sender
  114. {
  115.     NXStream *stream;
  116.     NXEPSImageRep *rep;
  117.     char    *sav;
  118.     char    *eps;
  119.     int i, n;
  120.  
  121.     sav = [self getSavename: imageFilename with:Type_eps];
  122.     if (sav == NULL) /* canceled */
  123.         return self;
  124.     stream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
  125.     if (stream == NULL) {
  126.         errAlert(sav, Err_MEMORY);
  127.         return self;
  128.     }
  129.     rep = (NXEPSImageRep *)[[[self toyView] image] bestRepresentation];
  130.     [rep getEPS:&eps length:&n];
  131.     for (i = 0; i < n; i++)
  132.         NXPutc(stream, *eps++);
  133.     NXFlush(stream);
  134.     if (NXSaveToFile(stream, sav))
  135.         errAlert(sav, Err_SAVE);
  136.     NXCloseMemory(stream, NX_FREEBUFFER);
  137.     return self;
  138. }
  139.  
  140.  
  141. /* New */
  142. - (NXStream *)rotateEPS: (int)angle name: (const char *)fname error: (int *)err
  143. {
  144.     NXStream *stream;
  145.     NXEPSImageRep *rep;
  146.     NXRect    rect;
  147.     char    *eps;
  148.     int    i, n, dx, dy, lx, ly;
  149.  
  150.     *err = 0;
  151.     rep = (NXEPSImageRep *)[[[self toyView] image] bestRepresentation];
  152.     [rep getEPS:&eps length:&n];
  153.     [rep getBoundingBox:&rect];
  154.     stream = NXOpenMemory(NULL, 0, NX_READWRITE);
  155.     if (stream == NULL) {
  156.         *err = Err_MEMORY;
  157.         return NULL;
  158.     }
  159.     dx = -(rect.origin.x + rect.size.width);
  160.     dy = -(rect.origin.y + rect.size.height);
  161.     lx = rect.size.width;
  162.     ly = rect.size.height;
  163.     if (angle == 45) {
  164.         dx = (rect.size.height / 2 + 0.5) - rect.origin.x;
  165.         dy = - (rect.size.height / 2 + 0.5) - rect.origin.y;
  166.         lx = (rect.size.height + rect.size.width) * 0.7071 + 0.5;
  167.         ly = lx;
  168.     }else if (angle == 90) {
  169.         dx = -rect.origin.x;
  170.         lx = rect.size.height;
  171.         ly = rect.size.width;
  172.     }else if (angle == 270) {
  173.         dy = -rect.origin.y;
  174.         lx = rect.size.height;
  175.         ly = rect.size.width;
  176.     }else if (angle == Horizontal) {
  177.         dy = -rect.origin.y;
  178.     }else if (angle == Vertical) {
  179.         dx = -rect.origin.x;
  180.     } /* else 180 */
  181.  
  182.     NXPrintf(stream, "%s\n%s: %s\n",
  183.         "%!PS-Adobe-2.0 EPSF-2.0", "%%Title", fname);
  184.     NXPrintf(stream, "%s: 0 0 %d %d\n%s\n\ngsave\n",
  185.         "%%BoundingBox", lx, ly, "%%EndComments");
  186.     if (angle > 0)
  187.         NXPrintf(stream, "%d rotate\n", angle);
  188.     else
  189.         NXPrintf(stream, "%s scale\n",
  190.             (angle == Horizontal)?"-1 1":"1 -1");
  191.     NXPrintf(stream, "%d %d translate\n%s\n", dx, dy, "%%BeginDocument: ");
  192.     for (i = 0; i < n; i++)
  193.         NXPutc(stream, *eps++);
  194.     NXPrintf(stream, "\n%s\ngrestore\n", "%%EndDocument");
  195.     NXFlush(stream);
  196.     NXSeek(stream, 0L, NX_FROMSTART);
  197.     // NXCloseMemory(stream, NX_FREEBUFFER);
  198.     return stream;
  199. }
  200.  
  201. /* New */
  202. - (NXStream *)clipEPS: (NXRect *)select error: (int *)err
  203. {
  204.     NXStream *stream;
  205.     NXEPSImageRep *rep;
  206.     NXRect    rect;
  207.     char    *eps, buf[512];
  208.     int    i, n, bp;
  209.     int    loc[4];
  210.  
  211.     *err = 0;
  212.     rep = (NXEPSImageRep *)[[[self toyView] image] bestRepresentation];
  213.     [rep getEPS:&eps length:&n];
  214.     [rep getBoundingBox:&rect];
  215.     loc[0] = rect.origin.x + select->origin.x;
  216.     loc[1] = rect.origin.y + select->origin.y;
  217.     loc[2] = loc[0] + (int)select->size.width;
  218.     loc[3] = loc[1] + (int)select->size.height;
  219.     stream = NXOpenMemory(NULL, 0, NX_READWRITE);
  220.     if (stream == NULL) {
  221.         *err = Err_MEMORY;
  222.         return NULL;
  223.     }
  224.     for (i = 0, bp = 0; ; i++) {
  225.         if (i >= n) {
  226.             NXCloseMemory(stream, NX_FREEBUFFER);
  227.             *err = Err_ILLG;
  228.             return NULL;
  229.         }
  230.         if ((buf[bp++] = *eps++) >= ' ')
  231.             continue;
  232.         if (strncmp(buf, "%%BoundingBox", 13) == 0) {
  233.             NXPrintf(stream, "%s: %d %d %d %d\n", "%%BoundingBox",
  234.                 loc[0], loc[1], loc[2], loc[3]);
  235.             for (++i ; i < n; i++)
  236.                 NXPutc(stream, *eps++);
  237.             break;
  238.         }else {
  239.             buf[bp] = 0;
  240.             NXPrintf(stream, "%s", buf);
  241.         }
  242.         bp = 0;
  243.     }
  244.     NXFlush(stream);
  245.     NXSeek(stream, 0L, NX_FROMSTART);
  246.     // NXCloseMemory(stream, NX_FREEBUFFER);
  247.     return stream;
  248. }
  249.  
  250. @end
  251.