public void run(ImageProcessor ip) {
    IndexColorModel icm = (IndexColorModel) ip.getColorModel();
    // IJ.write("Color Model=" + ip.getColorModel() + " " + ip.isColorLut());

    int pixBits = icm.getPixelSize();
    int mapSize = icm.getMapSize();

    // retrieve the current lookup tables (maps) for R,G,B
    byte[] Rmap = new byte[mapSize];
    icm.getReds(Rmap);
    byte[] Gmap = new byte[mapSize];
    icm.getGreens(Gmap);
    byte[] Bmap = new byte[mapSize];
    icm.getBlues(Bmap);

    // modify the lookup tables
    for (int idx = 0; idx < mapSize; idx++) {
      int r = 0xff & Rmap[idx]; // mask to treat as unsigned byte
      int g = 0xff & Gmap[idx];
      int b = 0xff & Bmap[idx];
      Rmap[idx] = (byte) Math.min(r + 10, 255);
      Gmap[idx] = (byte) Math.min(g + 10, 255);
      Bmap[idx] = (byte) Math.min(b + 10, 255);
    }
    // create a new color model and apply to the image
    IndexColorModel icm2 = new IndexColorModel(pixBits, mapSize, Rmap, Gmap, Bmap);
    ip.setColorModel(icm2);

    WindowManager.getCurrentImage().updateAndDraw();
  }
示例#2
0
 static ImageData convertToSWT(BufferedImage bufferedImage) {
   if (bufferedImage.getColorModel() instanceof DirectColorModel) {
     DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
     PaletteData palette =
         new PaletteData(
             colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask());
     ImageData data =
         new ImageData(
             bufferedImage.getWidth(),
             bufferedImage.getHeight(),
             colorModel.getPixelSize(),
             palette);
     WritableRaster raster = bufferedImage.getRaster();
     int[] pixelArray = new int[3];
     for (int y = 0; y < data.height; y++) {
       for (int x = 0; x < data.width; x++) {
         raster.getPixel(x, y, pixelArray);
         int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
         data.setPixel(x, y, pixel);
       }
     }
     return data;
   } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
     IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
     int size = colorModel.getMapSize();
     byte[] reds = new byte[size];
     byte[] greens = new byte[size];
     byte[] blues = new byte[size];
     colorModel.getReds(reds);
     colorModel.getGreens(greens);
     colorModel.getBlues(blues);
     RGB[] rgbs = new RGB[size];
     for (int i = 0; i < rgbs.length; i++) {
       rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
     }
     PaletteData palette = new PaletteData(rgbs);
     ImageData data =
         new ImageData(
             bufferedImage.getWidth(),
             bufferedImage.getHeight(),
             colorModel.getPixelSize(),
             palette);
     data.transparentPixel = colorModel.getTransparentPixel();
     WritableRaster raster = bufferedImage.getRaster();
     int[] pixelArray = new int[1];
     for (int y = 0; y < data.height; y++) {
       for (int x = 0; x < data.width; x++) {
         raster.getPixel(x, y, pixelArray);
         data.setPixel(x, y, pixelArray[0]);
       }
     }
     return data;
   }
   return null;
 }
示例#3
0
  public static void run(int bits, int size) {
    byte[] r = new byte[size];
    byte[] g = new byte[size];
    byte[] b = new byte[size];

    IndexColorModel cm = new IndexColorModel(bits, size, r, g, b);

    System.out.println("Bits         = " + bits);
    System.out.println("Size         = " + size);
    System.out.println("cm.PixelSize = " + cm.getPixelSize());
    System.out.println("cm.MapSize   = " + cm.getMapSize());
  }
  @Test
  public void test4BitPNG() throws Exception {

    // create test image
    IndexColorModel icm =
        new IndexColorModel(
            4,
            16,
            new byte[] {(byte) 255, 0, 0, 0, 16, 32, 64, (byte) 128, 1, 2, 3, 4, 5, 6, 7, 8},
            new byte[] {0, (byte) 255, 0, 0, 16, 32, 64, (byte) 128, 1, 2, 3, 4, 5, 6, 7, 8},
            new byte[] {0, 0, (byte) 255, 0, 16, 32, 64, (byte) 128, 1, 2, 3, 4, 5, 6, 7, 8});
    assertEquals(16, icm.getMapSize());

    // create random data
    WritableRaster data =
        com.sun.media.jai.codecimpl.util.RasterFactory.createWritableRaster(
            icm.createCompatibleSampleModel(32, 32), new Point(0, 0));
    for (int x = data.getMinX(); x < data.getMinX() + data.getWidth(); x++) {
      for (int y = data.getMinY(); y < data.getMinY() + data.getHeight(); y++) {
        data.setSample(x, y, 0, (x + y) % 8);
      }
    }

    final BufferedImage bi = new BufferedImage(icm, data, false, null);
    assertEquals(16, ((IndexColorModel) bi.getColorModel()).getMapSize());
    assertEquals(4, bi.getSampleModel().getSampleSize(0));
    bi.setData(data);
    if (TestData.isInteractiveTest()) {
      ImageIOUtilities.visualize(bi, "before");
    }

    // encode as png
    ImageWorker worker = new ImageWorker(bi);
    final File outFile = TestData.temp(this, "temp4.png");
    worker.writePNG(outFile, "FILTERED", 0.75f, true, false);
    worker.dispose();

    // make sure we can read it
    BufferedImage back = ImageIO.read(outFile);

    // we expect an IndexColorMolde one matching the old one
    IndexColorModel ccm = (IndexColorModel) back.getColorModel();
    assertEquals(3, ccm.getNumColorComponents());
    assertEquals(16, ccm.getMapSize());
    assertEquals(4, ccm.getPixelSize());
    if (TestData.isInteractiveTest()) {
      ImageIOUtilities.visualize(back, "after");
    }
  }
