Esempio n. 1
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);
    }
  }
Esempio n. 2
0
  public static ArthurImage add(ArthurImage one, ArthurNumber two) {
    BufferedImage image = JavaImageMath.clone(one.bf);
    int num = two.val.intValue();

    WritableRaster raster = image.getRaster();
    int[] pixelArray = new int[3];
    for (int y = 0; y < raster.getHeight(); y++) {
      for (int x = 0; x < raster.getWidth(); x++) {
        pixelArray = raster.getPixel(x, y, pixelArray);
        pixelArray[0] = pixelArray[0] + num;
        pixelArray[1] = pixelArray[1] + num;
        pixelArray[2] = pixelArray[2] + num;
        for (int i = 0; i < 3; i++) {
          if (pixelArray[i] > 255) {
            pixelArray[i] = 255;
          }
        }
        raster.setPixel(x, y, pixelArray);
      }
    }

    // save image
    String outputFn =
        one.filename.substring(0, one.filename.indexOf(".jpg"))
            + "+"
            + // filename can't contain the / or *characters; decide later
            num
            + counter
            + ".jpg";
    counter++;

    return new ArthurImage(image, outputFn);
  }
Esempio n. 3
0
  public static ArthurImage add(ArthurImage one, ArthurColor two) {
    BufferedImage image = JavaImageMath.clone(one.bf);

    double r = two.r.val;
    double g = two.g.val;
    double b = two.g.val;

    WritableRaster raster = image.getRaster();
    int[] pixelArray = new int[3];
    for (int y = 0; y < raster.getHeight(); y++) {
      for (int x = 0; x < raster.getWidth(); x++) {
        pixelArray = raster.getPixel(x, y, pixelArray);
        pixelArray[0] = (int) (3 * pixelArray[0] + r) / 4;
        pixelArray[1] = (int) (3 * pixelArray[1] + g) / 4;
        pixelArray[2] = (int) (3 * pixelArray[2] + b) / 4;
        raster.setPixel(x, y, pixelArray);
      }
    }

    // save image
    String outputFn =
        one.filename.substring(0, one.filename.indexOf(".jpg"))
            + "+"
            + // filename can't contain the / or *characters; decide later
            r
            + g
            + b
            + counter
            + ".jpg";
    counter++;

    return new ArthurImage(image, outputFn);
  }
Esempio n. 4
0
  public static ArthurImage multiply(
      ArthurImage one, ArthurNumber two) { // change to ArthurNumber later
    double f = Math.abs(two.val);
    // get image
    BufferedImage image = JavaImageMath.clone(one.bf);

    // manipulate image
    WritableRaster raster = image.getRaster();
    int width = (int) (raster.getWidth() * f);
    int height = (int) (raster.getHeight() * f);

    BufferedImage collage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = collage.createGraphics();
    g2d.drawImage(image, 0, 0, width, height, null);
    g2d.dispose();

    // save image
    String outputFn =
        one.filename.substring(0, one.filename.indexOf(".jpg"))
            + "X"
            + // filename can't contain the / or *characters; decide later
            f
            + counter
            + ".jpg";
    counter++;

    return new ArthurImage(collage, outputFn);
  }
Esempio n. 5
0
 public static BufferedImage clone(BufferedImage original) {
   WritableRaster raster = original.getRaster();
   BufferedImage clone =
       new BufferedImage(raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_INT_RGB);
   Graphics2D g2d = clone.createGraphics();
   g2d.drawImage(original, 0, 0, null);
   g2d.dispose();
   return clone;
 }
