static byte[] readStream(InputStream input) throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); int read; byte[] buffer = new byte[256]; while ((read = input.read(buffer, 0, 256)) != -1) { bytes.write(buffer, 0, read); } return bytes.toByteArray(); }
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()); } }