在 C__ 中将 PPM 从 RGB 转换为 HSL
Converting a PPM from RGB to HSL in C__
我有一些 C 代码示例。我需要进行直方图均衡。但是,我需要一步步向前。我被困在第一步。第一步是将文件从 RGB 转换为 YCbCr.So,我将与您分享代码。我拥有的所有代码都不适合这些区域。另外,我添加了一张显示我失败的图片。我想知道我哪里错了。我希望有人能给我指路明灯。错误消息说 "pointer value used where a floating point value was expected"。这条消息是什么意思?
`
#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.
"The error message says that "在需要浮点值的地方使用了指针值。这条消息是什么意思?
消息的意思与它所说的完全一致。您在编译器需要浮点值的地方使用了指针值。
根据你的定义
struct ppm_file
{
struct ppm_header *pheader;
unsigned char *rdata,*gdata,*bdata;
};
ppm_file.rdata、ppm_file.gdata 和 ppm_file.bdata 是无符号字符指针。
但是你这样做:
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 及其类型有更好的理解。有很多好书和教程可用。
我有一些 C 代码示例。我需要进行直方图均衡。但是,我需要一步步向前。我被困在第一步。第一步是将文件从 RGB 转换为 YCbCr.So,我将与您分享代码。我拥有的所有代码都不适合这些区域。另外,我添加了一张显示我失败的图片。我想知道我哪里错了。我希望有人能给我指路明灯。错误消息说 "pointer value used where a floating point value was expected"。这条消息是什么意思?
`
#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.
"The error message says that "在需要浮点值的地方使用了指针值。这条消息是什么意思?
消息的意思与它所说的完全一致。您在编译器需要浮点值的地方使用了指针值。
根据你的定义
struct ppm_file
{
struct ppm_header *pheader;
unsigned char *rdata,*gdata,*bdata;
};
ppm_file.rdata、ppm_file.gdata 和 ppm_file.bdata 是无符号字符指针。
但是你这样做:
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 及其类型有更好的理解。有很多好书和教程可用。