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

  1. #import "ToyView.h"
  2. #import <dpsclient/wraps.h>
  3. #import <appkit/publicWraps.h>
  4. #import <appkit/NXImage.h>
  5. #import <appkit/NXBitmapImageRep.h>
  6. #import <appkit/Application.h>
  7. #import <stdlib.h>
  8.  
  9. @implementation ToyView
  10.  
  11. /* Local Method */
  12. - setupInfo
  13. {
  14.     NXRect rect;
  15.     NXImageRep *rep;
  16.     int x;
  17.  
  18.     [image getSize: &origSize];
  19.     if (origSize.width == 0 || origSize.height == 0)
  20.         return nil;    /* maybe Filter error */
  21.     rawmap = NULL;
  22.     [image setScalable: YES];
  23.     [image setDataRetained: YES];
  24.     // [image setBackgroundColor: NX_COLORWHITE];
  25.     rep = [image bestRepresentation];
  26.     if ((x = [rep pixelsWide]) != NX_MATCHESDEVICE
  27.             && x != origSize.width) {
  28.         origSize.width = x;
  29.         origSize.height = [rep pixelsHigh];
  30.         [image setSize: &origSize];
  31.     }
  32.     scaleFactor = 1.0;
  33.     backgray = 1.0;        /* White */
  34.     rect.size = curSize = origSize;
  35.     rect.origin.x = rect.origin.y = 0;
  36.     [self initFrame: &rect];
  37.  
  38.     comInfo = (commonInfo *)malloc(sizeof(commonInfo));
  39.     comInfo->width    = origSize.width;
  40.     comInfo->height    = origSize.height;
  41.     comInfo->bits    = [rep bitsPerSample];
  42.     comInfo->numcolors = [rep numColors]; /* without alpha */
  43.     comInfo->alpha    = [rep hasAlpha];
  44.     if ( [rep isKindOf:[NXBitmapImageRep class]] ) {
  45.         comInfo->xbytes    = [(NXBitmapImageRep *)rep bytesPerRow];
  46.         comInfo->cspace    = [(NXBitmapImageRep *)rep colorSpace];
  47.         comInfo->isplanar = [(NXBitmapImageRep *)rep isPlanar];
  48.         comInfo->type = Type_tiff;
  49.     }else {
  50.         comInfo->xbytes    = 0;
  51.         //  comInfo->cspace    = 0;  DON'T CARE
  52.         comInfo->alpha    = YES;
  53.         comInfo->isplanar = YES;    /* maybe... */
  54.         comInfo->type = Type_eps;
  55.     }
  56.     comInfo->palette = NULL;
  57.     comInfo->palsteps = 0;
  58.     comInfo->memo[0] = 0;
  59.  
  60.     selectRect.size.width = 0.0;
  61.     selectRect.size.height = 0.0;
  62.  
  63.     return self;
  64. }
  65.  
  66. - initFromFile:(const char *)filename
  67. {
  68.     if ((image = [[NXImage alloc] initFromFile: filename]) == nil)
  69.         return nil;
  70.     [ToyView cursor];
  71.     return [self setupInfo];
  72. }
  73.  
  74. - initFromStream:(NXStream *)stream
  75. {
  76.     if ((image = [[NXImage alloc] initFromStream: stream]) == nil)
  77.         return nil;
  78.     [ToyView cursor];
  79.     return [self setupInfo];
  80. }
  81.  
  82.  
  83. - initDataPlanes:(unsigned char **)planes info:(commonInfo *)cinf
  84. {
  85.     NXRect frect;
  86.     NXBitmapImageRep *imageRep;
  87.     int spp;
  88.  
  89.     frect.origin.x = frect.origin.y = 0;
  90.     origSize.width  = cinf->width;
  91.     origSize.height = cinf->height;
  92.     frect.size = curSize = origSize;
  93.     scaleFactor = 1.0;
  94.     backgray = 1.0;        /* White */
  95.     rawmap = planes[0];
  96.     comInfo = cinf;
  97.  
  98.     [self initFrame: &frect];
  99.     spp = cinf->numcolors;
  100.     if (cinf->alpha) spp++;
  101.     imageRep = [[NXBitmapImageRep alloc] initDataPlanes: planes
  102.             pixelsWide: cinf->width    pixelsHigh: cinf->height
  103.             bitsPerSample: cinf->bits    samplesPerPixel: spp 
  104.             hasAlpha: cinf->alpha    isPlanar: YES 
  105.             colorSpace: cinf->cspace    bytesPerRow: cinf->xbytes 
  106.             bitsPerPixel: cinf->bits];
  107.     if (imageRep == nil)
  108.         return nil;
  109.     if ((image = [[NXImage alloc] initSize:&origSize]) == nil) {
  110.         [imageRep free];
  111.         return nil;
  112.     }
  113.     [image setScalable: YES];
  114.     [image setDataRetained: YES];
  115.     [image useRepresentation:imageRep];
  116.     // [image setBackgroundColor: NX_COLORWHITE]; NO NEED!!
  117.     [ToyView cursor];
  118.     return self;
  119. }
  120.  
  121. - setCommText: (TextField *)text
  122. {
  123.     commText = text;
  124.     return self;
  125. }
  126.  
  127. - (NXSize *)originalSize
  128. {
  129.     return &origSize;
  130. }
  131.  
  132. - (NXSize *)resize: (float)factor
  133. {
  134.     int wd = (int)(origSize.width * factor + 0.5);
  135.     int ht = (int)(origSize.height * factor + 0.5);
  136.     if (wd > MAXWidth || ht > MAXWidth || wd < 4 || ht < 4)
  137.         return NULL;
  138.     curSize.width = wd;
  139.     curSize.height = ht;
  140.     scaleFactor = factor;
  141.     backgray = 1.0;        /* White */
  142.     [self sizeTo: curSize.width : curSize.height];
  143.     [window invalidateCursorRectsForView:self];
  144.     [image setSize: &curSize];
  145.     [self clearDraggedLine];
  146.     return &curSize;
  147. }
  148.  
  149. - free
  150. {
  151.     if (image != nil)
  152.         [image free];
  153.     if (rawmap) free((void *)rawmap);
  154.     /* NXBitmapImageRep's Bug ??
  155.         Method "initDataPlanes: planes ..." allocates inside
  156.         an area of 20 bytes such as "unsigned char *planes[5]".
  157.         This area is not freed and becomes a leak node.
  158.     */
  159.     if (comInfo) {
  160.         if (comInfo->palette) free((void *)comInfo->palette);
  161.         free((void *)comInfo);
  162.     }
  163.     return [super free];
  164. }
  165.  
  166. - (NXImage *)image
  167. {
  168.     return image;
  169. }
  170.  
  171. - (commonInfo *)commonInfo
  172. {
  173.     return comInfo;
  174. }
  175.  
  176. - (NXRect *)selectedRect
  177. {
  178.     return &selectRect;
  179. }
  180.  
  181. - (NXRect *)selectedScaledRect
  182. {
  183.     static NXRect rct;
  184.  
  185.     if (scaleFactor == 1.0) return &selectRect;
  186.     rct.size.width = (int)(selectRect.size.width / scaleFactor + 0.5);
  187.     rct.size.height = (int)(selectRect.size.height / scaleFactor + 0.5);
  188.     rct.origin.x = (int)(selectRect.origin.x / scaleFactor + 0.5);
  189.     rct.origin.y = (int)(selectRect.origin.y / scaleFactor + 0.5);
  190.     return &rct;
  191. }
  192.  
  193. - (float)scaleFactor
  194. {
  195.     return scaleFactor;
  196. }
  197.  
  198. /* Overload */
  199. - beginPrologueBBox:(const NXRect *)boundingBox
  200.     creationDate:(const char *)dateCreated
  201.     createdBy:(const char *)anApplication
  202.     fonts:(const char *)fontNames
  203.     forWhom:(const char *)user
  204.     pages:(int)numPages
  205.     title:(const char *)aTitle
  206. {
  207. /* Not to use the title of the window as %%Title: of EPS file. */
  208.  
  209.     char buf[MAXFILENAMELEN];
  210.     const char *p;
  211.     int i, cc;
  212.  
  213.     p = aTitle;
  214.     if (!p)
  215.         p = [window title];
  216.     for (i = 0; ; i++) {
  217.         cc = p[i];
  218.         if (cc == 0 || (cc > 0 && cc <= ' ')
  219.         || cc == '(' || cc == ')' ) break;
  220.         buf[i] = cc;
  221.     }
  222.     buf[i] = 0;
  223.     return [super beginPrologueBBox:boundingBox
  224.         creationDate:dateCreated
  225.         createdBy:anApplication
  226.         fonts:fontNames
  227.         forWhom:user
  228.         pages:numPages
  229.         title:(const char *)buf];
  230. }
  231.  
  232. @end
  233.