libjpeg/libjpeg-turbo RGBA/32-bit int decompression

Bailey Parker

When using libjpeg to feed images into OpenCL, to be able to treat channels as normalized uint8's with CL_UNORM_INT8 (floats in the range [0.0, 1.0]), you can only feed it buffers with 4 channel components. This is problematic, because libjpeg only outputs 3 (by default in RGB order) since JPEG has no notion of opacity.

The only workaround I see is to scanlines with libjpeg and then make a duplicate buffer of the appropriate length (with the fourth channel component added for each pixel in the scanlines) and then memcpy the values over, setting the alpha component to 255 for each. You could even do this in place if you are tricky and initialize the buffer to be of row_stride * 4 initially and then walk backwards from index row_stride * 3 - 1 to 0, moving components to the proper places in the full buffer (and adding 255 for alpha where necessary).

However, this feels hacky and if you're dealing with large images (I am), it's unacceptable to have this extra pass over (what will be in aggregate) the entire image.

So, is there a way to get libjpeg to just extend the number of components to 4? I've tried setting properties on cinfo like output_components to no avail. I've read that the only workaround is to compile a special version of libjpeg with the constant RGB_COMPONENTS = 4 set in jmorecfg.h, but this certainly doesn't feel portable or for that matter necessary for such a (common) change of output.

Bailey Parker

So it turns out that the best solution (at least, the one that doesn't require any custom builds of libs or extra passes through the buffer) is to use libjpeg-turbo. As of 1.1.90 they provide a colorspace constant JCS_EXT_RGBX that adds a fake alpha channel. To my knowledge this is only documented in the release notes of a beta version on SourceForge so barring that this URL changes or no longer exists (read: the internet revolts against sf for its shady insertion of code into "inactive" popular repos and they are forced to shut down), here is the relevant bit reproduced:

When decompressing a JPEG image using an output colorspace of JCS_EXT_RGBX, JCS_EXT_BGRX, JCS_EXT_XBGR, or JCS_EXT_XRGB, libjpeg-turbo will now set the unused byte to 0xFF, which allows applications to interpret that byte as an alpha channel (0xFF = opaque).

Note that this also allows for alternate orderings such as BGR should you need them.

To use it after your jpeg_read_header() call (because this call sets a member on cinfo we need to a default) but before your jpeg_start_decompress() call (because it uses the value of this member), add:

cinfo.out_color_space = JCS_EXT_RGBX; // or JCS_EXT_XRGB, JCS_EXT_BGRX, etc.

And now scanning lines during the decompress will return an extra fourth component for each pixel set to 255.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

libjpeg/libjpeg-turbo RGBA/32-bit int decompression

From Dev

Bit twiddling to get sign bit of 32 bit int

From Dev

how to view turbo c++ fullscreen? (windows 7 32-bit)

From Dev

8 bit int vs 32 bit int on a 32 bit architechture (GCC)

From Dev

Converting 16 bit unsigned int array to 32 bit float array

From Dev

Unsigned int from 32 bit to 64bit OS

From Dev

Convert 11 bit hex value to a signed 32 bit int

From Dev

Int overflow on 32-bit systems

From Dev

constructing 32 bit unsigned int in c

From Dev

Python - Generate a 32 bit random int with arguments

From Dev

sizeof(int*) in 32-bit compatibility mode

From Dev

Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

From Dev

Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

From Dev

How to shift 32 bit int by 32 (yet again)

From Dev

how to extract the 4 bytes of a 32bit int in lua

From Java

A fast method to round a double to a 32-bit int explained

From Dev

How to get the first 11 bits of a 32 bit int with ctypes

From Dev

Reading a certain bit from a u32int

From Dev

How to split an unsigned long int (32 bit) into 8 nibbles?

From Dev

Conversion from 32 bit binary string to int Java

From Dev

C# change the first 32bit Int of a GUID

From Dev

Display a 32 bit unsigned int image with Tkinter and Python 3

From Dev

With FreeImage is it possible to have ConvertToRawBits output in ARGB32 or RGBA32 rather than BGRA32?

From Dev

Pass 64-bit int as output to 32-bit inline asm

From Dev

break a 32bit integer, and use the second half 16bit to form an int

From Dev

Combine four 8-bit unsigned ints into one 32-bit unsigned int

From Dev

Vector of 64-bit double faster to dot-product than a vector of 32-bit unsigned int?

From Dev

32bit C program no longer converting char to int on 64 bit OS Linux

From Dev

break a 32bit integer, and use the second half 16bit to form an int

Related Related

  1. 1

    libjpeg/libjpeg-turbo RGBA/32-bit int decompression

  2. 2

    Bit twiddling to get sign bit of 32 bit int

  3. 3

    how to view turbo c++ fullscreen? (windows 7 32-bit)

  4. 4

    8 bit int vs 32 bit int on a 32 bit architechture (GCC)

  5. 5

    Converting 16 bit unsigned int array to 32 bit float array

  6. 6

    Unsigned int from 32 bit to 64bit OS

  7. 7

    Convert 11 bit hex value to a signed 32 bit int

  8. 8

    Int overflow on 32-bit systems

  9. 9

    constructing 32 bit unsigned int in c

  10. 10

    Python - Generate a 32 bit random int with arguments

  11. 11

    sizeof(int*) in 32-bit compatibility mode

  12. 12

    Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

  13. 13

    Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

  14. 14

    How to shift 32 bit int by 32 (yet again)

  15. 15

    how to extract the 4 bytes of a 32bit int in lua

  16. 16

    A fast method to round a double to a 32-bit int explained

  17. 17

    How to get the first 11 bits of a 32 bit int with ctypes

  18. 18

    Reading a certain bit from a u32int

  19. 19

    How to split an unsigned long int (32 bit) into 8 nibbles?

  20. 20

    Conversion from 32 bit binary string to int Java

  21. 21

    C# change the first 32bit Int of a GUID

  22. 22

    Display a 32 bit unsigned int image with Tkinter and Python 3

  23. 23

    With FreeImage is it possible to have ConvertToRawBits output in ARGB32 or RGBA32 rather than BGRA32?

  24. 24

    Pass 64-bit int as output to 32-bit inline asm

  25. 25

    break a 32bit integer, and use the second half 16bit to form an int

  26. 26

    Combine four 8-bit unsigned ints into one 32-bit unsigned int

  27. 27

    Vector of 64-bit double faster to dot-product than a vector of 32-bit unsigned int?

  28. 28

    32bit C program no longer converting char to int on 64 bit OS Linux

  29. 29

    break a 32bit integer, and use the second half 16bit to form an int

HotTag

Archive