Image Transparency

miker1264 · 1328

miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #15 on: August 11, 2022, 11:53:14 AM
I think I'm getting the gist of crc32.

It just takes a bit to sort it all out.
No worries, i'm more than sure you are capable enough to figure it out (and in case not you can always ask for help).


Glad to hear the links were of use to you. thx for letting me know.

@magorium

I succeeded in writing a valid tRNS Chunk CRC.

Here is the key to crc32 that isn't exactly explained directly in documentation.

I was using this function: crc32 = Update_crc32 (crc, trans, num_trans);
trans is the byte array of tRNS Data & num_trans is the length in bytes of that data. Also ULONG crc = 0;

The crc value I got was far off from what I expected. With this I got 3746694771 but I wanted 24 4F A6 2E = 609199662.

Then I remembered seeing something similar in documentation. So I tried crc32 = Update_crc32 (crc, trans+4, num_trans+4);
Notice I added 4 bytes to the data & length. That yielded:  615395991 but not quite the target value: 609199662.
It was a slight amount off but not by much. So adding 4 to length is correct but the data buffer needed to be adjusted.

Previously I set up a binary file Data2.bin that contains: 'tRNS' + data for testing. So I simply read that into a byte array.
/* Calculate CRC Checksum */
      UBYTE *crc_buffer;
      crc_buffer = (UBYTE *)AllocVec(num_trans+4, MEMF_ANY);
      char *crc_data = ("Ram Disk:Data2.bin");
        BPTR crcfile = Open(crc_data, MODE_OLDFILE);
      Seek(crcfile,0,OFFSET_BEGINNING);
      Read(crcfile,crc_buffer, num_trans+4);
      crc32 = Update_crc32 (crc, crc_buffer, num_trans+4);
        //crc32 = Update_crc32 (crc, trans+4, num_trans+4);
      //crc32 = get_crc (trans, num_trans);
      Printf("tRNS Chunk Checksum:%lu\n", crc32);
      Close(crcfile);

The result was: 24 4F A6 2E = 609199662 which was exactly what I was looking for!
So the important part for crc is the byte buffer not only contains the data but also the ChunkID & total length is length + 4.
« Last Edit: August 11, 2022, 01:41:37 PM by miker1264 »



miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #16 on: August 11, 2022, 01:44:20 PM
Since the crc is additive maybe this works also.

ULONG crc32;
ULONG crc = 0;
crc32 = Update_crc32 (crc, trans, num_trans);
crc32 = Update_crc32 (crc, chunk_ID, 4);

First process the chunk-data then add the chunk-name.

In theory any 32bit image supported by AROS Datatypes will suffice for input with or without Transparency.

That includes 32bit BMPX, 32bit PNG, 32bit TIFF.

If the original image has Transparency then the output 8bit PNG will also have Transparency with tRNS Chunk.

« Last Edit: August 11, 2022, 02:11:29 PM by miker1264 »



magorium

  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #17 on: August 11, 2022, 02:12:27 PM
So the important part for crc is the byte buffer not only contains the data but also the ChunkID & total length is length + 4.
Yups, indeed the checksum needs to be calculated including the name of the chunk as well (see the first link i posted). Only the length field of the chunk is not part of the checksum.


Since the crc is additive maybe this works also.
Yes, that should work although my OCD tells me to add the chunk name first, then the data  :)


miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #18 on: August 11, 2022, 02:34:41 PM
So the important part for crc is the byte buffer not only contains the data but also the ChunkID & total length is length + 4.
Yups, indeed the checksum needs to be calculated including the name of the chunk as well (see the first link i posted). Only the length field of the chunk is not part of the checksum.


Since the crc is additive maybe this works also.
Yes, that should work although my OCD tells me to add the chunk name first, then the data  :)

Good point. I will just reverse it.

when writing the tRNS Chunk to file I first write the 4byte length. Then I put the chunk-name into a byte array & write that. Finally I write the chunk-data before writing the 4byte crc value. I already have chunkID in byte array. So rather than use memcpy to copy the chunkID & chunk-data to a new array why not just process the crc in two parts to simplify the code.