// Flush the packet to disk, and reset the accumulator void flush_char(OutputStream outs) throws IOException { if (a_count > 0) { outs.write(a_count); outs.write(accum, 0, a_count); a_count = 0; } }
public void WriteBits(int bits, int numbits) throws IOException { int bitsWritten = 0; int numBytes = 255; do { if ((index_ == 254 && bitsLeft_ == 0) || index_ > 254) { output_.write(numBytes); output_.write(buffer_, 0, numBytes); buffer_[0] = 0; index_ = 0; bitsLeft_ = 8; } if (numbits <= bitsLeft_) { buffer_[index_] |= (bits & ((1 << numbits) - 1)) << (8 - bitsLeft_); bitsWritten += numbits; bitsLeft_ -= numbits; numbits = 0; } else { buffer_[index_] |= (bits & ((1 << bitsLeft_) - 1)) << (8 - bitsLeft_); bitsWritten += bitsLeft_; bits >>= bitsLeft_; numbits -= bitsLeft_; buffer_[++index_] = 0; bitsLeft_ = 8; } } while (numbits != 0); }
/** Writes Graphic Control Extension */ protected void writeGraphicCtrlExt() throws IOException { out.write(0x21); // extension introducer out.write(0xf9); // GCE label out.write(4); // data block size int transp, disp; if (transparent == null) { transp = 0; disp = 0; // dispose = no action } else { transp = 1; disp = 2; // force clear if using transparent color } if (dispose >= 0) { disp = dispose & 7; // user override } disp <<= 2; // packed fields out.write( 0 | // 1:3 reserved disp | // 4:6 disposal 0 | // 7 user input - 0 = none transp); // 8 transparency flag writeShort(delay); // delay x 1/100 sec out.write(transIndex); // transparent color index out.write(0); // block terminator }
public void Write(OutputStream output) throws IOException { BitUtils.WriteWord(output, localScreenWidth_); BitUtils.WriteWord(output, localScreenHeight_); output.write(byte_); output.write(backgroundColorIndex_); output.write(pixelAspectRatio_); }
/** Writes color table */ protected void writePalette() throws IOException { out.write(colorTab, 0, colorTab.length); int n = (3 * 256) - colorTab.length; for (int i = 0; i < n; i++) { out.write(0); } }
public void Write(OutputStream output) throws IOException { output.write(separator_); BitUtils.WriteWord(output, leftPosition_); BitUtils.WriteWord(output, topPosition_); BitUtils.WriteWord(output, width_); BitUtils.WriteWord(output, height_); output.write(byte_); }
/** Writes Netscape application extension to define repeat count. */ protected void writeNetscapeExt() throws IOException { out.write(0x21); // extension introducer out.write(0xff); // app extension label out.write(11); // block size writeString("NETSCAPE" + "2.0"); // app id + auth code out.write(3); // sub-block size out.write(1); // loop sub-block id writeShort(repeat); // loop count (extra iterations, 0=repeat forever) out.write(0); // block terminator }
// ---------------------------------------------------------------------------- void encode(OutputStream os) throws IOException { os.write(initCodeSize); // write "initial code size" byte remaining = imgW * imgH; // reset navigation variables curPixel = 0; compress(initCodeSize + 1, os); // compress and write the pixel data os.write(0); // write block terminator }
public void Flush() throws IOException { int numBytes = index_ + (bitsLeft_ == 8 ? 0 : 1); if (numBytes > 0) { output_.write(numBytes); output_.write(buffer_, 0, numBytes); buffer_[0] = 0; index_ = 0; bitsLeft_ = 8; } }
/** Writes Logical Screen Descriptor */ protected void writeLSD() throws IOException { // logical screen size writeShort(width); writeShort(height); // packed fields out.write( (0x80 | // 1 : global color table flag = 1 (gct used) 0x70 | // 2-4 : color resolution = 7 0x00 | // 5 : gct sort flag = 0 palSize)); // 6-8 : gct size out.write(0); // background color index out.write(0); // pixel aspect ratio - assume 1:1 }
/** * Flushes any pending data and closes output file. If writing to an OutputStream, the stream is * not closed. */ public boolean finish() { if (!started) return false; boolean ok = true; started = false; try { out.write(0x3b); // gif trailer out.flush(); if (closeStream) { out.close(); } } catch (IOException e) { ok = false; } // reset for subsequent use transIndex = 0; out = null; image = null; pixels = null; indexedPixels = null; colorTab = null; closeStream = false; firstFrame = true; return ok; }
/** * DOCUMENT ME! * * @param out DOCUMENT ME! */ void flushBuffer(OutputStream out) throws IOException { int PutBuffer = bufferPutBuffer; int PutBits = bufferPutBits; while (PutBits >= 8) { int c = ((PutBuffer >> 16) & 0xFF); out.write(c); if (c == 0xFF) out.write(0); PutBuffer <<= 8; PutBits -= 8; } if (PutBits > 0) { int c = ((PutBuffer >> 16) & 0xFF); out.write(c); } }
/** Writes Image Descriptor */ protected void writeImageDesc() throws IOException { out.write(0x2c); // image separator writeShort(0); // image position x,y = 0,0 writeShort(0); writeShort(width); // image size writeShort(height); // packed fields if (firstFrame) { // no LCT - GCT is used for first (or only) frame out.write(0); } else { // specify normal LCT out.write( 0x80 | // 1 local color table 1=yes 0 | // 2 interlace - 0=no 0 | // 3 sorted - 0=no 0 | // 4-5 reserved palSize); // 6-8 size of color table } }
/** * Writes the image out to a stream in the GIF file format. This will be a single GIF87a image, * non-interlaced, with no background color. <B>This may take some time.</B> * * <p> * * @param output The stream to output to. This should probably be a buffered stream. * @exception IOException Will be thrown if a write operation fails. */ public void Write(OutputStream output) throws IOException { BitUtils.WriteString(output, "GIF87a"); ScreenDescriptor sd = new ScreenDescriptor(width_, height_, numColors_); sd.Write(output); output.write(colors_, 0, colors_.length); ImageDescriptor id = new ImageDescriptor(width_, height_, ','); id.Write(output); byte codesize = BitUtils.BitsNeeded(numColors_); if (codesize == 1) ++codesize; output.write(codesize); LZWCompressor.LZWCompress(output, codesize, pixels_); output.write(0); id = new ImageDescriptor((byte) 0, (byte) 0, ';'); id.Write(output); output.flush(); }
// Uses an integer long (32 bits) buffer to store the Huffman encoded bits // and sends them to out by the byte. void bufferIt(OutputStream out, int code, int size) throws IOException { int PutBuffer = code; int PutBits = bufferPutBits; PutBuffer &= ((1 << size) - 1); PutBits += size; PutBuffer <<= (24 - PutBits); PutBuffer |= bufferPutBuffer; while (PutBits >= 8) { int c = ((PutBuffer >> 16) & 0xFF); out.write(c); if (c == 0xFF) out.write(0); PutBuffer <<= 8; PutBits -= 8; } bufferPutBuffer = PutBuffer; bufferPutBits = PutBits; }
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()); } }
/** * DOCUMENT ME! * * @param data DOCUMENT ME! * @param out DOCUMENT ME! */ void WriteArray(byte[] data, OutputStream out) throws IOException { int length = (((int) (data[2] & 0xFF)) << 8) + (int) (data[3] & 0xFF) + 2; out.write(data, 0, length); }
/** * DOCUMENT ME! * * @param data DOCUMENT ME! * @param out DOCUMENT ME! */ void WriteMarker(byte[] data, OutputStream out) throws IOException { out.write(data, 0, 2); }
public static void WriteWord(OutputStream output, short w) throws IOException { output.write(w & 0xFF); output.write((w >> 8) & 0xFF); }
/** Writes string to output stream */ protected void writeString(String s) throws IOException { for (int i = 0; i < s.length(); i++) { out.write((byte) s.charAt(i)); } }
/** Write 16-bit value to output stream, LSB first */ protected void writeShort(int value) throws IOException { out.write(value & 0xff); out.write((value >> 8) & 0xff); }
static void WriteString(OutputStream output, String string) throws IOException { for (int loop = 0; loop < string.length(); ++loop) output.write((byte) (string.charAt(loop))); }
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()); } }