I've asked a similar question some months ago, on the following link: Creating a code to decompress byte-oriented RLE image
And I've also read this question (was closer to mine): Extract a bitmap from PostScript
I am dealing with CIP3 files, which are basically PostScript files with a few additional items. The important ones here are:
Image matrice
Image width / height
Image data
The image data is an RLE Encoded data - and I've got the code to decode RLE, from the question I asked some months ago. The code works, I tested it on some sample files I have.
However, look this table:
I got things working only on case 1 and 2. However, when it comes to read it "from right to left", or "from bottom to top" I can't get it to work.
I tried:
LINQ's Reverse() function - still produces an invalid image
Reverse the byte array using a regular for loop - invalid image also
Reverse the file after it was decoded - invalid image
Reverse the byte array before decoding - invalid image
This is the code to decode the RLE image:
class RLEDecompressor
{
public byte[] rleDecompressed { get; private set; }
public RLEDecompressor(byte[] byteFile)
{
List<byte> final = new List<byte>();
int i = 0;
while (i < byteFile.Length)
{
try
{
var lengthByte = byteFile[i++]; //First run, position 1
if (lengthByte <= 127)
{
int currLen = lengthByte + 1;
for (int j = 0; j < currLen; j++)
final.Add(byteFile[i++]);
}
else
{
int currLen = 257 - lengthByte;
byte byteToCopy = byteFile[i++];
for (int j = 0; j < currLen; j++)
final.Add(byteToCopy);
}
}
catch (Exception ex)
{
rleDecompressed = final.ToArray();
break;
}
}
rleDecompressed = final.ToArray();
}
I expected the output to be an valid image. However all outputs I got were blurry images or "something else" (not a valid image).
I'm providing a sample of the file I'm trying to decode. The link is: https://drive.google.com/open?id=1iTSyCKTzXGPyONejXnw28KEDacjIpBUl&fbclid=IwAR2DujV_eYBA8Ees6LRWUVcQio0cJ5EIpb4ck1BT6xmcRdpXGVubf57BHh4
Image width: 2061
Image Height: 1571
Channels: 1
If everything is decoded correctly, the image can be viewed in Photoshop (just save it with extension .raw).
Thanks in advance for any input.