用“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)声明局部变量 2)直接与指针的工作,改变他们的价值观。 但我仍然不能改变像素P(1,1)的颜色 –
你想要什么结果并没有你会得到什么结果呢? – pm100