Esempio n. 6
0
 private static void initImg(BufferedImage img, int pf, int flags) throws Exception {
   WritableRaster wr = img.getRaster();
   int imgType = img.getType();
   if (imgType == BufferedImage.TYPE_INT_RGB
       || imgType == BufferedImage.TYPE_INT_BGR
       || imgType == BufferedImage.TYPE_INT_ARGB
       || imgType == BufferedImage.TYPE_INT_ARGB_PRE) {
     SinglePixelPackedSampleModel sm = (SinglePixelPackedSampleModel) img.getSampleModel();
     int pitch = sm.getScanlineStride();
     DataBufferInt db = (DataBufferInt) wr.getDataBuffer();
     int[] buf = db.getData();
     initIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
   } else {
     ComponentSampleModel sm = (ComponentSampleModel) img.getSampleModel();
     int pitch = sm.getScanlineStride();
     DataBufferByte db = (DataBufferByte) wr.getDataBuffer();
     byte[] buf = db.getData();
     initBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
   }
 }
    private void makeNoDataTransparent(
        BufferedImage image, short missingDataSignal, short missingDataReplacement) {
      WritableRaster raster = null;

      if (null != image
          && (image.getType() == BufferedImage.TYPE_4BYTE_ABGR
              || image.getType() == BufferedImage.TYPE_INT_ARGB)
          && null != (raster = image.getRaster())) {
        int nodata_r = missingDataSignal & 0xff;
        int nodata_g = missingDataSignal & 0xff;
        int nodata_b = missingDataSignal & 0xff;

        int[] pixel = new int[4];
        int[] transparentPixel =
            new int[] {
              0xFF & missingDataReplacement /* red */,
              0xFF & missingDataReplacement /* green */,
              0xFF & missingDataReplacement,
              0x00 /* alpha */
            };

        int width = image.getWidth();
        int height = image.getHeight();
        for (int j = 0; j < height; j++) {
          for (int i = 0; i < width; i++) {
            // We know, by the nature of this source, that we are dealing with RGBA rasters...
            raster.getPixel(i, j, pixel);
            if (pixel[0] == nodata_r && pixel[1] == nodata_g && pixel[2] == nodata_b) {
              //                            pixel[0] = pixel[1] = pixel[2] = 0xFF &
              // missingDataReplacement;
              //                            pixel[3] = 0;   // transparent
              raster.setPixel(i, j, transparentPixel);
            } else {
              pixel[3] = 0xff; // non-transparent
              raster.setPixel(i, j, pixel);
            }
          }
        }
      }
    }
Esempio n. 8
0
  public static ArthurImage divide(
      ArthurImage one, ArthurNumber two) { // change to ArthurNumber later
    int f = Math.abs(two.intval());
    // get image
    BufferedImage image = JavaImageMath.clone(one.bf);

    // manipulate image
    WritableRaster raster = image.getRaster();
    int width0 = raster.getWidth();
    int height0 = raster.getHeight();
    int width = width0 / f;
    int height = height0 / f;

    BufferedImage temp = new BufferedImage(width * f, height * f, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = temp.createGraphics();
    for (int i = 0; i < f; i++) {
      for (int j = 0; j < f; j++) {
        g2d.drawImage(image, j * width, i * height, width, height, null);
      }
    }
    g2d.dispose();
    BufferedImage collage = new BufferedImage(width0, height0, BufferedImage.TYPE_INT_RGB);
    g2d = collage.createGraphics();
    g2d.drawImage(temp, 0, 0, width0, height0, null);
    g2d.dispose();

    // save image
    String outputFn =
        one.filename.substring(0, one.filename.indexOf(".jpg"))
            + "D"
            + // filename can't contain the / or *characters; decide later
            f
            + counter
            + ".jpg";
    counter++;

    return new ArthurImage(collage, outputFn);
  }
Esempio n. 9
0
  public static ArthurImage divide(ArthurImage one, ArthurImage two) {
    BufferedImage image = JavaImageMath.clone(one.bf);
    BufferedImage image2 = JavaImageMath.clone(two.bf);

    WritableRaster r1 = image.getRaster();
    int width = r1.getWidth();
    int height = r1.getHeight();

    BufferedImage r2resize = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = r2resize.createGraphics();
    g2d.drawImage(image2, 0, 0, width, height, null);
    g2d.dispose();
    WritableRaster r2 = r2resize.getRaster();

    BufferedImage collage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = collage.getRaster();

    int[] p1 = new int[3];
    int[] p2 = new int[3];
    int[] pixelArray = new int[3];
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        p1 = r1.getPixel(x, y, p1);
        p2 = r2.getPixel(x, y, p2);

        for (int i = 0; i < 3; i++) {
          pixelArray[i] = (int) ((p1[i] + p2[i]) / 2);
        }
        raster.setPixel(x, y, pixelArray);
      }
    }

    // save image
    String outputFn =
        one.filename.substring(0, one.filename.indexOf(".jpg"))
            + "D"
            + // filename can't contain the / or *characters; decide later
            two.filename.substring(0, two.filename.indexOf(".jpg"))
            + counter
            + ".jpg";
    counter++;

    return new ArthurImage(collage, outputFn);
  }
