private static byte[] getIndexedRGBPixels(BufferedImage img) {
    byte[] indices = ((java.awt.image.DataBufferByte) img.getRaster().getDataBuffer()).getData();
    int numOfPixels = indices.length;
    ColorModel model = img.getColorModel();
    byte[] rgbs = null;

    if (model.hasAlpha()) {
      rgbs = new byte[numOfPixels * 4];
      int i = 0;
      for (int j = 0; j < numOfPixels; j++) {
        int argb = model.getRGB(indices[j]);
        rgbs[i++] = (byte) ((argb >> 24) & 0xff);
        rgbs[i++] = (byte) ((argb) & 0xff);
        rgbs[i++] = (byte) ((argb >> 8) & 0xff);
        rgbs[i++] = (byte) ((argb >> 16) & 0xff);
      }
    } else {
      rgbs = new byte[numOfPixels * 3];
      int i = 0;
      for (int j = 0; j < numOfPixels; j++) {
        int argb = model.getRGB(indices[j]);
        rgbs[i++] = (byte) ((argb) & 0xff);
        rgbs[i++] = (byte) ((argb >> 8) & 0xff);
        rgbs[i++] = (byte) ((argb >> 16) & 0xff);
      }
    }

    return rgbs;
  }
 public void setPixels(
     int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) {
   if (model == rgbModel) {
     try {
       encodePixelsWrapper(x, y, w, h, pixels, off, scansize);
     } catch (IOException e) {
       iox = e;
       stop();
       return;
     }
   } else {
     int[] rgbPixels = new int[w];
     for (int row = 0; row < h; ++row) {
       int rowOff = off + row * scansize;
       for (int col = 0; col < w; ++col) rgbPixels[col] = model.getRGB(pixels[rowOff + col]);
       try {
         encodePixelsWrapper(x, y + row, w, 1, rgbPixels, 0, w);
       } catch (IOException e) {
         iox = e;
         stop();
         return;
       }
     }
   }
 }
 /*     */ public void Blit(
     SurfaceData paramSurfaceData1,
     SurfaceData paramSurfaceData2,
     Composite paramComposite,
     Region paramRegion,
     int paramInt1,
     int paramInt2,
     int paramInt3,
     int paramInt4,
     int paramInt5,
     int paramInt6)
       /*     */ {
   /* 120 */ Raster localRaster1 =
       paramSurfaceData1.getRaster(paramInt1, paramInt2, paramInt5, paramInt6);
   /* 121 */ ColorModel localColorModel = paramSurfaceData1.getColorModel();
   /*     */
   /* 123 */ Raster localRaster2 =
       paramSurfaceData2.getRaster(paramInt3, paramInt4, paramInt5, paramInt6);
   /* 124 */ IntegerComponentRaster localIntegerComponentRaster =
       (IntegerComponentRaster) localRaster2;
   /* 125 */ int[] arrayOfInt1 = localIntegerComponentRaster.getDataStorage();
   /*     */
   /* 127 */ Region localRegion =
       CustomComponent.getRegionOfInterest(
           paramSurfaceData1,
           paramSurfaceData2,
           paramRegion,
           paramInt1,
           paramInt2,
           paramInt3,
           paramInt4,
           paramInt5,
           paramInt6);
   /*     */
   /* 130 */ SpanIterator localSpanIterator = localRegion.getSpanIterator();
   /*     */
   /* 132 */ Object localObject = null;
   /*     */
   /* 134 */ int i = localIntegerComponentRaster.getScanlineStride();
   /*     */
   /* 136 */ paramInt1 -= paramInt3;
   /* 137 */ paramInt2 -= paramInt4;
   /* 138 */ int[] arrayOfInt2 = new int[4];
   /* 139 */ while (localSpanIterator.nextSpan(arrayOfInt2)) {
     /* 140 */ int j =
         localIntegerComponentRaster.getDataOffset(0) + arrayOfInt2[1] * i + arrayOfInt2[0];
     /* 141 */ for (int k = arrayOfInt2[1]; k < arrayOfInt2[3]; k++) {
       /* 142 */ int m = j;
       /* 143 */ for (int n = arrayOfInt2[0]; n < arrayOfInt2[2]; n++) {
         /* 144 */ localObject =
             localRaster1.getDataElements(n + paramInt1, k + paramInt2, localObject);
         /* 145 */ arrayOfInt1[(m++)] = localColorModel.getRGB(localObject);
         /*     */ }
       /* 147 */ j += i;
       /*     */ }
     /*     */
     /*     */ }
   /*     */
   /* 152 */ localIntegerComponentRaster.markDirty();
   /*     */ }
 /**
  * {@collect.stats} If the ColorModel object is the same one that has already been converted, then
  * simply passes the pixels through with the converted ColorModel, otherwise converts the buffer
  * of integer pixels to the default RGB ColorModel and passes the converted buffer to the
  * filterRGBPixels method to be converted one by one. Converts a buffer of integer pixels to the
  * default RGB ColorModel and passes the converted buffer to the filterRGBPixels method.
  *
  * <p>Note: This method is intended to be called by the <code>ImageProducer</code> of the <code>
  * Image</code> whose pixels are being filtered. Developers using this class to filter pixels from
  * an image should avoid calling this method directly since that operation could interfere with
  * the filtering operation.
  *
  * @see ColorModel#getRGBdefault
  * @see #filterRGBPixels
  */
 public void setPixels(
     int x, int y, int w, int h, ColorModel model, int pixels[], int off, int scansize) {
   if (model == origmodel) {
     consumer.setPixels(x, y, w, h, newmodel, pixels, off, scansize);
   } else {
     int filteredpixels[] = new int[w];
     int index = off;
     for (int cy = 0; cy < h; cy++) {
       for (int cx = 0; cx < w; cx++) {
         filteredpixels[cx] = model.getRGB(pixels[index]);
         index++;
       }
       index += scansize - w;
       filterRGBPixels(x, y + cy, w, 1, filteredpixels, 0, w);
     }
   }
 }
  private void convertToRGB() {
    int w = bimage.getWidth();
    int h = bimage.getHeight();
    int size = w * h;

    DataBufferInt dbi = new DataBufferInt(size);
    // Note that stealData() requires a markDirty() afterwards
    // since we modify the data in it.
    int newpixels[] = SunWritableRaster.stealData(dbi, 0);
    if (cmodel instanceof IndexColorModel
        && biRaster instanceof ByteComponentRaster
        && biRaster.getNumDataElements() == 1) {
      ByteComponentRaster bct = (ByteComponentRaster) biRaster;
      byte[] data = bct.getDataStorage();
      int coff = bct.getDataOffset(0);
      for (int i = 0; i < size; i++) {
        newpixels[i] = srcLUT[data[coff + i] & 0xff];
      }
    } else {
      Object srcpixels = null;
      int off = 0;
      for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
          srcpixels = biRaster.getDataElements(x, y, srcpixels);
          newpixels[off++] = cmodel.getRGB(srcpixels);
        }
      }
    }
    // We modified the data array directly above so mark it as dirty now...
    SunWritableRaster.markDirty(dbi);

    isSameCM = false;
    cmodel = ColorModel.getRGBdefault();

    int bandMasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000};

    biRaster = Raster.createPackedRaster(dbi, w, h, w, bandMasks, null);

    bimage = createImage(cmodel, biRaster, cmodel.isAlphaPremultiplied(), null);
    srcLUT = null;
    isDefaultBI = true;
  }