示例#5
0
  public ImageData image(BufferedImage image) {
    if (image.getColorModel() instanceof DirectColorModel) {
      DirectColorModel cmodel = (DirectColorModel) image.getColorModel();
      PaletteData palette =
          new PaletteData(cmodel.getRedMask(), cmodel.getGreenMask(), cmodel.getBlueMask());
      ImageData data =
          new ImageData(image.getWidth(), image.getHeight(), cmodel.getPixelSize(), palette);
      for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
          int rgb = image.getRGB(x, y);
          int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
          data.setPixel(x, y, pixel);
          if (cmodel.hasAlpha()) data.setAlpha(x, y, (rgb >> 24) & 0xFF);
        }
      }
      return data;

    } else if (image.getColorModel() instanceof IndexColorModel) {
      IndexColorModel cmodel = (IndexColorModel) image.getColorModel();
      int size = cmodel.getMapSize();
      byte[] reds = new byte[size];
      byte[] greens = new byte[size];
      byte[] blues = new byte[size];
      cmodel.getReds(reds);
      cmodel.getGreens(greens);
      cmodel.getBlues(blues);
      RGB[] rgbs = new RGB[size];
      for (int ii = 0; ii < rgbs.length; ii++) {
        rgbs[ii] = new RGB(reds[ii] & 0xFF, greens[ii] & 0xFF, blues[ii] & 0xFF);
      }
      PaletteData palette = new PaletteData(rgbs);
      ImageData data =
          new ImageData(image.getWidth(), image.getHeight(), cmodel.getPixelSize(), palette);
      data.transparentPixel = cmodel.getTransparentPixel();
      WritableRaster raster = image.getRaster();
      int[] pixelArray = new int[1];
      for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
          raster.getPixel(x, y, pixelArray);
          data.setPixel(x, y, pixelArray[0]);
        }
      }
      return data;
    } else if (image.getColorModel() instanceof ComponentColorModel) {
      ComponentColorModel cmodel = (ComponentColorModel) image.getColorModel();
      PaletteData palette = new PaletteData(0x0000FF, 0x00FF00, 0xFF0000); // BGR
      ImageData data = new ImageData(image.getWidth(), image.getHeight(), 24, palette);
      if (cmodel.hasAlpha()) data.alphaData = new byte[image.getWidth() * image.getHeight()];
      WritableRaster raster = image.getRaster();
      int[] pixelArray = new int[4];
      for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
          raster.getPixel(x, y, pixelArray);
          data.setPixel(x, y, (pixelArray[2] << 16) | (pixelArray[1] << 8) | (pixelArray[0]));
          if (data.alphaData != null) data.alphaData[y * data.width + x] = (byte) pixelArray[3];
        }
      }
      return data;
    }
    return null;
  }
  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);
    }
  }
