saving RGB pixel data of a .jpeg image in 3D array

I need to save RGB values of each pixel in a 3D array A[width][height][3]. I found a code online that transfers the bytes into an array but I cant understand how bytes are saved them so i could transfer them into an array. The truth is I dont know much about working with images at all so i have a problem working on them. Can someone help me transfer the RGB data from an .jpeg image into a 3D array? This is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
	

#include <iostream>
#include <jerror.h>
#include <jpeglib.h>
using namespace std;
int main()
{
FILE *pic = fopen( "image.jpeg", "rb+" );
    if ( pic == NULL )
    {
        return 0;
    }

struct jpeg_decompress_struct info;
struct jpeg_error_mgr err;

info.err = jpeg_std_error( &err );     
jpeg_create_decompress( &info );

jpeg_stdio_src( &info, pic );    
jpeg_read_header( &info, true );

jpeg_start_decompress( &info );

int w = info.output_width;
int h = info.output_height;
int n = info.num_components;
type = GL_RGB;
      if(n == 4) type = GL_RGBA;

data_size = x * y * 3;
jdata = (unsigned char *)malloc(data_size);
for (;info.output_scanline < info.output_height;)
    {
    rowptr[0] = (unsigned char *)jdata + 3*info.output_width*info.output_scanline; 
    jpeg_read_scanlines(&info, rowptr, 1);
}

int picture[w][h][n];
for(int i=0;i<w;i++)
	for(int j=0;j<h;j++)
		for(int z=0;z<n;z++)
			picture[i][j][z]=/*dont know what to do with this or how to save the bytes as values 0-255*/;
 
jpeg_finish_decompress( &info ); 
fclose(pic);
free (jdata);

return 1;
}
Why are you storing them in a 3d array? That's unnecessary. Assuming you're loading this for OpenGL, just dump the contents into a 1d array and ship it off to the GPU.
Im working on pixel enchanting program and it works for .bmp images, but im enchancing .bmp images by an algorithm that I wrote that works on a 3D array.. so was looking to do the same on .jpg images.
Just for the record, this is not C++
Just read them the same way you do with bmp images then.

@bugbyte
I'm certain he's aware of that.
Last edited on
Maybe it's not GOOD C++... But it's still C++. On to the question, a 3D array is a very bad idea. Get a one-dimensional array of packed RGB data.
You can find your pixel at (X + (Y * width)).
Isn't it simpler?
Sure, but then i also have to change the quality enchanceing algorithm. Ill give it a shot.

@EssGeEich
Im sorry i didnt fully understand your idea. After i save the bytes containing the RGB data, what should i do next, how do you expect me to access each pixel information and save the R G B values? I need them as numbers from 0-255, the whole idea is based on that.

1
2
3
4
5
6
7
data_size = x * y * 3;
jdata = (unsigned char *)malloc(data_size);
for (;info.output_scanline < info.output_height;)
    {
    rowptr[0] = (unsigned char *)jdata + 3*info.output_width*info.output_scanline; 
    jpeg_read_scanlines(&info, rowptr, 1);
}
Last edited on
Topic archived. No new replies allowed.