/// <summary> /// Encodes and writes raster data, together with alpha (transparency) data, /// as a 32-bit bitmap. /// </summary> /// <param name="raster"> /// the source raster data </param> /// <param name="alpha"> /// the source alpha data </param> private void Write32(Raster raster, Raster alpha) { int width = raster.getWidth(); int height = raster.getHeight(); // write lines for (int y = height - 1; y >= 0; y--) { // write pixel data for each line for (int x = 0; x < width; x++) { // get RGBA values byte r = (byte)raster.getSample(x, y, 0); byte g = (byte)raster.getSample(x, y, 1); byte b = (byte)raster.getSample(x, y, 2); byte a = (byte)alpha.getSample(x, y, 0); // write RGBA values writer.Write(b); writer.Write(g); writer.Write(r); writer.Write(a); } } }
/// <summary> /// Encodes and writes raster data as a 24-bit bitmap. /// </summary> /// <param name="raster"> /// the source raster data </param> private void Write24(Raster raster) { int width = raster.getWidth(); int height = raster.getHeight(); // calculate bytes per line int bytesPerLine = GetBytesPerLine24(width); // write lines for (int y = height - 1; y >= 0; y--) { // write pixel data for each line for (int x = 0; x < width; x++) { // get RGB values for pixel byte r = (byte)raster.getSample(x, y, 0); byte g = (byte)raster.getSample(x, y, 1); byte b = (byte)raster.getSample(x, y, 2); // write RGB values writer.Write(b); writer.Write(g); writer.Write(r); } // write padding bytes at end of line for (int i = width * 3; i < bytesPerLine; i++) { writer.Write((byte)0); } } }
/// <summary> /// Encodes and writes raster data as a 4-bit bitmap. /// </summary> /// <param name="raster"> /// the source raster data </param> private void Write4(Raster raster) { int width = raster.getWidth(); int height = raster.getHeight(); // calculate bytes per line int bytesPerLine = GetBytesPerLine4(width); // line buffer byte[] line = new byte[bytesPerLine]; // encode and write lines for (int y = height - 1; y >= 0; y--) { // clear line buffer for (int i = 0; i < bytesPerLine; i++) { line[i] = 0; } // encode raster data for line for (int x = 0; x < width; x++) { // calculate buffer index int bi = x / 2; // calculate nibble index (high order or low order) int i = x % 2; // get color index int index = raster.getSample(x, y, 0); // set color index in buffer line[bi] = SetNibble(line[bi], i, index); } // write line data (padding bytes included) writer.Write(line); } }
/// <summary> /// Encodes and writes raster data as an 8-bit bitmap. /// </summary> /// <param name="raster"> /// the source raster data </param> private void Write8(Raster raster) { int width = raster.getWidth(); int height = raster.getHeight(); // calculate bytes per line int bytesPerLine = GetBytesPerLine8(width); // write lines for (int y = height - 1; y >= 0; y--) { // write raster data for each line for (int x = 0; x < width; x++) { // get color index for pixel byte index = (byte)raster.getSample(x, y, 0); // write color index writer.Write(index); } // write padding bytes at end of line for (int i = width; i < bytesPerLine; i++) { writer.Write((byte)0); } } }
/// <summary> /// Encodes and writes raster data as a 1-bit bitmap. /// </summary> /// <param name="raster"> /// the source raster data </param> private void Write1(Raster raster) { int bytesPerLine = GetBytesPerLine1(raster.getWidth()); byte[] line = new byte[bytesPerLine]; for (int y = raster.getHeight() - 1; y >= 0; y--) { for (int i = 0; i < bytesPerLine; i++) { line[i] = 0; } for (int x = 0; x < raster.getWidth(); x++) { int bi = x / 8; int i = x % 8; int index = raster.getSample(x, y, 0); line[bi] = SetBit(line[bi], i, index); } writer.Write(line); } }