Ejemplo n.º 6
0
  public static void main(String args[]) {
    ColorModel cm =
        new ColorModel(32) {
          public int getAlpha(int pixel) {
            return 255;
          }

          public int getBlue(int pixel) {
            return 255;
          }

          public int getGreen(int pixel) {
            return 255;
          }

          public int getRed(int pixel) {
            return 255;
          }
        };

    cm.hasAlpha();
    cm.isAlphaPremultiplied();
    cm.getTransferType();
    cm.getPixelSize();
    cm.getComponentSize();
    cm.getComponentSize();
    cm.getTransparency();
    cm.getNumComponents();
    cm.getNumColorComponents();
    cm.getRed(20);
    cm.getGreen(20);
    cm.getBlue(20);
    cm.getAlpha(20);
    cm.getRGB(20);
    cm.isAlphaPremultiplied();
    cm.isAlphaPremultiplied();

    cm = ColorModel.getRGBdefault();
  }
Ejemplo n.º 7
0
  /** Constructs a DirectRasterAccessor object */
  public DirectRasterAccessor(Raster raster, ColorModel cm) {
    DataBuffer db = raster.getDataBuffer();

    offsetX = raster.getMinX() - raster.getSampleModelTranslateX();
    offsetY = raster.getMinY() - raster.getSampleModelTranslateY();

    if (!(db instanceof DataBufferByte)) {
      throw new RuntimeException(
          "DataBuffer of Raster not of correct type "
              + "(expected DataBufferByte, got "
              + db.getClass().getName()
              + ")");
    }

    DataBufferByte dbb = (DataBufferByte) db;

    SampleModel sm = raster.getSampleModel();

    if (!(sm instanceof MultiPixelPackedSampleModel)) {
      throw new RuntimeException(
          "SampleModel of Raster not of correct type "
              + "(expected MultiPixelPackedSampleModel, got "
              + sm.getClass().getName()
              + ")");
    }

    MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sm;

    data = dbb.getData();
    scanlineStride = mppsm.getScanlineStride();

    if (cm.getRGB(0) == Color.white.getRGB()) {
      white = 0;
      black = 1;
    } else {
      white = 1;
      black = 0;
    }
  }
  public void setPixels(
      int x, int y, int w, int h, ColorModel model, int pix[], int off, int scansize) {
    int lineOff = off;
    int poff;

    if (src != null) {
      src.checkSecurity(null, false);
    }

    // REMIND: What if the model doesn't fit in default color model?
    synchronized (this) {
      if (bimage == null) {
        if (cmodel == null) {
          cmodel = model;
        }
        createBufferedImage();
      }

      int[] storage = new int[w];
      int yoff;
      int pixel;

      if (cmodel instanceof IndexColorModel) {
        // REMIND: Right now we don't support writing back into ICM
        // images.
        convertToRGB();
      }

      if ((model == cmodel) && (biRaster instanceof IntegerComponentRaster)) {
        IntegerComponentRaster iraster = (IntegerComponentRaster) biRaster;

        if (off == 0 && scansize == w) {
          iraster.setDataElements(x, y, w, h, pix);
        } else {
          // Need to pack the data
          for (yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
            System.arraycopy(pix, lineOff, storage, 0, w);
            iraster.setDataElements(x, yoff, w, 1, storage);
          }
        }
      } else {
        if (model.getTransparency() != model.OPAQUE && cmodel.getTransparency() == cmodel.OPAQUE) {
          convertToRGB();
        }

        if (isDefaultBI) {
          IntegerComponentRaster iraster = (IntegerComponentRaster) biRaster;
          int[] data = iraster.getDataStorage();
          if (cmodel.equals(model)) {
            int sstride = iraster.getScanlineStride();
            int doff = y * sstride + x;
            for (yoff = 0; yoff < h; yoff++, lineOff += scansize) {
              System.arraycopy(pix, lineOff, data, doff, w);
              doff += sstride;
            }
            // Note: manual modification of pixels, mark the
            // raster as changed
            iraster.markDirty();
          } else {
            for (yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
              poff = lineOff;
              for (int i = 0; i < w; i++) {
                storage[i] = model.getRGB(pix[poff++]);
              }
              iraster.setDataElements(x, yoff, w, 1, storage);
            }
          }

          availinfo |= ImageObserver.SOMEBITS;
        } else {
          Object tmp = null;

          for (yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
            poff = lineOff;
            for (int xoff = x; xoff < x + w; xoff++) {
              pixel = model.getRGB(pix[poff++]);
              tmp = cmodel.getDataElements(pixel, tmp);
              biRaster.setDataElements(xoff, yoff, tmp);
            }
          }
          availinfo |= ImageObserver.SOMEBITS;
        }
      }
    }

    // Can't do this here since we might need to transform/clip
    // the region
    if (((availinfo & ImageObserver.FRAMEBITS) == 0)) {
      newInfo(image, ImageObserver.SOMEBITS, x, y, w, h);
    }
  }
  public void setPixels(
      int x, int y, int w, int h, ColorModel model, byte pix[], int off, int scansize) {
    int lineOff = off;
    int poff;
    int[] newLUT = null;

    if (src != null) {
      src.checkSecurity(null, false);
    }

    // REMIND: What if the model doesn't fit in default color model?
    synchronized (this) {
      if (bimage == null) {
        if (cmodel == null) {
          cmodel = model;
        }
        createBufferedImage();
      }

      if (w <= 0 || h <= 0) {
        return;
      }

      int biWidth = biRaster.getWidth();
      int biHeight = biRaster.getHeight();

      int x1 = x + w; // Overflow protection below
      int y1 = y + h; // Overflow protection below
      if (x < 0) {
        off -= x;
        x = 0;
      } else if (x1 < 0) {
        x1 = biWidth; // Must be overflow
      }
      if (y < 0) {
        off -= y * scansize;
        y = 0;
      } else if (y1 < 0) {
        y1 = biHeight; // Must be overflow
      }
      if (x1 > biWidth) {
        x1 = biWidth;
      }
      if (y1 > biHeight) {
        y1 = biHeight;
      }
      if (x >= x1 || y >= y1) {
        return;
      }
      // x,y,x1,y1 are all >= 0, so w,h must be >= 0
      w = x1 - x;
      h = y1 - y;
      // off is first pixel read so it must be in bounds
      if (off < 0 || off >= pix.length) {
        // They overflowed their own array
        throw new ArrayIndexOutOfBoundsException("Data offset out of bounds.");
      }
      // pix.length and off are >= 0 so remainder >= 0
      int remainder = pix.length - off;
      if (remainder < w) {
        // They overflowed their own array
        throw new ArrayIndexOutOfBoundsException("Data array is too short.");
      }
      int num;
      if (scansize < 0) {
        num = (off / -scansize) + 1;
      } else if (scansize > 0) {
        num = ((remainder - w) / scansize) + 1;
      } else {
        num = h;
      }
      if (h > num) {
        // They overflowed their own array.
        throw new ArrayIndexOutOfBoundsException("Data array is too short.");
      }

      if (isSameCM
          && (cmodel != model)
          && (srcLUT != null)
          && (model instanceof IndexColorModel)
          && (biRaster instanceof ByteComponentRaster)) {
        IndexColorModel icm = (IndexColorModel) model;
        ByteComponentRaster bct = (ByteComponentRaster) biRaster;
        int numlut = numSrcLUT;
        if (!setDiffICM(
            x,
            y,
            w,
            h,
            srcLUT,
            srcLUTtransIndex,
            numSrcLUT,
            icm,
            pix,
            off,
            scansize,
            bct,
            bct.getDataOffset(0))) {
          convertToRGB();
        } else {
          // Note that setDiffICM modified the raster directly
          // so we must mark it as changed
          bct.markDirty();
          if (numlut != numSrcLUT) {
            boolean hasAlpha = icm.hasAlpha();
            if (srcLUTtransIndex != -1) {
              hasAlpha = true;
            }
            int nbits = icm.getPixelSize();
            icm =
                new IndexColorModel(
                    nbits,
                    numSrcLUT,
                    srcLUT,
                    0,
                    hasAlpha,
                    srcLUTtransIndex,
                    (nbits > 8 ? DataBuffer.TYPE_USHORT : DataBuffer.TYPE_BYTE));
            cmodel = icm;
            bimage = createImage(icm, bct, false, null);
          }
          return;
        }
      }

      if (isDefaultBI) {
        int pixel;
        IntegerComponentRaster iraster = (IntegerComponentRaster) biRaster;
        if (srcLUT != null && model instanceof IndexColorModel) {
          if (model != srcModel) {
            // Fill in the new lut
            ((IndexColorModel) model).getRGBs(srcLUT);
            srcModel = model;
          }

          if (s_useNative) {
            // Note that setICMpixels modifies the raster directly
            // so we must mark it as changed afterwards
            if (setICMpixels(x, y, w, h, srcLUT, pix, off, scansize, iraster)) {
              iraster.markDirty();
            } else {
              abort();
              return;
            }
          } else {
            int[] storage = new int[w * h];
            int soff = 0;
            // It is an IndexColorModel
            for (int yoff = 0; yoff < h; yoff++, lineOff += scansize) {
              poff = lineOff;
              for (int i = 0; i < w; i++) {
                storage[soff++] = srcLUT[pix[poff++] & 0xff];
              }
            }
            iraster.setDataElements(x, y, w, h, storage);
          }
        } else {
          int[] storage = new int[w];
          for (int yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
            poff = lineOff;
            for (int i = 0; i < w; i++) {
              storage[i] = model.getRGB(pix[poff++] & 0xff);
            }
            iraster.setDataElements(x, yoff, w, 1, storage);
          }
          availinfo |= ImageObserver.SOMEBITS;
        }
      } else if ((cmodel == model)
          && (biRaster instanceof ByteComponentRaster)
          && (biRaster.getNumDataElements() == 1)) {
        ByteComponentRaster bt = (ByteComponentRaster) biRaster;
        if (off == 0 && scansize == w) {
          bt.putByteData(x, y, w, h, pix);
        } else {
          byte[] bpix = new byte[w];
          poff = off;
          for (int yoff = y; yoff < y + h; yoff++) {
            System.arraycopy(pix, poff, bpix, 0, w);
            bt.putByteData(x, yoff, w, 1, bpix);
            poff += scansize;
          }
        }
      } else {
        for (int yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
          poff = lineOff;
          for (int xoff = x; xoff < x + w; xoff++) {
            bimage.setRGB(xoff, yoff, model.getRGB(pix[poff++] & 0xff));
          }
        }
        availinfo |= ImageObserver.SOMEBITS;
      }
    }

    if ((availinfo & ImageObserver.FRAMEBITS) == 0) {
      newInfo(image, ImageObserver.SOMEBITS, x, y, w, h);
    }
  }
