Question : getting and setting pixel data for a 3 channels RGB  image library acces violation ?

Hi, I have an Image library that has grayscale and rgb image.

I have done code to read and write on the grayscale image and the call for this function runs well but unfortunatly I have an issue for the function setpixel on the rgb image.  

I think the problem comes from a conversion from double to unsigned long
because I'm using an LUT (llok up table) wich returns a double instead of long and then to pass it in the setpixel function maybe I must use an ltoa() function before passing it to setpixel of the rgb image so how do that ?

Here below the prototypes of both functions:

class CImageY_Int
{
public:
      // The y coordinate goes from top to botom
    virtual unsigned char get_pixel(int x, int y)=0;

      //set the pixel RGB with the char value
      virtual void set_pixel(int x, int y, unsigned char pxl_char)=0;

...
}

class CImageRGB_Int
{
public:
    // The x coordinate goes from left to right
    // The y coordinate goes from top to botom
    virtual unsigned long get_pixel(int x, int y)=0;

      //set the pixle RGB with the long value
      virtual void set_pixel(int x, int y, unsigned long pxl_lng)=0;

...
}

Thank you


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:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
//the grayscale function:

void pre_process_y  (CImageY_Int* CImgY, double* lut_transform, int param, CImageY_Int* CImgY_InOut)
{
	int w=CImgY->get_img_width();
	int h=CImgY->get_img_height();
	int i =0;
	int j =0;
	unsigned char gray;

	// create a cimage buffer
	unsigned char *cimageDataPointer;
	cimageDataPointer = CImgY->get_img_data();

	// create a cimage buffer
	unsigned char *cimageDataPointer_inout;
	cimageDataPointer_inout = CImgY_InOut->get_img_data();

	//copying from CImages
	for(unsigned int p=0;p<CImgY->get_img_height();p++)
	{
		// memory copy 
		memcpy(cimageDataPointer_inout, cimageDataPointer, CImgY->get_img_width()*1); 
		// cimageDataPointer  jump to next line 
		cimageDataPointer = cimageDataPointer + CImgY->get_img_width()*1; 
		// cvImageDataPointer  jump to next line
		cimageDataPointer_inout = cimageDataPointer_inout+(CImgY_InOut->get_img_width()*1);
	}

  for(j=0;j<h-1;j++)
	{
	for(i=0;i<w-1;i++)
	  {

		//int index = (i*h)*1+j*1;
		gray= CImgY_InOut->get_pixel(i,j);
		CImgY_InOut->set_pixel(i,j, lut_transform[gray]);

	  }
	}

}



//the rgb function:

pre_process_rgb  (CImageRGB_Int* CImgRGB, double* lut_transform, int param, CImageRGB_Int* CImgRGB_InOut)
{
	int nb_channel	=3;
	int w=CImgRGB->get_img_width();
	int h=CImgRGB->get_img_height();
	int i =0;
	int j =0;
	int k =0;
	unsigned long color;

	// create a cimage buffer
	unsigned char *cimageDataPointer;
	cimageDataPointer = CImgRGB->get_img_data();

	// create a cimage buffer
	unsigned char *cimageDataPointer_inout;
	cimageDataPointer_inout = CImgRGB_InOut->get_img_data();

	//copying from CImage to CV image
	for(unsigned int p=0;p<CImgRGB->get_img_height();p++)
	{
		// memory copy 
		memcpy(cimageDataPointer_inout, cimageDataPointer, CImgRGB->get_img_width()*nb_channel); 
		// cimageDataPointer  jump to next line 
		cimageDataPointer = cimageDataPointer + CImgRGB->get_img_width()*nb_channel; 
		// cvImageDataPointer  jump to next line
		cimageDataPointer_inout = cimageDataPointer_inout+(CImgRGB_InOut->get_img_width()*nb_channel);//+nb_channel?
	}

  for(j=0;j<h-1;j++)
	{
	for(i=0;i<(w-1);i++)
	  {

			//int index = (i*h)*nb_channel+j*nb_channel;
			color= CImgRGB_InOut->get_pixel(i,j);

			//pCImgRGB->set_pixel(0, 0, 0x080706);
		 	CImgRGB_InOut->set_pixel(i,j, lut_transform[color]); //ERROR ACCESS VIOLATION 

	  }
	}


}

Answer : getting and setting pixel data for a 3 channels RGB  image library acces violation ?

Ok. I'm starting to understand where you want to go.

Is this what you had in mind ?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
  for(j=0;j<h-1;j++)
	{
	for(i=0;i<(w-1);i++)
	  {

			//int index = (i*h)*nb_channel+j*nb_channel;
			color = CImgRGB_InOut->get_pixel(i,j);

			unsigned char color_r = color & 0x000000FF;
			unsigned char color_g = (color & 0x0000FF00) >> 8;
			unsigned char color_b = (color & 0x00FF0000) >> 16;
			unsigned char color_a = (color & 0xFF000000) >> 24;

			unsigned long color2 = (color_a << 24)
			                     | (((unsigned char) lut_transform[color_b]) << 16)
			                     | (((unsigned char) lut_transform[color_g]) << 8)
			                     | ((unsigned char) lut_transform[color_r]);

			//pCImgRGB->set_pixel(0, 0, 0x080706);
		 	CImgRGB_InOut->set_pixel(i,j, color2);

	  }
	}
Random Solutions  
 
programming4us programming4us