C - 需要帮助交换红色和绿色组件

时间:2013-04-12 23:34:54

标签: c struct ppm

这段代码(我在之前的Stack Overflow问题中找到的代码编辑)能够输入ppm图像并输出相同的图像,但所有颜色互补。在我的任务中,我被要求这样做,但随后交换红色和绿色组件(基本上将img-> data [i] .red转换为img-> data [i] .green,反之亦然)。但是,我不确定如何去做这件事。我不能只做两个平等的陈述,因为第一个基本上会使第二个无效。我唯一能想到的是创建一个PPMImage类型的临时数组(之前创建的结构)。但是,我有点不确定这个代码和逻辑是如何工作的。

这是代码。 changecolorPPM函数中的'for'循环是颜色补充的地方,我非常确定需要进行交换。

#include<stdio.h>
#include<stdlib.h>

typedef struct {
     unsigned char red,green,blue;
} pixel_t;

typedef struct {
     int x, y;
     pixel_t *data;
} PPMImage;

#define CREATOR "RPFELGUEIRAS"
#define RGB_COMPONENT_COLOR 255

static PPMImage *readPPM(const char *filename)
{
         char buff[16];
         PPMImage *img;
         FILE *fp;
         int c, rgb_comp_color;
         //open PPM file for reading
         fp = fopen(filename, "rb");
          if (!fp) {
              fprintf(stderr, "Unable to open file '%s'\n", filename);
              exit(1);
         }

     //read image format
     if (!fgets(buff, sizeof(buff), fp)) {
          perror(filename);
          exit(1);
     }

//check the image format
if (buff[0] != 'P' || buff[1] != '6') {
     fprintf(stderr, "Invalid image format (must be 'P6')\n");
     exit(1);
}

//alloc memory form image
img = (PPMImage *)malloc(sizeof(PPMImage));
if (!img) {
     fprintf(stderr, "Unable to allocate memory\n");
     exit(1);
}

//check for comments
c = getc(fp);
while (c == '#') {
while (getc(fp) != '\n') ;
     c = getc(fp);
}

ungetc(c, fp);
//read image size information
if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
     fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
     exit(1);
}

//read rgb component
if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
     fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
     exit(1);
}

//check rgb component depth
if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
     fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
     exit(1);
}

while (fgetc(fp) != '\n') ;
//memory allocation for pixel data
img->data = (pixel_t*)malloc(img->x * img->y * sizeof(pixel_t));

if (!img) {
     fprintf(stderr, "Unable to allocate memory\n");
     exit(1);
}

//read pixel data from file
if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
     fprintf(stderr, "Error loading image '%s'\n", filename);
     exit(1);
}

fclose(fp);
return img;
}
 void writePPM(const char *filename, PPMImage *img)
{
FILE *fp;
//open file for output
fp = fopen(filename, "wb");
if (!fp) {
     fprintf(stderr, "Unable to open file '%s'\n", filename);
     exit(1);
}

//write the header file
//image format
fprintf(fp, "P6\n");

//comments
fprintf(fp, "# Created by %s\n",CREATOR);

//image size
fprintf(fp, "%d %d\n",img->x,img->y);

// rgb component depth
fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);

// pixel data
fwrite(img->data, 3 * img->x, img->y, fp);
fclose(fp);
}

void changeColorPPM(PPMImage *img)
{
int i;

if(img){

     for(i=0;i<img->x*img->y;i++){
          img->data[i].red=RGB_COMPONENT_COLOR-img->data[i].red;
          img->data[i].green=RGB_COMPONENT_COLOR-img->data[i].green;
          img->data[i].blue=RGB_COMPONENT_COLOR-img->data[i].blue;
     }
}
}

int main(int argc, char* argv[]){
PPMImage *image;
char* filename = argv[1];
image = readPPM(filename);
changeColorPPM(image);
writePPM("OutputFile.ppm",image);
printf("Press Enter");
getchar();
}

1 个答案:

答案 0 :(得分:0)

从您的问题来看,我相信您正在寻找可以轻松实现的swap功能

img->data[i].red = img->data[i].red + img->data[i].green;
img->data[i].green = img->data[i].red - img->data[i].green;
img->data[i].red = img->data[i].red - img->data[i].green;

<强>更新

如果添加操作没有溢出,则此部分可以正常使用。但是,如果目标的精度不足以容纳这些位,则可能存在潜在的数据丢失。因此,在这些情况下,正如Mats和其他专家所建议的那样,传统的临时变量方法将是最好的前进方式

temp = img->data[i].red;
img->data[i].red = img->data[i].green;
img->data[i].green = temp;

注意:

如果您正在进行图像处理,那么从视觉角度来看,这种交换可能并不令人满意。通常,与红色和蓝色相比,绿色包含最大强度即亮度信息量,并且人眼可以非常容易地捕捉强度的变化。有趣的是检查交换后最终图片的样子。