示例#7
0
  /*
   * Return the color model to be used with this BufferedImage and
   * transform.
   */
  private ColorModel getTransformColorModel(
      SunGraphics2D sg, BufferedImage bImg, AffineTransform tx) {
    ColorModel cm = bImg.getColorModel();
    ColorModel dstCM = cm;

    if (tx.isIdentity()) {
      return dstCM;
    }
    int type = tx.getType();
    boolean needTrans = ((type & (tx.TYPE_MASK_ROTATION | tx.TYPE_GENERAL_TRANSFORM)) != 0);
    if (!needTrans && type != tx.TYPE_TRANSLATION && type != tx.TYPE_IDENTITY) {
      double[] mtx = new double[4];
      tx.getMatrix(mtx);
      // Check out the matrix.  A non-integral scale will force ARGB
      // since the edge conditions cannot be guaranteed.
      needTrans = (mtx[0] != (int) mtx[0] || mtx[3] != (int) mtx[3]);
    }

    if (sg.renderHint != SunHints.INTVAL_RENDER_QUALITY) {
      if (cm instanceof IndexColorModel) {
        Raster raster = bImg.getRaster();
        IndexColorModel icm = (IndexColorModel) cm;
        // Just need to make sure that we have a transparent pixel
        if (needTrans && cm.getTransparency() == cm.OPAQUE) {
          // Fix 4221407
          if (raster instanceof sun.awt.image.BytePackedRaster) {
            dstCM = ColorModel.getRGBdefault();
          } else {
            double[] matrix = new double[6];
            tx.getMatrix(matrix);
            if (matrix[1] == 0. && matrix[2] == 0. && matrix[4] == 0. && matrix[5] == 0.) {
              // Only scaling so do not need to create
            } else {
              int mapSize = icm.getMapSize();
              if (mapSize < 256) {
                int[] cmap = new int[mapSize + 1];
                icm.getRGBs(cmap);
                cmap[mapSize] = 0x0000;
                dstCM =
                    new IndexColorModel(
                        icm.getPixelSize(),
                        mapSize + 1,
                        cmap,
                        0,
                        true,
                        mapSize,
                        DataBuffer.TYPE_BYTE);
              } else {
                dstCM = ColorModel.getRGBdefault();
              }
            } /* if (matrix[0] < 1.f ...) */
          } /* raster instanceof sun.awt.image.BytePackedRaster */
        } /* if (cm.getTransparency() == cm.OPAQUE) */
      } /* if (cm instanceof IndexColorModel) */ else if (needTrans
          && cm.getTransparency() == cm.OPAQUE) {
        // Need a bitmask transparency
        // REMIND: for now, use full transparency since no loops
        // for bitmask
        dstCM = ColorModel.getRGBdefault();
      }
    } /* if (sg.renderHint == RENDER_QUALITY) */ else {

      if (cm instanceof IndexColorModel || (needTrans && cm.getTransparency() == cm.OPAQUE)) {
        // Need a bitmask transparency
        // REMIND: for now, use full transparency since no loops
        // for bitmask
        dstCM = ColorModel.getRGBdefault();
      }
    }

    return dstCM;
  }
示例#8
0
  public RenderedImage scale(RenderedImage renderedImage, int maxHeight, int maxWidth) {

    int imageHeight = renderedImage.getHeight();
    int imageWidth = renderedImage.getWidth();

    if (maxHeight == 0) {
      maxHeight = imageHeight;
    }

    if (maxWidth == 0) {
      maxWidth = imageWidth;
    }

    if ((imageHeight <= maxHeight) && (imageWidth <= maxWidth)) {
      return renderedImage;
    }

    double factor = Math.min((double) maxHeight / imageHeight, (double) maxWidth / imageWidth);

    int scaledHeight = Math.max(1, (int) (factor * imageHeight));
    int scaledWidth = Math.max(1, (int) (factor * imageWidth));

    BufferedImage bufferedImage = getBufferedImage(renderedImage);

    int type = bufferedImage.getType();

    if (type == 0) {
      type = BufferedImage.TYPE_INT_ARGB;
    }

    BufferedImage scaledBufferedImage = null;

    if ((type == BufferedImage.TYPE_BYTE_BINARY) || (type == BufferedImage.TYPE_BYTE_INDEXED)) {

      IndexColorModel indexColorModel = (IndexColorModel) bufferedImage.getColorModel();

      BufferedImage tempBufferedImage = new BufferedImage(1, 1, type, indexColorModel);

      int bits = indexColorModel.getPixelSize();
      int size = indexColorModel.getMapSize();

      byte[] reds = new byte[size];

      indexColorModel.getReds(reds);

      byte[] greens = new byte[size];

      indexColorModel.getGreens(greens);

      byte[] blues = new byte[size];

      indexColorModel.getBlues(blues);

      WritableRaster writableRaster = tempBufferedImage.getRaster();

      int pixel = writableRaster.getSample(0, 0, 0);

      IndexColorModel scaledIndexColorModel =
          new IndexColorModel(bits, size, reds, greens, blues, pixel);

      scaledBufferedImage =
          new BufferedImage(scaledWidth, scaledHeight, type, scaledIndexColorModel);
    } else {
      scaledBufferedImage = new BufferedImage(scaledWidth, scaledHeight, type);
    }

    Graphics graphics = scaledBufferedImage.getGraphics();

    Image scaledImage =
        bufferedImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);

    graphics.drawImage(scaledImage, 0, 0, null);

    return scaledBufferedImage;
  }