Ejemplo n.º 10
0
  /**
   * Returns the bytes of an image.
   *
   * @param image to be converted to bytes
   * @param bkg the color to be used for alpha-multiplication
   * @param code ARGB, A, or BGR, ... you may also use *ARGB to pre-multiply with alpha
   * @param pad number of bytes to pad the scanline with (1=byte, 2=short, 4=int, ...)
   */
  public static byte[] getBytes(RenderedImage image, Color bkg, String code, int pad) {
    if (pad < 1) pad = 1;

    Raster raster = image.getData();

    int width = image.getWidth();
    int height = image.getHeight();

    boolean preMultiply = (code.charAt(0) == '*');
    if (preMultiply) code = code.substring(1);

    int pixelSize = code.length();

    int size = width * height * pixelSize;
    size += (width % pad) * height;
    int index = 0;
    byte[] bytes = new byte[size];

    ColorModel colorModel = image.getColorModel();

    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {

        int argb = colorModel.getRGB(raster.getDataElements(x, y, (Object) null));
        int a = ((argb >> 24) & 0xFF);
        int r = ((argb >> 16) & 0xFF);
        int g = ((argb >> 8) & 0xFF);
        int b = ((argb >> 0) & 0xFF);

        // Check the transparancy. If transparent substitute
        // the background color.
        if (preMultiply && (a < 0xFF)) {
          if (bkg == null) bkg = Color.BLACK;
          double alpha = a / 255.0;
          r = (int) (alpha * r + (1 - alpha) * bkg.getRed());
          g = (int) (alpha * g + (1 - alpha) * bkg.getGreen());
          b = (int) (alpha * b + (1 - alpha) * bkg.getBlue());
        }

        for (int i = 0; i < code.length(); i++) {
          switch (code.charAt(i)) {
            case 'a':
            case 'A':
              bytes[index] = (byte) a;
              break;

            case 'r':
            case 'R':
              bytes[index] = (byte) r;
              break;

            case 'g':
            case 'G':
              bytes[index] = (byte) g;
              break;

            case 'b':
            case 'B':
              bytes[index] = (byte) b;
              break;

            default:
              System.err.println(
                  ImageUtilities.class.getClass()
                      + ": Invalid code in '"
                      + code
                      + "'"); //$NON-NLS-1$ //$NON-NLS-2$
              break;
          }
          index++;
        }
      }
      for (int i = 0; i < (width % pad); i++) {
        bytes[index] = 0;
        index++;
      }
    }

    return bytes;
  }