Search code examples
cimage-processinghistogramrgbppm

Converting a PPM from RGB to HSL in C__


I have some code samples in C. I need to do the histogram equalization. However, I need to forward step by step. I was stucked in first step. First step is to convert the file from RGB to YCbCr.So, I will share the codes with you. All codes that I've is not gonna fit into these area. Also, I've added a picture that shows my failure. I wonder where I'm wrong. I hope somebody can show me the light. The error message says that "pointer value used where a floating point value was expected". What does it mean with this message?

`

#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.


Solution

  • "The error message says that "pointer value used where a floating point value was expected". What does it mean with this message?"

    The message means exactly what it says. You used a pointer value where the compiler expected a floating point value.

    According to your definition

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

    ppm_file.rdata, ppm_file.gdata and ppm_file.bdata are unsigned char pointers.

    But you do this:

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

    so you're trying to cast an unsigned char pointer to float, divide it by 255 and asign that to a float variable. That is not going to work.

    First you have to dereference the unsigned char pointer. The resulting unsigned char value can then be cast to a float value, which you can then use as planned.

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

    should do the trick.

    Please make sure you have a better understanding of C and it's types befor you continue. There are plenty of good books and tutorials available.