Esempio n. 10
0
  public static ArthurImage addition(ArthurImage one, ArthurImage two, int op) {
    BufferedImage image = JavaImageMath.clone(one.bf);
    BufferedImage image2 = JavaImageMath.clone(two.bf);

    WritableRaster r1 = image.getRaster();
    WritableRaster r2 = image2.getRaster();
    int newWidth = r1.getWidth() + r2.getWidth();
    int newHeight = Math.max(r1.getHeight(), r2.getHeight());

    BufferedImage collage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = collage.createGraphics();
    // g2d.setBackground(Color.BLACK);
    g2d.drawImage(image, 0, 0, null);
    g2d.drawImage(image2, r1.getWidth(), 0, null);
    g2d.dispose();

    // save image
    String outputFn;
    if (op == 0) {
      outputFn =
          one.filename.substring(0, one.filename.indexOf(".jpg"))
              + "+"
              + two.filename.substring(0, two.filename.indexOf(".jpg"))
              + counter
              + ".jpg";
    } else {
      outputFn =
          two.filename.substring(0, two.filename.indexOf(".jpg"))
              + "-"
              + one.filename.substring(0, one.filename.indexOf(".jpg"))
              + counter
              + ".jpg";
    }

    counter++;

    return new ArthurImage(collage, outputFn);
  }
Esempio n. 11
0
  public static ArthurImage multiply(ArthurImage one, ArthurImage two) {
    BufferedImage image = JavaImageMath.clone(one.bf);
    BufferedImage image2 = JavaImageMath.clone(two.bf);

    WritableRaster r1 = image.getRaster();
    WritableRaster r2 = image2.getRaster();
    int newWidth = Math.max(r1.getWidth(), r2.getWidth());
    int newHeight = Math.max(r1.getHeight(), r2.getHeight());

    BufferedImage collage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = collage.getRaster();

    int[] p1 = new int[3];
    int[] p2 = new int[3];
    int[] pixelArray = new int[3];
    for (int y = 0; y < newHeight; y++) {
      for (int x = 0; x < newWidth; x++) {
        p1 = null;
        p2 = null;

        if (x < r1.getWidth() && y < r1.getHeight()) {
          p1 = r1.getPixel(x, y, p1);
        }

        if (x < r2.getWidth() && y < r2.getHeight()) {
          p2 = r2.getPixel(x, y, p2);
        }

        for (int i = 0; i < 3; i++) {
          if (p1 == null && p2 == null) {
            pixelArray[i] = 0;
          } else if (p1 == null && p2 != null) {
            pixelArray[i] = p2[i];
          } else if (p1 != null && p2 == null) {
            pixelArray[i] = p1[i];
          } else {
            pixelArray[i] = (int) ((p1[i] + p2[i]) / 2);
          }
        }
        raster.setPixel(x, y, pixelArray);
      }
    }

    // save image
    String outputFn =
        one.filename.substring(0, one.filename.indexOf(".jpg"))
            + "X"
            + // filename can't contain the / or *characters; decide later
            two.filename.substring(0, two.filename.indexOf(".jpg"))
            + counter
            + ".jpg";
    counter++;

    return new ArthurImage(collage, outputFn);
  }
