Exemplo n.º 1
0
/**
 * @brief read a PNG file into a 8bit integer array, converted to gray
 *
 * See read_png_u8() for details.
 */
unsigned char *read_png_u8_gray(const char *fname, size_t * nx, size_t * ny)
{
    size_t nc;
    unsigned char *img;

    /* read the image */
    img = (unsigned char *) read_png_raw(fname, nx, ny, &nc,
                                         PNG_TRANSFORM_STRIP_ALPHA,
                                         IO_PNG_U8);
    if (NULL == img)
        /* error */
        return NULL;
    if (1 == nc)
        /* already gray */
        return img;
    else
    {
        /* convert to gray */
        unsigned char *ptr_r, *ptr_g, *ptr_b, *ptr_gray, *ptr_end;

        /*
         * RGB->gray conversion
         * Y = (6969 * R + 23434 * G + 2365 * B)/32768
         * integer approximation of
         * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
         */
        ptr_r = img;
        ptr_g = img + *nx * *ny;
        ptr_b = img + 2 * *nx * *ny;
        ptr_gray = img;
        ptr_end = ptr_gray + *nx * *ny;
        while (ptr_gray < ptr_end)
            *ptr_gray++ = (unsigned char) (6969 * *ptr_r++
                                           + 23434 * *ptr_g++
                                           + 2365 * *ptr_b++) / 32768;
        /* resize and return the image */
        img = (unsigned char*) realloc(img, *nx * *ny * sizeof(unsigned char));
        return img;
    }
}
Exemplo n.º 2
0
unsigned char *read_png_u8_rgb(const char *fname, size_t * nxp, size_t * nyp)
{
    size_t nc;
    unsigned char *img;

    /* read the image */
    img = (unsigned char *) read_png_raw(fname, nxp, nyp, &nc,
                                         PNG_TRANSFORM_STRIP_ALPHA,
                                         IO_PNG_U8);
    if (NULL == img)
        /* error */
        return NULL;
    if (3 == nc)
        /* already RGB */
        return img;
    else
    {
        /* convert to RGB */
        unsigned char *ptr_r, *ptr_g, *ptr_b, *ptr_end;

        /* resize the image */
        img = realloc(img, 3 * *nxp * *nyp * sizeof(unsigned char));

        /* gray->RGB conversion */
        ptr_r = img;
        ptr_end = ptr_r + *nxp * *nyp;
        ptr_g = img + *nxp * *nyp;
        ptr_b = img + 2 * *nxp * *nyp;
        while (ptr_r < ptr_end)
        {
            *ptr_g++ = *ptr_r;
            *ptr_b++ = *ptr_r++;
        }
        return img;
    }
}
Exemplo n.º 3
0
/**
 * @brief read a PNG file into a 32bit float array
 *
 * The array contains the deinterlaced channels.
 * 1, 2, 4 and 8bit images are converted to float values
 * between 0. and 1., 3., 15. or 255.
 * 16bit images are also downscaled to 8bit before conversion.
 *
 * @param fname PNG file name
 * @param nx, ny, nc pointers to variables to be filled with the number of
 *        columns, lines and channels of the image
 * @return pointer to an allocated unsigned char array of pixels,
 *         or NULL if an error happens
 */
float *read_png_f32(const char *fname, size_t * nx, size_t * ny, size_t * nc)
{
    /* read the image as float */
    return (float *) read_png_raw(fname, nx, ny, nc,
                                  PNG_TRANSFORM_IDENTITY, IO_PNG_F32);
}