home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Graphics / ToyViewer-2.6a / src / ToyWinSave.m < prev    next >
Encoding:
Text File  |  1997-01-24  |  4.3 KB  |  171 lines

  1. #import "ToyWin.h"
  2. #import <appkit/Application.h>
  3. #import <appkit/publicWraps.h>
  4. #import <appkit/SavePanel.h>
  5. #import <appkit/NXImage.h>
  6. #import <appkit/NXBitmapImageRep.h>
  7. #import <appkit/NXEPSImageRep.h>
  8. #import <appkit/Control.h>
  9. #import  <objc/NXBundle.h>    /* LocalizedString */
  10. #import <appkit/Panel.h>
  11. #import <appkit/tiff.h>
  12. #import <streams/streams.h> // NXStream
  13. #import <stdio.h>
  14. #import <stdlib.h>
  15. #import <string.h>
  16. #import <libc.h>
  17. #import <time.h>
  18. #import "ToyView.h"
  19. #import "TVController.h"
  20. #import "ColorMap.h"
  21. #import "common.h"
  22. #import "getpixel.h"
  23.  
  24. #define  BINFixedLength   120
  25.  
  26. @implementation ToyWin (Saving)
  27.  
  28. static void wrFixedLength(NXStream *stream,
  29.     const unsigned char *plane, int length,
  30.     const unsigned char *alp, int oneisblack)
  31. {
  32.     int    n, cc;
  33.     int    idx = 0;
  34.     static const char hex[] = "0123456789abcdef";
  35.  
  36.     for (n = 0; n < length; n++) {
  37.         cc = oneisblack ? ~plane[n] : plane[n];
  38.         if (alp)
  39.             cc |= ~alp[n];
  40.         cc &= 0xff;
  41.         NXPutc(stream, hex[cc >> 4]); 
  42.         NXPutc(stream, hex[cc & 0x0f]);
  43.         if (++idx >= BINFixedLength) {
  44.             idx = 0;
  45.             NXPutc(stream, '\n');
  46.         }
  47.     }
  48.     if (idx) NXPutc(stream, '\n');
  49. }
  50.  
  51. /* Local Method */
  52. /* ...... Don't call this method when (cinf->alpha && !cinf->isplanar) */
  53. - writeBitmapAsEPS:(unsigned char **)map
  54.         info:(commonInfo *)cinf to:(NXStream *)stream
  55. {
  56.     const char *p, *q;
  57.     int    cc, i, bwid, buflen;
  58.     time_t    tt;
  59.  
  60.     NXPrintf(stream, "%s\n%s",
  61.         "%!PS-Adobe-2.0 EPSF-2.0", "%%Title: ");
  62.     for (p = q = [self filename]; *p; p++)
  63.         if (*p == '/') q = p + 1;
  64.     for (p = q; *p; p++) {
  65.         if ((cc = *p & 0xff) <= ' ' || cc == '(' || cc == ')')
  66.             break;
  67.         NXPutc(stream, cc);
  68.     }
  69.     (void)time(&tt);
  70.     NXPrintf(stream, "\n%s\n%s%s",
  71.         "%%Creator: ToyViewer", "%%CreationDate: ", ctime(&tt));
  72.     NXPrintf(stream, "%s\n%s 0 0 %d %d\n%s\n\n",
  73.         "%%DocumentFonts: (atend)", "%%BoundingBox:",
  74.         cinf->width, cinf->height,
  75.         "%%EndComments");
  76.  
  77.     bwid = byte_length(cinf->bits, cinf->width);
  78.     buflen = bwid;
  79.     if (cinf->numcolors == 1)
  80.         NXPrintf(stream, "/pictstr %d string def\n", bwid);
  81.     else if (!cinf->isplanar) { /* mesh */
  82.         buflen = byte_length(cinf->bits, cinf->width * 3);
  83.         NXPrintf(stream, "/pictstr %d string def\n", buflen);
  84.     }else {
  85.         NXPrintf(stream, "/pictstr %d string def\n", bwid * 3);
  86.         for (i = 0; i < 3; i++)
  87.             NXPrintf(stream,
  88.             "/subStr%d pictstr %d %d getinterval def\n",
  89.             i, bwid * i, bwid);
  90.     }
  91.     NXPrintf(stream, "gsave\n0 0 translate\n%d %d %d [1 0 0 -1 0 %d]\n",
  92.         cinf->width, cinf->height, cinf->bits, cinf->height);
  93.     if (cinf->numcolors == 1 || !cinf->isplanar)
  94.         NXPrintf(stream, "{currentfile pictstr readhexstring pop}\n");
  95.     else {
  96.         for (i = 0; i < 3; i++)
  97.             NXPrintf(stream, 
  98.             "{currentfile subStr%d readhexstring pop}\n", i);
  99.     }
  100.     if (cinf->numcolors == 1) {
  101.         NXPrintf(stream, "image\n");
  102.         wrFixedLength(stream, map[0], bwid * cinf->height,
  103.             (cinf->alpha ? map[1]: NULL),
  104.             (cinf->cspace == NX_OneIsBlackColorSpace) );
  105.     }else if (cinf->isplanar) {
  106.         int    y, idx;
  107.         unsigned char *alp;
  108.         NXPrintf(stream, "true 3 colorimage\n");
  109.         for (y = 0; y < cinf->height; y++) {
  110.             idx = y * bwid;
  111.             alp = cinf->alpha ? &map[3][idx]: NULL;
  112.             for (i = 0; i < 3; i++)
  113.                 wrFixedLength(stream, &map[i][idx], bwid, alp, NO);
  114.         }
  115.     }else {
  116.         /* IGNORE (cinf->alpha && !cinf->isplanar) */
  117.         NXPrintf(stream, "false 3 colorimage\n");
  118.         wrFixedLength(stream, map[0], buflen * cinf->height, NULL, NO);
  119.     }
  120.     NXPrintf(stream, "grestore\n%s\n", "%%Trailer");
  121.     return self;
  122. }
  123.  
  124. - (NXStream *)openEPSStream
  125. {
  126.     NXStream *stream;
  127.     id    tv;
  128.     commonInfo *cinf;
  129.  
  130.     if ((stream = NXOpenMemory(NULL, 0, NX_READWRITE)) == NULL)
  131.         return NULL;
  132.     tv = [self toyView];
  133.     cinf = [tv commonInfo];
  134.     if (cinf->type == Type_eps || cinf->cspace == NX_CMYKColorSpace
  135.         || (cinf->alpha && !cinf->isplanar)) {
  136.         /* This code may be no use */
  137.         NXRect    rect;
  138.         [tv getFrame:&rect];
  139.         [tv copyPSCodeInside:&rect to:stream];
  140.     }else {
  141.         unsigned char *map[MAXPLANE];
  142.         [self getBitmap: map info: &cinf];
  143.         [self writeBitmapAsEPS: map info: cinf to: stream];
  144.         [self freeTempBitmap];
  145.     }
  146.     NXFlush(stream);
  147.     NXSeek(stream, 0L, NX_FROMSTART);
  148.     return stream;
  149. }
  150.  
  151. - (int)getBitmap:(unsigned char **)map info:(commonInfo **)infp
  152. {
  153.     NXImageRep *rep;
  154.     rep = [[[self toyView] image] bestRepresentation];
  155.     [(NXBitmapImageRep *)rep getDataPlanes: map];
  156.     return 0;
  157. }
  158.  
  159. - freeTempBitmap
  160. {
  161.     return self;
  162. }
  163.  
  164. - print: sender
  165. {
  166.     [[self toyView] printPSCode:sender];
  167.     return self;
  168. }
  169.  
  170. @end
  171.