home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / Rhapsody / Graphics / Morph-2.0a / NSBitmapImageRep_editing.m < prev    next >
Encoding:
Text File  |  1998-01-25  |  4.4 KB  |  166 lines

  1. // NSBitmapImageRep_editing.m 
  2. //
  3. // Implements pixel color manipulations for bitmaps.
  4. // Only works for a few image formags such as 24 bit RGB and 8 bit gray.
  5. // This code should be rewritten or complemented to handle all possible data
  6. // formats. Anti aliasing would me nice as well.
  7. //
  8. // created by Martin Wennerberg on Sun 11-Aug-1996
  9. //
  10. // when        who    modification
  11.  
  12. #import "NSBitmapImageRep_editing.h"
  13.  
  14. #ifndef rint
  15. #define rint(x)    ((x) + 0.5)
  16. #endif
  17.  
  18. @implementation NSBitmapImageRep(editing)
  19. - (void) setRGBA:(struct RGBA) col atPoint:(NSPoint) point
  20. {
  21.     int        i;
  22.     int        numColors = _moreRepFlags.numColors;
  23.     int        bytesPerRow = _bytesPerRow;
  24.     BOOL    isPlanar = _moreRepFlags.isPlanar;
  25.     unsigned char    *planes[5];
  26.     unsigned char    *plane;
  27.     int             x, y;
  28.     NSSize         size;
  29.  
  30.     size = [self size];
  31.     x = rint (point.x);
  32.     y = rint (size.height - point.y) - 1;
  33.     if (x < 0 || x > size.width || y < 0 || y > size.height)
  34.         return;
  35.  
  36.     [self getBitmapDataPlanes:planes];
  37.  
  38.     if (!isPlanar)
  39.     {
  40.         plane = planes[0];
  41.         i = rint(bytesPerRow * y + x * numColors);
  42.         switch (numColors) // Does not support CMYK or HSB
  43.         {
  44.             case 1:
  45.                 plane[i] = col.r * 255.0;
  46.                 break;
  47.             case 2:
  48.                 plane[i] = col.r * 255.0;
  49.                 plane[i + 1] = col.a * 255.0 ;
  50.                 break;
  51.             case 3:
  52.                 plane[i] = col.r * 255.0;
  53.                 plane[i + 1] = col.g * 255.0 ;
  54.                 plane[i + 2] = col.b * 255.0 ;
  55.                 break;
  56.             case 4:
  57.                 plane[i] = col.r * 255.0;
  58.                 plane[i + 1] = col.g * 255.0 ;
  59.                 plane[i + 2] = col.b * 255.0 ;
  60.                 plane[i + 3] = col.a * 255.0  ;
  61.                 break;
  62.            default:
  63.                break;
  64.         }
  65.     }
  66.     else
  67.     {
  68.         i = bytesPerRow * y + x;
  69.         switch (numColors)
  70.         {
  71.             case 1:
  72.                 planes[0][i] = col.r * 255.0;
  73.                 break;
  74.             default:
  75.                 break;
  76.         }
  77.     }
  78.     return;
  79. }
  80.  
  81. - (struct RGBA) RGBAAtPoint:(NSPoint)point
  82. {
  83.     struct RGBA    color;
  84.     int        i;
  85.     int        numColors = _moreRepFlags.numColors;
  86.     int        bytesPerRow = _bytesPerRow;
  87.     BOOL    isPlanar = _moreRepFlags.isPlanar;
  88.     int     bitsPerPixel = _moreRepFlags.bitsPerPixel;
  89.     unsigned char    *planes[5];
  90.     unsigned char    *plane;
  91.     int             x, y;
  92.     NSSize         size;
  93.  
  94.     memset (&color, 0, sizeof(color));
  95.     size = [self size];
  96.     x = rint (point.x);
  97.     y = rint (size.height - point.y) - 1.0;
  98.     if (x < 0 || x > size.width || y < 0 || y > size.height - 1.0)
  99.         return color;
  100.  
  101.     color.a = 1;
  102.     
  103.     [self getBitmapDataPlanes:planes];
  104.     
  105.     if (!isPlanar)
  106.     {
  107.         plane = planes[0];
  108.         i = rint(bytesPerRow * y + (x * bitsPerPixel / 8));
  109.         switch (numColors)
  110.         {
  111.             case 1:
  112.                 color.r = color.b = color. g = plane[i] / 255.0;
  113.                 break;
  114.             case 2:
  115.                 color.r = color.b = color. g = plane[i] / 255.0;
  116.                 color.a = plane[i + 1] / 255.0;
  117.                 break;
  118.             case 3:
  119.                 color.r = plane[i] / 255.0;
  120.                 color.g = plane[i + 1] / 255.0;
  121.                 color.b = plane[i + 2] / 255.0;
  122.                 break;
  123.             case 4:
  124.                 color.r = plane[i] / 255.0;
  125.                 color.g = plane[i + 1] / 255.0;
  126.                 color.b = plane[i + 2] / 255.0;
  127.                 color.a = plane[i + 3] / 255.0;
  128.                 break;
  129.             default:
  130.                 break;
  131.         }
  132.     }
  133.     else
  134.     {
  135.         i = bytesPerRow * y + x * bitsPerPixel / (8 * numColors);
  136. //        i = bytesPerRow * y + x;
  137.         switch (numColors)
  138.         {
  139.             case 1:
  140.                 color.r = color.g = color.b = planes[0][i] / 255.0;
  141.                 break;
  142.             case 2:
  143.                 color.r = planes[0][i] / 255.0;
  144.                 color.a = planes[1][i] / 255.0;
  145.                 break;
  146.             case 3:
  147.                 color.r = planes[0][i] / 255.0;
  148.                 color.g = planes[1][i] / 255.0;
  149.                 color.b = planes[2][i] / 255.0;
  150.                 break;
  151.             case 4:
  152.                 color.r = planes[0][i] / 255.0;
  153.                 color.g = planes[1][i] / 255.0;
  154.                 color.b = planes[2][i] / 255.0;
  155.                 color.a = planes[3][i] / 255.0;
  156.                 break;
  157.             default:
  158.                 break;
  159.         }
  160.     }
  161.    
  162.     return color;
  163. }
  164.  
  165. @end
  166.