2015-06-11 44 views
0

用「graphics.h中」庫我需要學習這些東西,爲了通過考試,以便 我想這個代碼,但沒有奏效。我怎樣才能使它工作?改變顏色沒有用C

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

( 'img_header.h' 包含了一些功能)

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); 
} 

我就與指針直接處理,但不成功!該代碼仍然生成9像素位圖圖像,顏色(紅色= 205,藍色= 205,綠色= 205),這似乎是一個奇怪的結果,因爲我在編譯代碼時,會打印出以下代碼:

blue = 0 
green = 72 
red = 45 

blue = 255 
green = 0 
red = 0 

Press any key to continue . . . 

而且代碼:

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);  
+1

可以改善你的問題嗎?什麼不行?你嘗試了什麼?你得到了什麼錯誤? –

+0

沒有錯誤,它只是結果不是我想要的,我 嘗試這兩種方法: 1)聲明局部變量 2)直接與指針的工作,改變他們的價值觀。 但我仍然不能改變像素P(1,1)的顏色 –

+1

你想要什麼結果並沒有你會得到什麼結果呢? – pm100

回答

0

這裏的問題是,你是要修改局部變量red,​​,blueimg內沒有反映這些chages。

相反,擺脫這些局部變量和直接處理指針,就像

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? 

,然後,如果你做

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

這將反映在img

這就是說,請do not cast返回值malloc()和家人在C

+0

@ K.F你確實檢查過'fopen()'是否成功? –

+0

我確實直接處理了指針,但徒勞無功!該代碼仍舊產生9個像素的位圖圖像,與顏色(紅色= 205,藍色= 205,綠色= 205),而這似乎是一個奇怪的結果,因爲當我編譯代碼,它打印出這一點: 「 藍色= 0 綠色= 72 紅色= 45 藍色= 255 綠色= 0 紅色= 0 按任意鍵繼續「 –

+0

和代碼是:。 p_blue =(IMG。數據[3 *(w * y + x)+ 0]); \t p_green =(img.data [3 *(W * Y + X)+ 1]); \t p_red =&(img。數據[3 *(w * y + x)+ 2]); \t printf(「blue =%i \ n」,* p_blue); \t printf(「green =%i \ n」,* p_green); printf(「red =%i \ n」,* p_red); \t \t printf(「\ n \ n」); \t * p_red = 0; \t * p_green = 0; \t * p_blue = 255; \t printf(「blue =%i \ n」,* p_blue); \t printf(「green =%i \ n」,* p_green); printf(「red =%i \ n」,* p_red); –