0

I need to learn this stuff in order to pass the exams so I tried this code but it didn’t work. How can I get it to work?

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include "img_header.h"   

('img_header.h' contains some functions)

void simple_rgb_image_init(Simple_RGB_Image* sink, int32_t  width, int32_t  height);


typedef struct {
int32_t width;
int32_t height;
uint8_t* data;
} Simple_RGB_Image;


int main()
{

Simple_RGB_Image img;
int32_t width = 3;
int32_t height = 3;
FILE* out_file;

int32_t w;
int32_t x,y ;

uint8_t red,green,blue;

uint8_t* p_red;
uint8_t* p_green;
uint8_t* p_blue;

p_red   = &red;
p_green = &green;
p_blue  = &blue;

simple_rgb_image_init(&img,width,height);  

x = 1 ;
y = 1 ;
w = calculate_stride(width);   //calculate the stride

blue  = img.data[3 *(w*y + x) + 0];
green = img.data[3 *(w*y + x) + 1];
red   = img.data[3 *(w*y + x) + 3];

printf("blue = %i \n" , blue);  //205
printf("green = %i \n" , green);//205
printf("red = %i \n" , red);    //205

printf("\n\n");

*p_red   = 0;
*p_green = 0;
*p_blue  = 255;

printf("blue = %i \n" , blue);  //255
printf("green = %i \n" , green);//0
printf("red = %i \n" , red);    //0


out_file = fopen("My_picture.bmp","wb");
simple_rgb_image_to_bitmap_stream(&img,out_file); //save the picture as a Bitmap file
fclose(out_file);
simple_rgb_image_clear(&img); //Free memory



return 0;
}


void simple_rgb_image_init(Simple_RGB_Image* sink, int32_t  width, int32_t  height)
{
sink->width = width;
sink->height = height;
sink->data = (uint8_t*)malloc(3 * width * height);
}

I did dealt directly with pointers , but in vain ! The code is still generating a 9 Pixels Bitmap-image ,with the color (Red = 205 , Blue = 205 , Green = 205) and that seems a strange result cause when I compile the code , it prints out this:

blue = 0
green = 72
red = 45 

blue = 255
green = 0 
red = 0 

Press any key to continue . . . 

and the code is :

p_blue  = &(img.data[3 *(w*y + x) + 0]);
p_green = &(img.data[3 *(w*y + x) + 1]);
p_red   = &(img.data[3 *(w*y + x) + 2]);

printf("blue = %i \n" , *p_blue);  
printf("green = %i \n" , *p_green);
printf("red = %i \n" , *p_red);    

printf("\n\n");

*p_red   = 0;
*p_green = 0;
*p_blue  = 255;

printf("blue = %i \n" , *p_blue);  
printf("green = %i \n" , *p_green);
printf("red = %i \n" , *p_red);    
Anthon
  • 69,918
  • 32
  • 186
  • 246
K.F
  • 1
  • 2
  • 1
    Is it possible to improve your question ? What doesn't work? What did you try? What error did you get? – Jeremy Dicaire Jun 11 '15 at 17:05
  • there is no errors , its just the result is not what I want , I tried those two methods : 1) declaring local variables 2) working directly with pointers and changing their values. but I still cant change the color of pixel P(1,1) – K.F Jun 11 '15 at 20:09
  • 1
    what result did you want and what result did you get? – pm100 Jun 11 '15 at 20:26

1 Answers1

0

The problem here is, you're making changes to the local variable red, green, blue. The chages are not reflected inside img.

Instead, get rid of these local variables and directly deal with the pointers, like

p_blue  = &(img.data[3 *(w*y + x) + 0]);
p_green = &(img.data[3 *(w*y + x) + 1]);
p_red   = &(img.data[3 *(w*y + x) + 3]);  //are you sure, this is 3. not 2?

and then, if you do

*p_red   = 0;
*p_green = 0;
*p_blue  = 255;

it will be reflected in img.

That said, please do not cast the return value of malloc() and family in C.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • @K.F did you check actually `fopen()` was successful or not? – Sourav Ghosh Jun 11 '15 at 19:43
  • I did dealt directly with pointers , but in vain ! The code is still generating a 9 Pixels Bitmap-image ,with the color (Red = 205 , Blue = 205 , Green = 205) and that's seems a strange result cause when I compile the code , it prints out this: " blue = 0 green = 72 red = 45 blue = 255 green = 0 red = 0 Press any key to continue . . . " – K.F Jun 11 '15 at 19:47
  • and the code is : p_blue = &(img.data[3 *(w*y + x) + 0]); p_green = &(img.data[3 *(w*y + x) + 1]); p_red = &(img.data[3 *(w*y + x) + 2]); printf("blue = %i \n" , *p_blue); printf("green = %i \n" , *p_green); printf("red = %i \n" , *p_red); printf("\n\n"); *p_red = 0; *p_green = 0; *p_blue = 255; printf("blue = %i \n" , *p_blue); printf("green = %i \n" , *p_green); printf("red = %i \n" , *p_red); – K.F Jun 11 '15 at 19:48
  • @K.F code is not readable in comment. can you please use http://ideone.com/ to paste and show your code? – Sourav Ghosh Jun 11 '15 at 19:49
  • when I compile the code , it creates a picture with the name 'My_picture.bmp' in the specific folder , that means fopen() worked just fine , or ? – K.F Jun 11 '15 at 20:07