home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / ToyView.m < prev    next >
Encoding:
Text File  |  1995-11-12  |  4.0 KB  |  186 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.     rect.size = curSize = origSize;
  34.     rect.origin.x = rect.origin.y = 0;
  35.     [self initFrame: &rect];
  36.  
  37.     comInfo = (commonInfo *)malloc(sizeof(commonInfo));
  38.     comInfo->width    = origSize.width;
  39.     comInfo->height    = origSize.height;
  40.     comInfo->bits    = [rep bitsPerSample];
  41.     comInfo->numcolors = [rep numColors]; /* without alpha */
  42.     comInfo->alpha    = [rep hasAlpha];
  43.     if ( [rep isKindOf:[NXBitmapImageRep class]] ) {
  44.         comInfo->xbytes    = [(NXBitmapImageRep *)rep bytesPerRow];
  45.         comInfo->cspace    = [(NXBitmapImageRep *)rep colorSpace];
  46.         comInfo->isplanar = [(NXBitmapImageRep *)rep isPlanar];
  47.         comInfo->type = Type_tiff;
  48.     }else {
  49.         comInfo->xbytes    = 0;
  50.         //  comInfo->cspace    = 0;  DON'T CARE
  51.         comInfo->alpha    = YES;
  52.         comInfo->isplanar = YES;    /* maybe... */
  53.         comInfo->type = Type_eps;
  54.     }
  55.     comInfo->palette = NULL;
  56.     comInfo->palsteps = 0;
  57.     comInfo->memo[0] = 0;
  58.  
  59.     selectRect.size.width = 0.0;
  60.     selectRect.size.height = 0.0;
  61.  
  62.     return self;
  63. }
  64.  
  65. - initFromFile:(const char *)filename
  66. {
  67.     if ((image = [[NXImage alloc] initFromFile: filename]) == nil)
  68.         return nil;
  69.     [ToyView cursor];
  70.     return [self setupInfo];
  71. }
  72.  
  73. - initFromStream:(NXStream *)stream
  74. {
  75.     if ((image = [[NXImage alloc] initFromStream: stream]) == nil)
  76.         return nil;
  77.     [ToyView cursor];
  78.     return [self setupInfo];
  79. }
  80.  
  81.  
  82. - initDataPlanes:(unsigned char **)planes info:(commonInfo *)cinf
  83. {
  84.     NXRect frect;
  85.     NXBitmapImageRep *imageRep;
  86.     int spp;
  87.  
  88.     frect.origin.x = frect.origin.y = 0;
  89.     origSize.width  = cinf->width;
  90.     origSize.height = cinf->height;
  91.     frect.size = curSize = origSize;
  92.     scaleFactor = 1.0;
  93.     rawmap = planes[0];
  94.     comInfo = cinf;
  95.  
  96.     [self initFrame: &frect];
  97.     spp = cinf->numcolors;
  98.     if (cinf->alpha) spp++;
  99.     imageRep = [[NXBitmapImageRep alloc] initDataPlanes: planes
  100.             pixelsWide: cinf->width    pixelsHigh: cinf->height
  101.             bitsPerSample: cinf->bits    samplesPerPixel: spp 
  102.             hasAlpha: cinf->alpha    isPlanar: YES 
  103.             colorSpace: cinf->cspace    bytesPerRow: cinf->xbytes 
  104.             bitsPerPixel: cinf->bits];
  105.     if (imageRep == nil)
  106.         return nil;
  107.     if ((image = [[NXImage alloc] initSize:&origSize]) == nil) {
  108.         [imageRep free];
  109.         return nil;
  110.     }
  111.     [image setScalable: YES];
  112.     [image setDataRetained: YES];
  113.     [image useRepresentation:imageRep];
  114.     // [image setBackgroundColor: NX_COLORWHITE]; NO NEED!!
  115.     [ToyView cursor];
  116.     return self;
  117. }
  118.  
  119. - setCommText: (TextField *)text
  120. {
  121.     commText = text;
  122.     return self;
  123. }
  124.  
  125. - setPreference: (PrefControl *)pref
  126. {
  127.     preference = pref;
  128.     return self;
  129. }
  130.  
  131. - (NXSize *)originalSize
  132. {
  133.     return &origSize;
  134. }
  135.  
  136. - (NXSize *)resize: (float)factor
  137. {
  138.     int wd = (int)(origSize.width * factor + 0.5);
  139.     int ht = (int)(origSize.height * factor + 0.5);
  140.     if (wd > MAXWidth || ht > MAXWidth || wd < 4 || ht < 4)
  141.         return NULL;
  142.     curSize.width = wd;
  143.     curSize.height = ht;
  144.     scaleFactor = factor;
  145.     [self sizeTo: curSize.width : curSize.height];
  146.     [window invalidateCursorRectsForView:self];
  147.     [image setSize: &curSize];
  148.     [self clearDraggedLine];
  149.     return &curSize;
  150. }
  151.  
  152. - free
  153. {
  154.     if (image != nil)
  155.         [image free];
  156.     if (rawmap) free((void *)rawmap);
  157.     if (comInfo) {
  158.         if (comInfo->palette) free((void *)comInfo->palette);
  159.         free((void *)comInfo);
  160.     }
  161.     return [super free];
  162. }
  163.  
  164. - (NXImage *)image
  165. {
  166.     return image;
  167. }
  168.  
  169. - (commonInfo *)commonInfo
  170. {
  171.     return comInfo;
  172. }
  173.  
  174. - (NXRect *)selectedRect
  175. {
  176.     return &selectRect;
  177. }
  178.  
  179. - (float)scaleFactor
  180. {
  181.     return scaleFactor;
  182. }
  183.  
  184.  
  185. @end
  186.