Esempio n. 12
0
  private IFD writeYCbCrImage(
      ImageOutputStream out, BufferedImage image, int comp, TIFFImageWriteParam param)
      throws IOException {
    image = convert(image, BufferedImage.TYPE_INT_RGB);
    try {
      int width = image.getWidth();
      int height = image.getHeight();

      IFD ifd = new IFD(); // entries need to be in tag order !

      int ss = (param == null) ? 0x22 : param.getSubSampling();

      int ssh = (ss >> 4) & 0x0F;
      int ssv = ss & 0x0F;

      if (ssh
          < ssv) { // YCbCrSubsampleVert shall always be less than or equal to YCbCrSubsampleHoriz.
        throw new IOException(
            "Internal error: YCbCrSubsampleVert is not less than YCbCrSubsampleHoriz.");
      }

      //      int ww=((width +ssh-1)/ssh)*ssh;                                   // [1] p.92
      //      int hh=((height+ssv-1)/ssv)*ssv;
      int ww = width;
      int hh = height;

      ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file
      ifd.add(new DEFactory.ImageWidthDE(ww)); // 256
      ifd.add(new DEFactory.ImageLengthDE(hh)); // 257

      DEFactory.BitsPerSampleDE bpss = new DEFactory.BitsPerSampleDE(3);
      bpss.setBitsPerSample(0, 8); // Y
      bpss.setBitsPerSample(1, 8); // Cb
      bpss.setBitsPerSample(2, 8); // Cr
      ifd.add(bpss); // 258

      ifd.add(new DEFactory.CompressionDE(comp)); // 259
      ifd.add(new DEFactory.PhotometricInterpretationDE(YCbCr)); // 262

      int maxrps, maxstripes; // max RowsPerStrip
      if ((1 << 13) <= width) {
        maxrps = 1;
        maxstripes = height; // one row per strip
      } else {
        maxrps = (1 << 13) / width;
        maxstripes = (height + maxrps - 1) / maxrps;
      }
      if (comp == JPEG) {
        maxrps = ((maxrps + 8 * ssv - 1) / (8 * ssv)) * (8 * ssv);
        maxstripes = (height + maxrps - 1) / maxrps;
      }

      DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes);
      ifd.add(offsets); // 273
      ifd.add(new DEFactory.SamplesPerPixelDE(3)); // 277
      ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278
      DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes);
      ifd.add(counts); // 279

      if (param == null) {
        ifd.add(new DEFactory.XResolutionDE(72.0)); // 282
        ifd.add(new DEFactory.YResolutionDE(72.0)); // 283
      } else {
        ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282
        ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283
      }
      ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      OutputStream os = baos;
      JPEGOutputStream jpegos = null;

      if (comp == JPEG) {
        jpegos = new JPEGOutputStream(baos);

        int quality = (param == null) ? 50 : (int) (param.getCompressionQuality() * 100);

        jpegos.setZZQuantizationTable(0, JPEGConstants.LQT, quality);
        jpegos.setZZQuantizationTable(1, JPEGConstants.CQT, quality);

        jpegos.setRawDCHuffmanTable(0, JPEGConstants.HLDCTable);
        jpegos.setRawACHuffmanTable(0, JPEGConstants.HLACTable);
        jpegos.setRawDCHuffmanTable(1, JPEGConstants.HCDCTable);
        jpegos.setRawACHuffmanTable(1, JPEGConstants.HCACTable);

        jpegos.defineQuantizationTables();
        jpegos.defineHuffmanTables();
        jpegos.close();

        DEFactory.JPEGTablesDE jpegtables = new DEFactory.JPEGTablesDE(baos.toByteArray());
        ifd.add(jpegtables); // 347

        baos.reset();

        os = jpegos;
      }

      //      CCIR Recommendation 601-1  LumaRed=299/1000 LumaGreen=587/1000 LumeBlue=114/1000
      //      Y  = ( LumaRed * R + LumaGreen * G + LumaBlue * B )
      //      Cb = ( B - Y ) / ( 2 - 2 * LumaBlue )
      //      Cr = ( R - Y ) / ( 2 - 2 * LumaRed )

      double LumaRed = 299.0 / 1000.0;
      double LumaGreen = 587.0 / 1000.0;
      double LumaBlue = 114.0 / 1000.0;

      DEFactory.YCbCrCoefficientsDE YCbCrCoeff = new DEFactory.YCbCrCoefficientsDE();
      YCbCrCoeff.setLumaRed(LumaRed); // Y
      YCbCrCoeff.setLumaGreen(LumaGreen); // Cb
      YCbCrCoeff.setLumaBlue(LumaBlue); // Cr
      ifd.add(YCbCrCoeff); // 529

      DEFactory.YCbCrSubSamplingDE YCbCrSubSampling = new DEFactory.YCbCrSubSamplingDE();
      YCbCrSubSampling.setHoriz(ssh);
      YCbCrSubSampling.setVert(ssv);
      ifd.add(YCbCrSubSampling); // 530

      double RfBY = 0;
      double RfWY = 255;
      double RfBCb = 128;
      double RfWCb = 255;
      double RfBCr = 128;
      double RfWCr = 255;

      DEFactory.ReferenceBlackWhiteDE ReferenceBlackWhite = new DEFactory.ReferenceBlackWhiteDE();
      ReferenceBlackWhite.setY(RfBY, RfWY);
      ReferenceBlackWhite.setCb(RfBCb, RfWCb);
      ReferenceBlackWhite.setCr(RfBCr, RfWCr);
      ifd.add(ReferenceBlackWhite); // 532

      TIFFYCbCrOutputStream ycbcros;
      if (jpegos == null) {
        ycbcros = new TIFFYCbCrOutputStream(os, width, ssv, ssh);
        os = new TIFFSubSamplingOutputStream(ycbcros, width, ssv, ssh);
      } else {
        ycbcros = new TIFFYCbCrOutputStream(os, width, 1, 1); // jpeg does own subsampling
        os = ycbcros;
      }

      ycbcros.setPositioning(1);
      ycbcros.setColourCoefficients(LumaRed, LumaGreen, LumaBlue);
      ycbcros.setRfBWY(RfBY, RfWY);
      ycbcros.setRfBWCb(RfBCb, RfWCb);
      ycbcros.setRfBWCr(RfBCr, RfWCr);

      WritableRaster raster = image.getRaster();
      DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
      int[] imgdata = (int[]) buffer.getData();

      int c = 0, index = 0;
      for (int y = 0; y < height; y += maxrps) {

        if ((height - y) < maxrps) {
          maxrps = height - y;
        }

        if (jpegos != null) {
          jpegos.startOfImage();
          int[] hv = {(ssh << 4) | ssv, 0x11, 0x11}; // (Hi<<4)|Vi
          int[] q = {0, 1, 1}; // quantization table Y=0, Cb=Cr=1
          //          jpegos.startOfFrame(((maxrps+ssv-1)/ssv)*ssv,ww,hv,q);
          jpegos.startOfFrame(maxrps, ww, hv, q);
          int[] sel = {0, 1, 1}; // DC,AC code table Y=0, Cb=Cr=1
          jpegos.startOfScan(sel);
        }

        for (int i = 0; i < maxrps; i++) {
          int x = 0;
          while (x < width) {
            c = imgdata[x + (y + i) * width];
            //            c = image.getRGB(x,y+i);
            os.write((c >> 16) & 0x000000FF);
            os.write((c >> 8) & 0x000000FF);
            os.write(c & 0x000000FF);
            x++;
          }
          while (x < ww) {
            os.write((c >> 16) & 0x000000FF);
            os.write((c >> 8) & 0x000000FF);
            os.write(c & 0x000000FF);
            x++;
          }
        }
        os.close();

        byte[] data = baos.toByteArray();
        counts.setCount(index, data.length); // update ifd strip counter array
        offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array
        out.write(data); // write to image stream
        baos.reset();
        index++;
      }
      return ifd;
    } catch (Exception e) {
      e.printStackTrace();
      throw new IOException(getClass().getName() + ".writeYCbCrImage:\n\t" + e.getMessage());
    }
  }
