void ToIndexedColor(byte[][] r, byte[][] g, byte[][] b) throws AWTException { this.pixels_ = new byte[this.width_ * this.height_]; this.colors_ = new byte[768]; int colornum = 0; for (int x = 0; x < this.width_; x++) { for (int y = 0; y < this.height_; y++) { int search; for (search = 0; search < colornum; search++) { if ((this.colors_[(search * 3)] == r[x][y]) && (this.colors_[(search * 3 + 1)] == g[x][y]) && (this.colors_[(search * 3 + 2)] == b[x][y])) break; } if (search > 255) { throw new AWTException("Too many colors."); } this.pixels_[(y * this.width_ + x)] = (byte) search; if (search == colornum) { this.colors_[(search * 3)] = r[x][y]; this.colors_[(search * 3 + 1)] = g[x][y]; this.colors_[(search * 3 + 2)] = b[x][y]; colornum++; } } } this.numColors_ = (1 << BitUtils.BitsNeeded(colornum)); byte[] copy = new byte[this.numColors_ * 3]; System.arraycopy(this.colors_, 0, copy, 0, this.numColors_ * 3); this.colors_ = copy; }
public void Write(OutputStream output) throws IOException { BitUtils.WriteString(output, "GIF87a"); ScreenDescriptor sd = new ScreenDescriptor(this.width_, this.height_, this.numColors_); sd.Write(output); output.write(this.colors_, 0, this.colors_.length); ImageDescriptor id = new ImageDescriptor(this.width_, this.height_, ','); id.Write(output); byte codesize = BitUtils.BitsNeeded(this.numColors_); if (codesize == 1) codesize = (byte) (codesize + 1); output.write(codesize); LZWCompressor.LZWCompress(output, codesize, this.pixels_); output.write(0); id = new ImageDescriptor((short) 0, (short) 0, ';'); id.Write(output); output.flush(); }