image scaling with C

Posted by sa125 on Stack Overflow See other posts from Stack Overflow or by sa125
Published on 2010-03-06T15:43:28Z Indexed on 2010/05/08 10:58 UTC
Read the original article Hit count: 421

Hi - I'm trying to read an image file and scale it by multiplying each byte by a scale its pixel levels by some absolute factor. I'm not sure I'm doing it right, though -

void scale_file(char *infile, char *outfile, float scale)
{
    // open files for reading
    FILE *infile_p = fopen(infile, 'r');
    FILE *outfile_p = fopen(outfile, 'w');

    // init data holders
    char *data;
    char *scaled_data;

    // read each byte, scale and write back
    while ( fread(&data, 1, 1, infile_p) != EOF )
    {
        *scaled_data = (*data) * scale;
        fwrite(&scaled_data, 1, 1, outfile);
    }

    // close files
    fclose(infile_p);
    fclose(outfile_p);
}

What gets me is how to do each byte multiplication (scale is 0-1.0 float) - I'm pretty sure I'm either reading it wrong or missing something big. Also, data is assumed to be unsigned (0-255). Please don't judge my poor code :)

thanks

© Stack Overflow or respective owner

Related posts about image-manipulation

Related posts about c