在C__中将PPM从RGB转换为HSL

时间:2017-04-30 11:32:41

标签: c image-processing histogram rgb ppm

我在C中有一些代码示例。我需要进行直方图均衡。但是,我需要一步一步地前进。我被困在第一步。第一步是将文件从RGB转换为YCbCr.So,我将与您共享代码。我所有的代码都不适合这些区域。另外,我添加了一张显示我失败的图片。我想知道我哪里错了。我希望有人能告诉我光明。错误消息显示“指向浮点值所需的指针值”。这条消息意味着什么?

`

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
#include <fcntl.h>
#include <malloc.h>
#include <math.h>
#define PI 3.1415926535897932384626433832795
struct ppm_header
{
    char pgmtype1;
    char pgmtype2;
    int pwidth;
    int pheight;
    int pmax;
};
struct ppm_file
{
    struct ppm_header *pheader;
    unsigned char *rdata,*gdata,*bdata;
};

// The codes that I've begin from here.    

/*struct RGB  // In fact, this structer is not a comment. I changed it.
{
    unsigned char R;
    unsigned char G;
    unsigned char B;
};*/

struct YCbCr
{
    float Y;
    float Cb;
    float Cr;
};

struct YCbCr RGBToYCbCr(struct ppm_file rgb) {
    float fr = (float)rgb.rdata / 255;
    float fg = (float)rgb.gdata / 255;
    float fb = (float)rgb.bdata / 255;

    struct YCbCr ycbcr;
    ycbcr.Y = (float)(0.2989 * fr + 0.5866 * fg + 0.1145 * fb);
    ycbcr.Cb = (float)(-0.1687 * fr - 0.3313 * fg + 0.5000 * fb);
    ycbcr.Cr = (float)(0.5000 * fr - 0.4184 * fg - 0.0816 * fb);

    return ycbcr;
}

// The codes that I added end here.

void get_image_data(char *filename,struct ppm_file *image);
void write_image(char *filename,struct ppm_file *image);

// I do not have the enough space for the get_image_data and the write_image functions implementation. 
// If I will a solution for the space, I'll add the functions.

main()
{
    struct ppm_file resim;
    get_image_data("mandrill1.ppm",&resim);


    printf("pgmtype...=%c%c\n",resim.pheader->pgmtype1,resim.pheader->pgmtype2);
    printf("width...=%d\n",resim.pheader->pwidth);
    printf("height...=%d\n",resim.pheader->pheight);
    printf("max gray level...=%d\n",resim.pheader->pmax);

    write_image("pnr.ppm",&resim);
    return 0;
}

`

I've uploaded an image that shows my failures. I hope somebody can help me about that. That warning made me hopeless for the solution.

1 个答案:

答案 0 :(得分:-1)

&#34;错误消息表示&#34;指针值用于预期浮点值的位置&#34;。这条消息意味着什么?&#34;

这条消息正是它所说的。您使用了指针值,编译器期望浮点值。

根据您的定义

struct ppm_file
{
    struct ppm_header *pheader;
    unsigned char *rdata,*gdata,*bdata;
};

ppm_file.rdata,ppm_file.gdata和ppm_file.bdata是无符号的char指针。

但你这样做:

float fr = (float)rgb.rdata / 255;
float fg = (float)rgb.gdata / 255;
float fb = (float)rgb.bdata / 255;

所以你试图将一个unsigned char指针转换为float,将它除以255并将其指向float变量。那不行。

首先,您必须取消引用unsigned char指针。然后可以将生成的unsigned char值强制转换为浮点值,然后您可以按计划使用它。

float fr = (float) *rgb.rdata / 255;

应该这样做。

请确保您对C及其类型有更好的理解,以便继续。有很多好书和教程可供选择。