Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting RGB from WIC image c++
    primarykey
    data
    text
    <p>I am using WIC to load in an image and then I want to be able to get the RGB values of each pixel. I have got as far as using <strong>getDataPointer()</strong> to create a <strong>byte</strong> buffer (which I cast to a <strong>COLORREF</strong> array) but after this things get strange.</p> <p>I have a 10x10 24bit png that I'm testing with. If I look at the <strong>size</strong> value <strong>getDataPointer()</strong> gives me it says it's 300, which makes sense because 10 * 10 * 3 (for 3 bytes per pixel) = 300. If I do <strong>getStride()</strong> it gives me 30, which also makes sense.</p> <p>So I create a loop to go through the <strong>COLORREF</strong> with an iterator and the condition is <strong>i &lt; size/3</strong> because I know there are only 100 pixels in the array. I then use the macros <strong>GetRValue()</strong>, <strong>GetGValue()</strong> and <strong>GetBValue()</strong> to get the rgb values out.</p> <p>This is when things go weird - My small image is just a test image with solid red, green, blue and black pixels in, but my RGB values come out (255, 0, 0), (0, 255, 0), (27, 255, 36), (0, 0, 0) etc. it seems that some values don't come out properly and are corrupted somehow. Also, the last 20/30 pixels of the image are either loads of crazy colors or all black making me think there some sort of corruption.</p> <p>I also tested it with a larger actual photograph and this comes out all grayscale and repeating the same pattern, making me think it's a stride issue but I don't understand how because when I call <strong>getPixelFormat()</strong> WIC says it's 24bppBGR or 24bppRGB depending on the images.</p> <p>Does anyone have any idea what I am doing wrong? Shouldn't I be using COLORREF and the macros or something like that?</p> <p>Thanks for your time.</p> <p><strong>EDIT</strong></p> <p>Hmm I've still got a problem here. I tried with another 24bit PNG that PixelFormat() was reporting as 24bppBGR but it seems like the stride is off or something because it is drawing as skewed (obligatory nyan cat test):</p> <p><img src="https://i.stack.imgur.com/AkOq5.png" alt="Test WIC load with nyan cat but the stride is off"></p> <p><strong>EDIT 2</strong></p> <p>Okay so now it seems I have some that work and some that don't. Some reporting themselves as 24bpp BGR work while others look like the image above and if I calculate the stride it gives me compared to what it should be they are different and also the size of the buffer is different too. I also have some 32bpp BGR images and some of those work and others don't. Am I missing something here? What could make up the extra bytes in the buffer?</p> <p>Here is an example image:</p> <p><strong>24bppBGR JPEG:</strong></p> <pre><code>width = 126 height = 79 buffer size = 30018 stride = 380 </code></pre> <p>If I calculate this:</p> <pre><code>buffer size should be: width * height * 3 = 126 * 79 * 3 = 29862 difference between calculation and actual buffer size: 30018 - 29862 = 156 bytes stride size should be: width * 3 = 378 difference between calculation and actual buffer size: 380 - 378 = 2 bytes I was thinking that we had 2 extra bytes per line but 79 * 2 is 158 not 156 hmm. </code></pre> <p>If I do these calculations for the images that have worked so far I find no difference in the calculations and the values the code gives me...</p> <p>Am I understanding what is happening here wrong? Should these calculations now work as I have thought?</p> <p>Thanks again</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload