1
\$\begingroup\$

I'm having abit of a brainfart and I can't quite grasp what I'm doing wrong. It's quite simple, I am importing an image with FreeImage (http://freeimage.sourceforge.net/) which has a method FreeImage_GetBits that returns a pointer to the first byte of the image data. I then try to load all the data into memory using (bitsperpixel / 8) * pixelsWidth ' pixelHeight, like this:

uint32_t bitsPerPixel           = FreeImage_GetBPP(bitmap);   // resolves to 24
uint32_t widthInPixels          = FreeImage_GetWidth(bitmap);  // resolves to 1024
uint32_t heightInPixels         = FreeImage_GetHeight(bitmap);  // resolves to 1024

// container is a std::vector<uint8_t>
pkgMaterial.mTextureData.insert(pkgMaterial.mTextureData.begin(), FreeImage_GetBits(bitmap), FreeImage_GetBits(bitmap) + ((bitsPerPixel/8) * widthInPixels * heightInPixels));

I have a jpg which is 31 kilobytes in size on disc. Yet when I load it using the above formula, I see the vector is then filled with 3145728 bytes, which is approx 3145 kilobytes. What am I doing wrong?

\$\endgroup\$
5
  • \$\begingroup\$ Jpegs are compressed data types, is the jpeg being uncompressed when you load it in? \$\endgroup\$
    – Evan
    Commented Apr 28, 2013 at 21:12
  • \$\begingroup\$ I cannot find any information if it loads the jpeg compressed or uncompressed. Why would it try to uncompress a jpg image though... isnt it just a one-way compression? \$\endgroup\$ Commented Apr 28, 2013 at 21:45
  • 1
    \$\begingroup\$ It will have to uncompress the data before it can be used as an image, if it uncompresses it at the time it is loaded, then it doesn't have to waste clock cycles when the image is being drawn. One way or another, drawing an image requires access to per pixel color data. \$\endgroup\$
    – Evan
    Commented Apr 28, 2013 at 22:16
  • \$\begingroup\$ OpenGL as probably every other graphics API only understands an image if it is uncompressed - that means one block of memory (24 or 32 bit) is one pixel. JPEG files does not provide the image data in that manner, it must be uncompressed. \$\endgroup\$
    – Appleshell
    Commented Oct 6, 2013 at 18:51
  • \$\begingroup\$ @AdamS Bit of a nitpick, but GPUs and graphics APIs do understand some compressed formats - it's just that JPEG isn't one of them. :) The DXT and BC compression formats are supported by the graphics hardware, though, and are almost universally used in games. \$\endgroup\$ Commented Nov 5, 2013 at 21:47

1 Answer 1

1
\$\begingroup\$

You are doing nothing wrong.
FreeImage simply decompresses the image, so you can easily read from it and/or modify it.
You cannot render an image or read any pixel values from it without decompressing it first.
And obviously the memory consumption increases if you decompress the image.

You can try to compare the file size of a BMP image before and after loading it. It should roughly be the same, because BMP images are not compressed.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .