public ColorMap <ushort> DecodeMap(Stream stream, Exif _exif) { var exif = (PanasonicExif)_exif; stream.Seek(exif.RawOffset, SeekOrigin.Begin); int row, col, i, j, sh = 0; int[] pred = new int[2], nonz = new int[2]; var resultHeight = exif.ImageHeight; var resultWidth = exif.CropRight; var map = new ColorMap <ushort>(resultWidth, resultHeight, 12); int value; var bits = new PanasonicBitStream(stream); for (row = 0; row < exif.ImageHeight; row++) { var line = map.GetRow(row); for (col = 0; col < exif.ImageWidth; col++) { unchecked { i = col % 14; if (i == 0) { pred[0] = pred[1] = nonz[0] = nonz[1] = 0; } if (i % 3 == 2) { sh = 4 >> (3 - bits.Read(2)); } if (nonz[i & 1] != 0) { j = bits.Read(8); if (j != 0) { pred[i & 1] -= 0x80 << sh; if (pred[i & 1] < 0 || sh == 4) { pred[i & 1] &= ~(-1 << sh); } pred[i & 1] += j << sh; } } else { nonz[i & 1] = bits.Read(8); if (nonz[i & 1] != 0 || i > 11) { pred[i & 1] = nonz[i & 1] << 4 | bits.Read(4); } } if (col >= resultWidth) { continue; } value = pred[col & 1]; if (value > 4098) { throw new Exception("Decoding error"); } line.SetAndMoveNext((ushort)Math.Min(4095, value)); } } } return(map); }
private static RawImageFile <ushort> DecodeImagePart(Stream stream, PanasonicExif exif) { int row, col, i, j, sh = 0; int[] pred = new int[2], nonz = new int[2]; var resultHeight = exif.ImageHeight; var resultWidth = exif.CropRight; var map = new RawBGGRMap <ushort>(resultWidth, resultHeight, 12); var raw = map.GetPixel(); int value; var bits = new PanasonicBitStream(stream); for (row = 0; row < exif.ImageHeight; row++) { for (col = 0; col < exif.ImageWidth; col++) { unchecked { i = col % 14; if (i == 0) { pred[0] = pred[1] = nonz[0] = nonz[1] = 0; } if (i % 3 == 2) { sh = 4 >> (3 - bits.Read(2)); } if (nonz[i & 1] != 0) { j = bits.Read(8); if (j != 0) { pred[i & 1] -= 0x80 << sh; if (pred[i & 1] < 0 || sh == 4) { pred[i & 1] &= ~(-1 << sh); } pred[i & 1] += j << sh; } } else { nonz[i & 1] = bits.Read(8); if (nonz[i & 1] != 0 || i > 11) { pred[i & 1] = nonz[i & 1] << 4 | bits.Read(4); } } if (col >= resultWidth) { continue; } value = pred[col & 1]; if (value > 4098) { throw new Exception("Decoding error"); } raw.SetAndMoveNext((ushort)Math.Min(4095, value)); } } } var result = new RawImageFile <ushort>(map) { Exif = exif, }; return(result); }