private IFD writeCMYKImage(ImageOutputStream out, BufferedImage image, TIFFImageWriteParam param) throws IOException { 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(4); bpss.setBitsPerSample(0, 8); // cyan bpss.setBitsPerSample(1, 8); // magneta bpss.setBitsPerSample(2, 8); // yellow bpss.setBitsPerSample(3, 8); // key (black) ifd.add(bpss); // 258 ifd.add(new DEFactory.CompressionDE(NOCOMPRESSION)); // 259 ifd.add(new DEFactory.PhotometricInterpretationDE(CMYK)); // 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; } DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes); ifd.add(offsets); // 273 ifd.add(new DEFactory.SamplesPerPixelDE(4)); // 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 int index = 0; for (int y = 0; y < height; y += maxrps) { /* Assume rgb image. Each strip: evaluate c m y k colour save in byte array write to image file */ ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (int i = 0; i < maxrps; i++) { if ((y + i) == height) { break; } // last strip might have less rows for (int x = 0; x < width; x++) { int c = image.getRGB(x, y + i); int R = (c >> 16) & 0x00FF; int G = (c >> 8) & 0x00FF; int B = (c) & 0x00FF; if ((R == 255) && (G == 255) && (B == 255)) { baos.write(0); baos.write(0); baos.write(0); baos.write(0); } else { double C = 1.0 - R / 255.0; double M = 1.0 - G / 255.0; double Y = 1.0 - B / 255.0; double K = C; if (M < K) { K = M; } if (Y < K) { K = Y; } C = ((C - K) / (1.0 - K)) * 255.0; M = ((M - K) / (1.0 - K)) * 255.0; Y = ((Y - K) / (1.0 - K)) * 255.0; K *= 255.0; baos.write((int) C); baos.write((int) M); baos.write((int) Y); baos.write((int) K); } } } baos.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 index++; } return ifd; } catch (Exception e) { e.printStackTrace(); throw new IOException(getClass().getName() + ".writeCMYKImage:\n\t" + e.getMessage()); } }
private IFD writeBModHufImage( ImageOutputStream out, BufferedImage image, TIFFImageWriteParam param) throws IOException { 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 ifd.add(new DEFactory.CompressionDE(CCITTGROUP3MODHUFFMAN)); // 259 ifd.add(new DEFactory.PhotometricInterpretationDE(WhiteIsZero)); // 262 int maxrps, maxstripes; // max RowsPerStrip if ((1 << 13) <= width) { maxrps = 1; maxstripes = height; // one row per stripe } else { maxrps = (1 << 13) / width; maxstripes = (height + maxrps - 1) / maxrps; } DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes); ifd.add(offsets); // 273 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 int index = 0; for (int y = 0; y < height; y += maxrps) { /* Assume bilevel image (black/white[=-1]) Each strip: count run length encode into modified hufman codes swap bits save in byte array write to image file */ ByteArrayOutputStream baos = new ByteArrayOutputStream(); BitSwapOutputStream bsos = new BitSwapOutputStream(baos); ModHuffmanOutputStream mhos = new ModHuffmanOutputStream(bsos); RLEOutputStream rlos = new RLEOutputStream(mhos, 3); // rgb = 3 bytes per sample code word (not needed here) for (int i = 0; i < maxrps; i++) { if ((y + i) == height) { break; } // last strip might have less rows rlos.setStartCodeWord(-1); // white run first for (int x = 0; x < width; x++) { rlos.write(image.getRGB(x, y + i)); } rlos.flush(); // write padding after ever image row } rlos.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 index++; } return ifd; } catch (Exception e) { e.printStackTrace(); throw new IOException(getClass().getName() + ".writeBModHufImage:\n\t" + e.getMessage()); } }