public override ImageData Read(IBinaryStream file, ImageMetaData info) { var meta = (RbpMetaData)info; file.Position = meta.DataOffset; int stride = 4 * (int)meta.Width; var pixels = new byte[stride * (int)meta.Height]; if (24 == meta.BPP) { for (int i = 0; i < pixels.Length; i += 4) { int pixel = file.ReadInt24(); pixels[i] = (byte)(pixel << 3); pixels[i + 1] = (byte)((pixel >> 3) & 0xFC); pixels[i + 2] = (byte)((pixel >> 8) & 0xF8); pixels[i + 3] = (byte)((pixel >> 16) * 0xFF / 0x3F); } } else { file.Read(pixels, 0, pixels.Length); for (int i = 3; i < pixels.Length; i += 4) { byte alpha = pixels[i]; if (alpha != 0) { pixels[i] = (byte)(alpha * 0xFF / 0x3F); } } } return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, stride)); }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { int total_pixels = (int)info.Width * (int)info.Height; var pixels = new byte[total_pixels * 3]; int count = total_pixels >> 1; int dst = 0; int s1 = 1, s2 = 1, s3 = 1; int r = 0, g = 0, b = 0; file.Position = 0x10; for (int i = 0; i < count; ++i) { int c = file.ReadInt24(); s1 = s1 << 4 | c & 0xF; s2 = s2 << 4 | (c >> 4) & 0xF; s3 = s3 << 4 | (c >> 8) & 0xF; b += RgbShift[s1]; g += RgbShift[s2]; r += RgbShift[s3]; pixels[dst++] = (byte)b; pixels[dst++] = (byte)g; pixels[dst++] = (byte)r; s1 = ShiftTable[s1] << 4 | (c >> 16) & 0xF; s2 = ShiftTable[s2] << 4 | (c >> 20) & 0xF; s3 = ShiftTable[s3] << 4 | (c >> 12) & 0xF; b += RgbShift[s1]; g += RgbShift[s2]; r += RgbShift[s3]; pixels[dst++] = (byte)b; pixels[dst++] = (byte)g; pixels[dst++] = (byte)r; s1 = ShiftTable[s1]; s2 = ShiftTable[s2]; s3 = ShiftTable[s3]; } return(ImageData.CreateFlipped(info, PixelFormats.Bgr24, null, pixels, (int)info.Width * 3)); }
void QlzUnpack(IBinaryStream input, byte[] output) { int dst = 0; uint bits = 1; int output_last = output.Length - 11; while (dst < output.Length) { if (1 == bits) { bits = input.ReadUInt32(); } if ((bits & 1) == 1) { int ctl = input.PeekByte(); int offset, count = 3; if ((ctl & 3) == 0) { offset = input.ReadUInt8() >> 2; } else if ((ctl & 2) == 0) { offset = input.ReadUInt16() >> 2; } else if ((ctl & 1) == 0) { offset = input.ReadUInt16() >> 6; count += ((ctl >> 2) & 0xF); } else if ((ctl & 0x7F) != 3) { offset = (input.ReadInt24() >> 7) & 0x1FFFF; count += ((ctl >> 2) & 0x1F) - 1; } else { uint v = input.ReadUInt32(); offset = (int)(v >> 15); count += (int)((v >> 7) & 0xFF); } Binary.CopyOverlapped(output, dst - offset, dst, count); dst += count; } else { if (dst > output_last) { break; } output[dst++] = input.ReadUInt8(); } bits >>= 1; } while (dst < output.Length) { if (1 == bits) { input.Seek(4, SeekOrigin.Current); bits = 0x80000000u; } output[dst++] = input.ReadUInt8(); bits >>= 1; } }
byte[] Unpack24bpp() { Format = PixelFormats.Bgr24; int pixel_count = (int)(Info.Width * Info.Height); var output = new byte[pixel_count * Info.BPP / 8 + 1]; var frame = new byte[384]; int dst = 0; int v19 = 0; while (pixel_count > 0) { int count, frame_pos, pixel; if (v19 != 0) { pixel = m_input.ReadInt24(); count = 1; frame_pos = 127; --v19; } else { count = m_input.ReadUInt8(); int lo = count & 0x1F; if (0 != (count & 0x80)) { count = ((byte)count >> 5) & 3; if (count != 0) { frame_pos = lo; } else { count = lo << 1; frame_pos = m_input.ReadUInt8(); if (0 != (frame_pos & 0x80)) { ++count; } frame_pos &= 0x7F; } if (0 == count) { count = m_input.ReadInt32(); } int fpos = 3 * frame_pos; pixel = frame[fpos] | frame[fpos + 1] << 8 | frame[fpos + 2] << 16; } else { if (1 == count) { v19 = m_input.ReadUInt8() - 1; } else if (0 == count) { count = m_input.ReadInt32(); } pixel = m_input.ReadInt24(); frame_pos = 127; } } if (count > pixel_count) { count = pixel_count; } pixel_count -= count; LittleEndian.Pack(pixel, output, dst); dst += 3; if (--count > 0) { count *= 3; Binary.CopyOverlapped(output, dst - 3, dst, count); dst += count; } if (frame_pos != 0) { Buffer.BlockCopy(frame, 0, frame, 3, 3 * frame_pos); } frame[0] = (byte)pixel; frame[1] = (byte)(pixel >> 8); frame[2] = (byte)(pixel >> 16); } return(output); }