Esempio n. 13
0
  private IFD writeRGBImage(
      ImageOutputStream out, BufferedImage image, int comp, TIFFImageWriteParam param)
      throws IOException {
    image = convert(image, BufferedImage.TYPE_INT_RGB);
    try {
      int width = image.getWidth();
      int height = image.getHeight();

      IFD ifd = new IFD(); // entries need to be in tag order !

      ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file
      ifd.add(new DEFactory.ImageWidthDE(width)); // 256
      ifd.add(new DEFactory.ImageLengthDE(height)); // 257

      DEFactory.BitsPerSampleDE bpss = new DEFactory.BitsPerSampleDE(3);
      bpss.setBitsPerSample(0, 8); // red
      bpss.setBitsPerSample(1, 8); // green
      bpss.setBitsPerSample(2, 8); // blue
      ifd.add(bpss); // 258

      ifd.add(new DEFactory.CompressionDE(comp)); // 259
      ifd.add(new DEFactory.PhotometricInterpretationDE(RGB)); // 262

      int maxrps, maxstripes; // max RowsPerStrip
      if ((1 << 13) <= width) {
        maxrps = 1;
        maxstripes = height; // one row per strip
      } else {
        maxrps = (1 << 13) / width;
        maxstripes = (height + maxrps - 1) / maxrps;
      }
      if (comp == JPEG) {
        maxrps = ((maxrps + 8 - 1) / 8) * 8;
        maxstripes = (height + maxrps - 1) / maxrps;
      }
      DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes);
      ifd.add(offsets); // 273
      ifd.add(new DEFactory.SamplesPerPixelDE(3)); // 277
      ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278
      DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes);
      ifd.add(counts); // 279
      if (param == null) {
        ifd.add(new DEFactory.XResolutionDE(72.0)); // 282
        ifd.add(new DEFactory.YResolutionDE(72.0)); // 283
      } else {
        ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282
        ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283
      }
      ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      OutputStream os = baos;
      JPEGOutputStream jpegos = null;

      if (comp == JPEG) { // add JPEGTables tag
        jpegos = new JPEGOutputStream(baos);

        int quality = (param == null) ? 50 : (int) (param.getCompressionQuality() * 100);

        jpegos.setZZQuantizationTable(0, JPEGConstants.LQT, quality);
        jpegos.setRawDCHuffmanTable(0, JPEGConstants.HLDCTable);
        jpegos.setRawACHuffmanTable(0, JPEGConstants.HLACTable);

        jpegos.defineQuantizationTables();
        jpegos.defineHuffmanTables();
        jpegos.close();

        DEFactory.JPEGTablesDE jpegtables = new DEFactory.JPEGTablesDE(baos.toByteArray());
        ifd.add(jpegtables); // 347

        baos.reset();

        os = jpegos;
      }

      WritableRaster raster = image.getRaster();
      DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
      int[] imgdata = (int[]) buffer.getData();

      int index = 0;
      for (int y = 0; y < height; y += maxrps) {
        /*
        Assume rgb image.

        Each strip: evaluate r g b colour
                    save in byte array
                    write to image file
        */

        if ((height - y) < maxrps) {
          maxrps = height - y;
        }

        if (jpegos != null) { // jpeg: SOI,SOF,SOS marker
          jpegos.startOfImage();
          int[] hv = {0x11, 0x11, 0x11}; // (Hi<<4)|Vi
          int[] q = {0, 0, 0}; // quantization table 0
          jpegos.startOfFrame(maxrps, width, hv, q);
          int[] sel = {0, 0, 0}; // DC,AC code table 0
          jpegos.startOfScan(sel);
        }

        for (int i = 0; i < maxrps; i++) { // write RGB data
          for (int x = 0; x < width; x++) {
            int c = imgdata[x + (y + i) * width];
            os.write((c >> 16) & 0x000000FF);
            os.write((c >> 8) & 0x000000FF);
            os.write(c & 0x000000FF);
          }
        }
        os.close(); // jpeg: EOI marker

        byte[] data = baos.toByteArray();
        counts.setCount(index, data.length); // update ifd strip counter array
        offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array
        out.write(data); // write to image stream
        baos.reset();
        index++;
      }
      return ifd;
    } catch (Exception e) {
      e.printStackTrace();
      throw new IOException(getClass().getName() + ".writeRGBImage:\n\t" + e.getMessage());
    }
  }
