Example #1
0
  /**
   * Method to change bands order only on the BufferedImage.
   *
   * @param bufferedImage
   * @return new bufferedImage
   */
  public Image invertRGB(BufferedImage bufferedImage, String bands) {

    ColorModel colorModel = bufferedImage.getColorModel();

    if (colorModel instanceof DirectColorModel) {
      DirectColorModel directColorModel = (DirectColorModel) colorModel;
      int red = directColorModel.getRedMask();
      int blue = directColorModel.getBlueMask();
      int green = directColorModel.getGreenMask();
      int alpha = directColorModel.getAlphaMask();

      int[] components = new int[3];
      String bds = bands.toLowerCase();
      components[0] = getComponent(bds.charAt(0), red, green, blue);
      components[1] = getComponent(bds.charAt(1), red, green, blue);
      components[2] = getComponent(bds.charAt(2), red, green, blue);

      directColorModel =
          new DirectColorModel(32, components[0], components[1], components[2], alpha);
      ColorProcessor colorProcessor = new ColorProcessor(bufferedImage);
      colorProcessor.setColorModel(directColorModel);

      return colorProcessor.createImage();
    }
    return bufferedImage;
  }
 private GDIWindowSurfaceData(WComponentPeer peer, SurfaceType sType) {
   super(sType, peer.getDeviceColorModel());
   ColorModel cm = peer.getDeviceColorModel();
   this.peer = peer;
   int rMask = 0, gMask = 0, bMask = 0;
   int depth;
   switch (cm.getPixelSize()) {
     case 32:
     case 24:
       if (cm instanceof DirectColorModel) {
         depth = 32;
       } else {
         depth = 24;
       }
       break;
     default:
       depth = cm.getPixelSize();
   }
   if (cm instanceof DirectColorModel) {
     DirectColorModel dcm = (DirectColorModel) cm;
     rMask = dcm.getRedMask();
     gMask = dcm.getGreenMask();
     bMask = dcm.getBlueMask();
   }
   this.graphicsConfig = (Win32GraphicsConfig) peer.getGraphicsConfiguration();
   this.solidloops = graphicsConfig.getSolidLoops(sType);
   Win32GraphicsDevice gd = graphicsConfig.getDevice();
   scaleX = gd.getDefaultScaleX();
   scaleY = gd.getDefaultScaleY();
   initOps(peer, depth, rMask, gMask, bMask, gd.getScreen());
   setBlitProxyKey(graphicsConfig.getProxyKey());
 }
Example #3
0
  /**
   * Save onscreen image to file - suffix must be png, jpg, or gif.
   *
   * @param filename the name of the file with one of the required suffixes
   */
  public static void save(String filename) {
    File file = new File(filename);
    String suffix = filename.substring(filename.lastIndexOf('.') + 1);

    // png files
    if (suffix.toLowerCase().equals("png")) {
      try {
        ImageIO.write(onscreenImage, suffix, file);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // need to change from ARGB to RGB for jpeg
    // reference: http://archives.java.sun.com/cgi-bin/wa?A2=ind0404&L=java2d-interest&D=0&P=2727
    else if (suffix.toLowerCase().equals("jpg")) {
      WritableRaster raster = onscreenImage.getRaster();
      WritableRaster newRaster;
      newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, new int[] {0, 1, 2});
      DirectColorModel cm = (DirectColorModel) onscreenImage.getColorModel();
      DirectColorModel newCM =
          new DirectColorModel(
              cm.getPixelSize(), cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask());
      BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null);
      try {
        ImageIO.write(rgbBuffer, suffix, file);
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      System.out.println("Invalid image file type: " + suffix);
    }
  }
  void createBufferedImage() {
    // REMIND:  Be careful!  Is this called everytime there is a
    // startProduction?  We only want to call it if it is new or
    // there is an error
    isDefaultBI = false;
    try {
      biRaster = cmodel.createCompatibleWritableRaster(width, height);
      bimage = createImage(cmodel, biRaster, cmodel.isAlphaPremultiplied(), null);
    } catch (Exception e) {
      // Create a default image
      cmodel = ColorModel.getRGBdefault();
      biRaster = cmodel.createCompatibleWritableRaster(width, height);
      bimage = createImage(cmodel, biRaster, false, null);
    }
    int type = bimage.getType();

    if ((cmodel == ColorModel.getRGBdefault())
        || (type == BufferedImage.TYPE_INT_RGB)
        || (type == BufferedImage.TYPE_INT_ARGB_PRE)) {
      isDefaultBI = true;
    } else if (cmodel instanceof DirectColorModel) {
      DirectColorModel dcm = (DirectColorModel) cmodel;
      if (dcm.getRedMask() == 0xff0000
          && dcm.getGreenMask() == 0xff00
          && dcm.getBlueMask() == 0xff) {
        isDefaultBI = true;
      }
    }
  }
 private void writeDirectColorModel(DataOutput out, DirectColorModel colorModel)
     throws IOException {
   out.writeInt(colorModel.getPixelSize());
   out.writeInt(colorModel.getRedMask());
   out.writeInt(colorModel.getGreenMask());
   out.writeInt(colorModel.getBlueMask());
   out.writeInt(colorModel.getAlphaMask());
 }
 public static boolean isFilterableDCM(ColorModel cm) {
   if (cm instanceof DirectColorModel) {
     DirectColorModel dcm = (DirectColorModel) cm;
     return (isMaskOK(dcm.getAlphaMask(), true)
         && isMaskOK(dcm.getRedMask(), false)
         && isMaskOK(dcm.getGreenMask(), false)
         && isMaskOK(dcm.getBlueMask(), false));
   }
   return false;
 }
Example #7
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;
 }
  private static boolean isBgrColorModel(ColorModel cm) {
    if (cm instanceof DirectColorModel && cm.getTransferType() == DataBuffer.TYPE_INT) {
      DirectColorModel directCM = (DirectColorModel) cm;

      return directCM.getRedMask() == 0x000000FF
          && directCM.getGreenMask() == 0x0000FF00
          && directCM.getBlueMask() == 0x00FF0000
          && (directCM.getNumComponents() == 3 || directCM.getAlphaMask() == 0xFF000000);
    }

    return false;
  }
  public void setColorModel(ColorModel model) {
    if (src != null) {
      src.checkSecurity(null, false);
    }
    srcModel = model;

    // Check to see if model is INT_RGB
    if (model instanceof IndexColorModel) {
      if (model.getTransparency() == model.TRANSLUCENT) {
        // REMIND:
        // Probably need to composite anyway so force ARGB
        cmodel = ColorModel.getRGBdefault();
        srcLUT = null;
      } else {
        IndexColorModel icm = (IndexColorModel) model;
        numSrcLUT = icm.getMapSize();
        srcLUT = new int[Math.max(numSrcLUT, 256)];
        icm.getRGBs(srcLUT);
        srcLUTtransIndex = icm.getTransparentPixel();
        cmodel = model;
      }
    } else {
      if (cmodel == null) {
        cmodel = model;
        srcLUT = null;
      } else if (model instanceof DirectColorModel) {
        // If it is INT_RGB or INT_ARGB, use the model
        DirectColorModel dcm = (DirectColorModel) model;
        if ((dcm.getRedMask() == 0xff0000)
            && (dcm.getGreenMask() == 0xff00)
            && (dcm.getBlueMask() == 0x00ff)) {
          cmodel = model;
          srcLUT = null;
        }
      }
    }

    isSameCM = (cmodel == model);
  }
 public ImageData getImageData(BufferedImage bufferedImage) {
   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;
 }
Example #11
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;
  }