/** Generate a tiled image that contains a view of the mask. */ @SuppressWarnings("restriction") public TiledImage getMaskImage() { TiledImage ti = new TiledImage( 0, 0, mwidth, mheight, 0, 0, new PixelInterleavedSampleModel( DataBuffer.TYPE_BYTE, mwidth, mheight, 1, mwidth, Transform.bandstride), new ComponentColorModel( ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE)); WritableRaster gradRaster = ti.getWritableTile(0, 0); DataBufferByte gradDB = (DataBufferByte) gradRaster.getDataBuffer(); byte[] gradImageData = gradDB.getData(); int maskwh = mwidth * mheight; for (int i = 0; i < maskwh; i++) gradImageData[i] = (byte) mask[i]; return ti; }
private IFD writeGrayImage( ImageOutputStream out, BufferedImage image, int comp, TIFFImageWriteParam param) throws IOException { image = convert(image, BufferedImage.TYPE_BYTE_GRAY); 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(1); bpss.setBitsPerSample(0, 8); // gray scale ifd.add(bpss); // 258 ifd.add(new DEFactory.CompressionDE(comp)); // 259 ifd.add(new DEFactory.PhotometricInterpretationDE(BlackIsZero)); // 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(1)); // 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(); DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer(); byte[] imgdata = (byte[]) buffer.getData(); int index = 0; for (int y = 0; y < height; y += maxrps) { /* Assume rgb image. Each strip: evaluate gray scale 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}; // (Hi<<4)|Vi int[] q = {0}; // quantization table 0 jpegos.startOfFrame(maxrps, width, hv, q); int[] sel = {0}; // DC,AC code table 0 jpegos.startOfScan(sel); } os.write(imgdata, y * width, maxrps * width); os.close(); // jpeg EOF: end of frame 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()); } }