Esempio n. 14
0
  public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    checkIndex(imageIndex);
    readHeader();

    if (iis == null) throw new IllegalStateException("input is null");

    BufferedImage img;

    clearAbortRequest();
    processImageStarted(imageIndex);

    if (param == null) param = getDefaultReadParam();

    sourceRegion = new Rectangle(0, 0, 0, 0);
    destinationRegion = new Rectangle(0, 0, 0, 0);

    computeRegions(
        param, this.width, this.height, param.getDestination(), sourceRegion, destinationRegion);

    scaleX = param.getSourceXSubsampling();
    scaleY = param.getSourceYSubsampling();

    // If the destination band is set used it
    sourceBands = param.getSourceBands();
    destBands = param.getDestinationBands();

    seleBand = (sourceBands != null) && (destBands != null);
    noTransform = destinationRegion.equals(new Rectangle(0, 0, width, height)) || seleBand;

    if (!seleBand) {
      sourceBands = new int[colorPlanes];
      destBands = new int[colorPlanes];
      for (int i = 0; i < colorPlanes; i++) destBands[i] = sourceBands[i] = i;
    }

    // If the destination is provided, then use it.  Otherwise, create new one
    bi = param.getDestination();

    // Get the image data.
    WritableRaster raster = null;

    if (bi == null) {
      if (sampleModel != null && colorModel != null) {
        sampleModel =
            sampleModel.createCompatibleSampleModel(
                destinationRegion.width + destinationRegion.x,
                destinationRegion.height + destinationRegion.y);
        if (seleBand) sampleModel = sampleModel.createSubsetSampleModel(sourceBands);
        raster = Raster.createWritableRaster(sampleModel, new Point(0, 0));
        bi = new BufferedImage(colorModel, raster, false, null);
      }
    } else {
      raster = bi.getWritableTile(0, 0);
      sampleModel = bi.getSampleModel();
      colorModel = bi.getColorModel();

      noTransform &= destinationRegion.equals(raster.getBounds());
    }

    byte bdata[] = null; // buffer for byte data

    if (sampleModel.getDataType() == DataBuffer.TYPE_BYTE)
      bdata = (byte[]) ((DataBufferByte) raster.getDataBuffer()).getData();

    readImage(bdata);

    if (abortRequested()) processReadAborted();
    else processImageComplete();

    return bi;
  }
Esempio n. 15
0
 public static BufferedImage getImageFromArray(int[] pixels, int width, int height) {
   BufferedImage imageBI = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
   WritableRaster raster = (WritableRaster) imageBI.getData();
   raster.setPixels(0, 0, width, height, pixels);
   return imageBI;
 }