home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / Graphics / Misc / IconBuilder / SilkScreen / source / bitmap.m < prev    next >
Encoding:
Text File  |  1993-03-11  |  4.5 KB  |  234 lines

  1. #import "bitmap.h"
  2.  
  3.  
  4. NXColor
  5. ReadPixel (unsigned char **data,
  6.     int width,
  7.     int height,
  8.     int bps,
  9.     int spp,
  10.     BOOL hasAlpha,
  11.     BOOL isPlanar,
  12.     NXColorSpace colourSpace,
  13.     int x,
  14.     int y)
  15. {
  16.     int bpp, offset, index, maxval, i;
  17.     int sample, samples[5];
  18.     float components[5];
  19.     NXColor colour;
  20.     
  21.     
  22.     /* Number of bits per pixel. */
  23.     bpp = bps * spp;
  24.  
  25.  
  26.     /* Calculate the offset (in bits) into the data. */
  27.     offset = (y * width + x) * bpp;
  28.     index = offset >> 3;
  29.  
  30.  
  31.     /* Read the samples. */
  32.     maxval = (0x1 << bps) - 1;
  33.  
  34.     if (isPlanar) {
  35.         for (i=0; i<spp && data[i]; i++)
  36.             samples[i] = (int)(data[i][index] >> ((8-bps) - (offset & 0x7))) & maxval;
  37.  
  38.     } else {
  39.         for (i=0; i<spp; i++) {
  40.             index = offset >> 3;
  41.             samples[i] = (int)(data[0][index] >> ((8-bps) - (offset & 0x7))) & maxval;
  42.             offset += bps;
  43.         }
  44.     }
  45.     
  46.     
  47.     /* Convert the samples to [0, 1] interval. */
  48.     for (i=0; i<spp; i++)
  49.         components[i] = (float)samples[i] / (float)maxval;
  50.     
  51.     
  52.     /* Build the NXColor object. */
  53.     if (hasAlpha) {
  54.         switch (colourSpace) {
  55.             case NX_OneIsBlackColorSpace:
  56.                 colour = NXConvertGrayAlphaToColor(1. - components[0],
  57.                     components[1]);
  58.                 break;
  59.     
  60.             case NX_OneIsWhiteColorSpace:
  61.                 colour = NXConvertGrayAlphaToColor(components[0],
  62.                     components[1]);
  63.                 break;
  64.     
  65.             case NX_RGBColorSpace:
  66.                 colour = NXConvertRGBAToColor(components[0],
  67.                     components[1],
  68.                     components[2],
  69.                     components[3]);
  70.                 break;
  71.     
  72.             case NX_CMYKColorSpace:
  73.                 colour = NXConvertCMYKAToColor(components[0],
  74.                     components[1],
  75.                     components[2],
  76.                     components[3],
  77.                     components[4]);
  78.                 break;
  79.     
  80.             default:
  81.                 colour = NXConvertGrayAlphaToColor(0., 1.);
  82.         }
  83.     } else {
  84.         switch (colourSpace) {
  85.             case NX_OneIsBlackColorSpace:
  86.                 colour = NXConvertGrayToColor(1. - components[0]);
  87.                 break;
  88.     
  89.             case NX_OneIsWhiteColorSpace:
  90.                 colour = NXConvertGrayToColor(components[0]);
  91.                 break;
  92.     
  93.             case NX_RGBColorSpace:
  94.                 colour = NXConvertRGBToColor(components[0],
  95.                     components[1],
  96.                     components[2]);
  97.                 break;
  98.     
  99.             case NX_CMYKColorSpace:
  100.                 colour = NXConvertCMYKToColor(components[0],
  101.                     components[1],
  102.                     components[2],
  103.                     components[3]);
  104.                 break;
  105.     
  106.             default:
  107.                 colour = NX_COLORBLACK;
  108.         }
  109.     }
  110.     
  111.     return colour;
  112. }
  113.  
  114.  
  115. void
  116. WritePixel (unsigned char **data,
  117.     int width,
  118.     int height,
  119.     int bps,
  120.     int spp,
  121.     BOOL hasAlpha,
  122.     BOOL isPlanar,
  123.     NXColorSpace colourSpace,
  124.     int x,
  125.     int y,
  126.     NXColor colour)
  127. {
  128.     int bpp, offset, index, maxval, i;
  129.     int sample, samples[5];
  130.     float components[5];
  131.     
  132.     
  133.     /* Convert the colour into its individual components. */
  134.     if (hasAlpha) {
  135.         switch (colourSpace) {
  136.             case NX_OneIsBlackColorSpace:
  137.                 NXConvertColorToGrayAlpha(colour,
  138.                     &components[0],
  139.                     &components[1]);
  140.                 
  141.                 components[0] = 1. - components[0];
  142.                 break;
  143.     
  144.             case NX_OneIsWhiteColorSpace:
  145.                 NXConvertColorToGrayAlpha(colour,
  146.                     &components[0],
  147.                     &components[1]);
  148.                 break;
  149.     
  150.             case NX_RGBColorSpace:
  151.                 NXConvertColorToRGBA(colour,
  152.                     &components[0],
  153.                     &components[1],
  154.                     &components[2],
  155.                     &components[3]);
  156.                 break;
  157.     
  158.             case NX_CMYKColorSpace:
  159.                  NXConvertColorToCMYKA(colour,
  160.                     &components[0],
  161.                     &components[1],
  162.                     &components[2],
  163.                     &components[3],
  164.                     &components[4]);
  165.                 break;
  166.     
  167.             default:
  168.                 return;
  169.         }
  170.     } else {
  171.         switch (colourSpace) {
  172.             case NX_OneIsBlackColorSpace:
  173.                 NXConvertColorToGray(colour, &components[0]);
  174.                 components[0] = 1. - components[0];
  175.                 break;
  176.     
  177.             case NX_OneIsWhiteColorSpace:
  178.                 NXConvertColorToGray(colour, &components[0]);
  179.                 break;
  180.     
  181.             case NX_RGBColorSpace:
  182.                 NXConvertColorToRGB(colour,
  183.                     &components[0],
  184.                     &components[1],
  185.                     &components[2]);
  186.                 break;
  187.     
  188.             case NX_CMYKColorSpace:
  189.                  NXConvertColorToCMYK(colour,
  190.                     &components[0],
  191.                     &components[1],
  192.                     &components[2],
  193.                     &components[3]);
  194.                 break;
  195.     
  196.             default:
  197.                 return;
  198.         }
  199.     }
  200.     
  201.     
  202.     /* Convert the colour value into the sample range. */
  203.     maxval = (0x1 << bps) - 1;
  204.     for (i=0; i<spp; i++)
  205.         samples[i] = (int)(maxval * components[i]);
  206.  
  207.     
  208.     /* Number of bits per pixel. */
  209.     bpp = bps * spp;
  210.     
  211.     /* Calculate the offset (in bits) into the data. */
  212.     offset = (y * width + x) * bpp;
  213.     index = offset >> 3;
  214.     
  215.     if (isPlanar) {
  216.         for (i=0; i<spp && data[i]; i++) {
  217.             sample = samples[i] << ((8-bps) - (offset & 0x7));
  218.             data[i][index] &= ~(maxval << ((8-bps) - (offset & 0x7)));
  219.             data[i][index] |= sample;
  220.         }
  221.     } else {
  222.         for (i=0; i<spp; i++) {
  223.             index = offset >> 3;
  224.             sample = samples[i] << ((8-bps) - (offset & 0x7));
  225.             data[0][index] &= ~(maxval << ((8-bps) - (offset & 0x7)));
  226.             data[0][index] |= sample;
  227.             
  228.             offset += bps;
  229.         }
  230.     }
  231. }
  232.  
  233.  
  234.