home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Graphics / ToyViewer-2.6a / src / ImageOpr.bproj / ImageCutDown.m < prev    next >
Encoding:
Text File  |  1997-01-22  |  3.4 KB  |  140 lines

  1. #import "../ImageOpr.h"
  2. #import <appkit/Application.h>
  3. #import <appkit/publicWraps.h>
  4. #import <appkit/NXBitmapImageRep.h>
  5. #import <appkit/Control.h>
  6. #import <stdio.h>
  7. #import <stdlib.h>
  8. #import <string.h>
  9. #import <libc.h>
  10. #import "../TVController.h"
  11. #import "../ToyWin.h"
  12. #import "../ToyView.h"
  13. #import "../common.h"
  14. #import "../getpixel.h"
  15. #import "imageOperation.h"
  16.  
  17.  
  18. @implementation ImageOpr (CutDownBits)
  19.  
  20. /* Local Method */
  21. - doCutDown:(const char *)fn parent:parent
  22.     info:(commonInfo *)cinf map:(unsigned char **)map to:(int)bits
  23. {
  24.     ToyWin    *tw;
  25.     commonInfo *newinf = NULL;
  26.     unsigned char *working[MAXPLANE], *planes[MAXPLANE];
  27.     int    i, pl;
  28.  
  29.     working[0] = planes[0] = NULL;
  30.     tw = NULL;
  31.     if ((newinf = (commonInfo *)malloc(sizeof(commonInfo))) == NULL)
  32.         goto ErrEXIT;
  33.     *newinf = *cinf;
  34.     if (cinf->cspace == NX_OneIsBlackColorSpace)
  35.         newinf->cspace = NX_OneIsWhiteColorSpace;
  36.         /* getPixel() fixes 0 as Black */
  37.     newinf->bits = bits;
  38.     newinf->xbytes = byte_length(bits, newinf->width);
  39.     newinf->palette = NULL;
  40.     newinf->palsteps = 0;
  41.     newinf->isplanar = YES;
  42.     pl = newinf->numcolors;
  43.     if (newinf->alpha) pl++;
  44.  
  45.     if (allocImage(planes, newinf->width, newinf->height, bits, pl))
  46.         goto ErrEXIT;
  47.     tw = [[ToyWin alloc] init:parent by:CutDown];
  48.     [tw initLocateWindow:fn width:newinf->width height:newinf->height];
  49.     [tw makeComment: newinf from: cinf];
  50.  
  51.     if (!cinf->isplanar || cinf->bits < 8) {
  52.         int    x, y, ptr, pidx;
  53.         int    idx[MAXPLANE];
  54.         int    pix[MAXPLANE];
  55.  
  56.         for (i = 0; i < newinf->numcolors; i++) idx[i] = i;
  57.         for ( ; i < MAXPLANE; i++) idx[i] = -1;
  58.         if (newinf->alpha) idx[ALPHA] = pl;
  59.         if (allocImage(working, newinf->width, newinf->height, 8, pl))
  60.             goto ErrEXIT;
  61.         resetPixel(map, 0);
  62.         for (y = 0; y < newinf->height; y++) {
  63.             ptr = y * newinf->width;
  64.             for (x = 0; x < newinf->width; x++) {
  65.                 getPixelA(pix);
  66.                 for (i = 0; i <= ALPHA; i++) {
  67.                     if ((pidx = idx[i]) < 0) continue;
  68.                     working[pidx][ptr + x] = pix[i];
  69.                 }
  70.             }
  71.         }
  72.         packWorkingImage(newinf, pl, working, planes);
  73.         free((void *)working[0]);
  74.     }else
  75.         packWorkingImage(newinf, pl, map, planes);
  76.     if ([tw drawView:planes info: newinf] == nil)
  77.         goto ErrEXIT;
  78.     [theController newWindow: tw];
  79.     return self;
  80.  
  81. ErrEXIT:
  82.     if (working[0]) free((void *)working[0]);
  83.     if (planes[0]) free((void *)planes[0]);
  84.     if (newinf)  free((void *)newinf);
  85.     if (tw) [[tw window] performClose: self];
  86.         /* This call frees tw */
  87.     return nil;
  88. }
  89.  
  90.  
  91. - cutDownBitsTo:(int)bits
  92. {
  93.     ToyWin    *tw;
  94.     ToyView    *tv = NULL;
  95.     NXImageRep    *rep;
  96.     commonInfo    *cinf;
  97.     unsigned char    *map[MAXPLANE];
  98.     const char    *fnam;
  99.     char    fn[256];
  100.     int    err;
  101.  
  102.     if ((tw = [theController keyWindow]) == nil ||
  103.         (bits != 4 && bits != 2 && bits != 1)) {
  104.         NXBeep();
  105.         return self;
  106.     }
  107.     tv = [tw toyView];
  108.     cinf = [tv commonInfo];
  109.     fnam = [tw filename];
  110.     if (cinf->type == Type_eps) {
  111.         warnAlert(fnam, Err_EPS_IMPL);
  112.         return self;
  113.     }
  114.     if (cinf->bits <= bits) {
  115.         warnAlert(fnam, Err_OPR_IMPL);
  116.         return self;
  117.     }
  118.     if (cinf->cspace == NX_CMYKColorSpace) {
  119.         warnAlert(fnam, Err_IMPLEMENT);
  120.         return self;
  121.     }
  122.     if (cinf->width >= MAXWidth || cinf->height >= MAXWidth) {
  123.         errAlert(fnam, Err_MEMORY);
  124.         return self;
  125.     }
  126.     sprintf(fn, "%s(%dBits)", fnam, bits);
  127.  
  128.     rep = [[tv image] bestRepresentation];
  129.     [(NXBitmapImageRep *)rep getDataPlanes: map];
  130.     if ((err = initGetPixel(cinf)) != 0) {
  131.         errAlert(fnam, err);
  132.         return self;
  133.     }
  134.     if ([self doCutDown:fn parent:tw info:cinf map:map to:bits] == nil)
  135.         errAlert(fn, Err_MEMORY);
  136.     return self;
  137. }
  138.  
  139. @end
  140.