Menu

#88 Incorrect integer linear color scaling

None
wont-fix
Amiga (2) RGB (1)
5
2023-12-10
2023-12-04
SD Snatcher
No

I noticed this problem when converting Amiga IFFs to PNGs, but it possibly affects other platforms.

Integer linear scaling (iow, simple bit shifting) seems to be used to scale the source number of DAC color bits to 24bit RGB. This results in the lower bits all being set to zero, and a insuficient white level will be resulted (240 for Amiga images, instead of 255).

Bit shifting was used for this kind of RGB bit depth upscale conversion in the 80s because it was easy and fast. But nowadays it shouldn't be used anymore.

Nowadays, the RGB bit depth upscaling must be done using floating point and the rule of three formula, then round to the nearest integer value at the end.

For example, to convert Amiga & PC-98 RGB-12bit values to RGB-24bit the formula is

r8=round(r4*255/15);
g8=round(g4*255/15);
b8=round(b4*255/15);

Analogously, this is for the MSX2 * Atari ST RGB-9bit values:

r8=round(r3*255/7);
g8=round(g3*255/7);
b8=round(b3*255/7);

The generic formula is:

r8=round(rSrc*255/(pow(2,rBits)-1));
g8=round(gSrc*255/(pow(2,gBits)-1));
b8=round(bSrc*255/(pow(2,bBits)-1));

I attached here an example IFF file, the png with incorrect color upscaling done by Recoil, and a reference png with correct color scaling for reference.

Note: The bug reported in bug #51 is related to this bug. But there, the number of source RGB bits will have to be selected according to the platform, since it's a multi-platform file.

4 Attachments

Discussion

  • Piotr Fusik

    Piotr Fusik - 2023-12-04
    • status: open --> accepted
    • assigned_to: Piotr Fusik
    • Attachments has changed:

    Diff:

    --- old
    +++ new
    @@ -1,3 +1,4 @@
    +disk2_1-hex.png (18.1 kB; image/png)
     disk2_1.iff (31.4 kB; image/x-recoil)
     disk2_1_by_Recoil.png (9.5 kB; image/png)
     disk2_1_correctColorUpscaling.png (9.5 kB; image/png)
    
    • Group: -->
     
  • Piotr Fusik

    Piotr Fusik - 2023-12-04

    IFFs store the palette in 8 bits per component. See the attached hex dump.
    It is not ok to unconditionally strip it to 4 bits.
    Since 2015, RECOIL includes additional code to detect if the image is originating from an OCS Amiga and only then treat the palette as 4 bits per component, using equal nibbles.
    It checks if all the lower nibbles are zero (which they are in this picture), but also checks a byte in BMHD. Here, this byte with value 0x80 at offset 0x1F prevents from treating this image as an OCS Amiga picture.

     
    • SD Snatcher

      SD Snatcher - 2023-12-10

      Thank you for the explanation.

      This means that for IFF files, RECOIL strips the bits as it should. Could you please do the same for the bug #51, for .MAG, .MKI and .PI files?

       
  • Piotr Fusik

    Piotr Fusik - 2023-12-04

    I just found the info about this byte in a 2015-10-10 mail from cholok, who is an expert in Amiga formats. He said that this header flag specifically indicates the color palette is 24-bit.

    Other platform's palettes, if below 24-bit, are also expanded to 24-bit and not zero extended, so I'm going to close this ticket unless you add more examples that you think are wrong.

     
    • SD Snatcher

      SD Snatcher - 2023-12-10

      Understood. It seems that I have some IFFs with this incorrect info on their headers.

      I used an hex editor to change the offset 0x1F to 0x00, and indeed the colors were rendered correctly now. I'll manually fix the rest of the files of this set, thanks!

      I think you might close this ticket then.

       

      Last edit: SD Snatcher 2023-12-10
  • Piotr Fusik

    Piotr Fusik - 2023-12-10
    • status: accepted --> wont-fix
     

Log in to post a comment.