将字节数组(从内存)加载到LibPNG c ++

时间:2017-05-31 03:50:39

标签: c++ opengl libpng

而不是使用'png_init_io'我试图使用'png_set_read_fn'来提供一个自定义函数来从内存而不是从文件加载字节数组。

为什么会发生这种情况......似乎失败了:

if (setjmp(png_jmpbuf(png_ctx)) != 0)
{
    png_destroy_read_struct(&png_ctx, &info_ctx, NULL);
    free(img_data); free(row_data);

    return (1); /* libpng feedback (?) */
}

这是我的代码到目前为止,PNG数据位于文件的末尾

#ifndef __EMBEDDEDPNG_H__
#define __EMBEDDEDPNG_H__

#include <png.h>

typedef struct {
    const png_byte* data;
    const png_size_t size;
} DataHandle;

typedef struct {
    const DataHandle data;
    png_size_t offset;
} ReadDataHandle;

#include <GL/gl.h>

#include <algorithm>

#define PNG_SIG_BYTES (8) /* bytes in the PNG file signature. */

using byte = unsigned char;

#define PNG_RGBA_PIXEL_LIMIT (0x1000000)
static int
png_rgba_pixel_limit(png_uint_32 w, png_uint_32 h)
{
    double da;

    /* assert(w != 0 && h != 0); */

    if (w > PNG_RGBA_PIXEL_LIMIT || h > PNG_RGBA_PIXEL_LIMIT)
        return (1); /* since both (w) and (h) are non-zero. */

                    /* since an IEEE-754 double has a 53 bit mantissa, it can
                    * represent the maximum area: (w * h == 2^48) exactly. */

    da = ((double)w) * ((double)h);

    if (da > ((double)PNG_RGBA_PIXEL_LIMIT))
        return (1);

    return (0); /* the PNG image is within the pixel limit. */
}

class EmbeddedPNG
{
public:

    byte *Data;

public:
    size_t Read(byte* dest, const size_t byteCount);

    unsigned int Load(int &width, int &height);
};

void ReadDataFromInputStream(
    png_structp png_ptr, png_byte* raw_data, png_size_t read_length) {

    ReadDataHandle* handle = (ReadDataHandle*)png_get_io_ptr(png_ptr);
    const png_byte* png_src = handle->data.data + handle->offset;

    memcpy(raw_data, png_src, read_length);
    handle->offset += read_length;
}


size_t EmbeddedPNG::Read(byte* dest, const size_t byteCount)
{
    std::copy(Data + 0, Data + byteCount, dest);
    return byteCount;
}


unsigned int EmbeddedPNG::Load(int &width, int &height)
{

    png_byte magic[PNG_SIG_BYTES]; /* (signature byte buffer) */

    png_structp png_ctx;
    png_infop info_ctx;

    png_uint_32 img_width, img_height, row;
    png_byte img_depth, img_color_type;

    /* 'volatile' qualifier forces reload in setjmp cleanup: */

    png_byte *volatile img_data = NULL;
    png_bytep *volatile row_data = NULL;

    ;//*buf = NULL;


     /* it is assumed that 'longjmp' can be invoked within this
     * code to efficiently unwind resources for *all* errors. */

     /* PNG structures and resource unwinding: */

    if ((png_ctx = png_create_read_struct(
        PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL)
        return (1); /* ENOMEM (?) */

    if ((info_ctx = png_create_info_struct(png_ctx)) == NULL)
    {
        png_destroy_read_struct(&png_ctx, NULL, NULL);
        return (1); /* ENOMEM (?) */
    }

    if (setjmp(png_jmpbuf(png_ctx)) != 0)
    {
        png_destroy_read_struct(&png_ctx, &info_ctx, NULL);
        free(img_data); free(row_data);

        return (1); /* libpng feedback (?) */
    }


    /* check PNG file signature: */

    //if (fread(magic, (1), PNG_SIG_BYTES, fp) != PNG_SIG_BYTES)
    //  png_error(png_ctx, "invalid PNG file");

    Read(magic, PNG_SIG_BYTES);

    if (png_sig_cmp(magic, 0, PNG_SIG_BYTES))
        png_error(png_ctx, "invalid PNG file");


    /* set the input file stream and get the PNG image info: */

    //png_init_io(png_ctx, fp);

    ReadDataHandle a = ReadDataHandle{ { Data, 898 }, 0 };


    png_set_read_fn(png_ctx, &a, ReadDataFromInputStream);

    //png_set_read_fn(png_ptr, NULL, ReadDataFromInputStream);

    png_set_sig_bytes(png_ctx, PNG_SIG_BYTES);

    //////////////// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    png_read_info(png_ctx, info_ctx);

    img_width = png_get_image_width(png_ctx, info_ctx);
    img_height = png_get_image_height(png_ctx, info_ctx);

#if (1) /* PNG doesn't support zero area image? */

    if (img_width == 0 || img_height == 0)
        png_error(png_ctx, "zero area PNG image");
#endif

    if (png_rgba_pixel_limit(img_width, img_height))
        png_error(png_ctx, "PNG image exceeds pixel limits");

    img_depth = png_get_bit_depth(png_ctx, info_ctx);
    img_color_type = png_get_color_type(png_ctx, info_ctx);

    /* ignored image interlacing, compression and filtering. */

    /* force 8-bit color channels: */

    if (img_depth == 16)
        png_set_strip_16(png_ctx);

    else if (img_depth < 8)
        png_set_packing(png_ctx);

    /* force formats to RGB: */

    if (img_color_type != PNG_COLOR_TYPE_RGBA)
        png_set_expand(png_ctx);

    if (img_color_type == PNG_COLOR_TYPE_PALETTE)
        png_set_palette_to_rgb(png_ctx);

    if (img_color_type == PNG_COLOR_TYPE_GRAY)
        png_set_gray_to_rgb(png_ctx);

    if (img_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
        png_set_gray_to_rgb(png_ctx);

    /* add full opacity alpha channel if required: */

    if (img_color_type != PNG_COLOR_TYPE_RGBA)
        png_set_filler(png_ctx, 0xff, PNG_FILLER_AFTER);

    /* apply the output transforms before reading image data: */

    png_read_update_info(png_ctx, info_ctx);


    /* allocate RGBA image data: */

    img_data = (png_byte *)
        malloc((size_t)(img_width * img_height * (4)));

    if (img_data == NULL)
        png_error(png_ctx, "error allocating image buffer");

    /* allocate row pointers: */

    row_data = (png_bytep *)
        malloc((size_t)(img_height * sizeof(png_bytep)));

    if (row_data == NULL)
        png_error(png_ctx, "error allocating row pointers");

    /* set the row pointers and read the RGBA image data: */

    for (row = 0; row < img_height; row++)
        row_data[row] = img_data +
        (img_height - (row + 1)) * (img_width * (4));

    png_read_image(png_ctx, row_data);

    /* libpng and dynamic resource unwinding: */

    png_read_end(png_ctx, NULL);
    png_destroy_read_struct(&png_ctx, &info_ctx, NULL);

    free(row_data);

    //*w = (unsigned) img_width, *h = (unsigned) img_height;
    width = img_width;
    height = img_height;
    //*buf = img_data; /* (asserts png_byte is an unsigned char) */

    //Now generate the OpenGL texture object
    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Important!


    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
        GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)img_data);


        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);



    return texture;
}


class EmbeddedPNG_Icon_HandShake
    : public EmbeddedPNG
{
public:
    EmbeddedPNG_Icon_HandShake()
    {
        Data = new byte[898]{ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x08, 0x04, 0x00, 0x00, 0x00, 0xD9, 0x73, 0xB2, 0x7F, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4D, 0x41, 0x00, 0x00, 0xB1, 0x8F, 0x0B, 0xFC, 0x61, 0x05, 0x00, 0x00, 0x00, 0x20, 0x63, 0x48, 0x52, 0x4D, 0x00, 0x00, 0x7A, 0x26, 0x00, 0x00, 0x80, 0x84, 0x00, 0x00, 0xFA, 0x00, 0x00, 0x00, 0x80, 0xE8, 0x00, 0x00, 0x75, 0x30, 0x00, 0x00, 0xEA, 0x60, 0x00, 0x00, 0x3A, 0x98, 0x00, 0x00, 0x17, 0x70, 0x9C, 0xBA, 0x51, 0x3C, 0x00, 0x00, 0x00, 0x02, 0x62, 0x4B, 0x47, 0x44, 0x00, 0x00, 0xAA, 0x8D, 0x23, 0x32, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0D,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, 0xBC, 0xB0, 0xCE, 0xC7, 0x68, 0xB7, 0x2A, 0xA6, 0x1A, 0xD5, 0x9E, 0xE8, 0x74, 0xDC, 0xA8, 0x9B, 0xFA, 0xDD, 0xB1, 0x40, 0xBD, 0x2E, 0x83, 0xBA, 0x8D, 0x59, 0x3E, 0x65, 0x3C, 0x22, 0xC2, 0x4E, 0x19, 0x63, 0x7E, 0x37, 0xFB, 0xED, 0x7F, 0x44, 0x33, 0xDA, 0x35, 0x4D, 0x2B, 0x1E, 0x11, 0x61, 0x86, 0x45, 0xB9, 0xAF, 0xA0, 0x5F, 0xAF, 0x8C, 0xD7, 0xE6, 0x4E, 0x3B, 0x5E, 0x80, 0x35, 0x3A, 0xA7, 0xDD, 0x8E, 0xFF, 0x18, 0xFF, 0x1F, 0xE6, 0x5F, 0x1F, 0xA9, 0x98, 0xCC, 0x0D, 0x77, 0xF1, 0x32, 0x00, 0x00, 0x00, 0x25, 0x74, 0x45, 0x58, 0x74, 0x64, 0x61, 0x74, 0x65, 0x3A, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x00, 0x32, 0x30, 0x31, 0x37, 0x2D, 0x30, 0x35, 0x2D, 0x32, 0x39, 0x54, 0x32, 0x31, 0x3A, 0x35, 0x38, 0x3A, 0x30, 0x37, 0x2B, 0x30, 0x32, 0x3A, 0x30, 0x30, 0x19, 0xBB, 0x75, 0xB7, 0x00, 0x00, 0x00, 0x25, 0x74, 0x45, 0x58, 0x74, 0x64, 0x61, 0x74, 0x65, 0x3A, 0x6D, 0x6F, 0x64, 0x69, 0x66, 0x79, 0x00, 0x32, 0x30, 0x31, 0x37, 0x2D, 0x30, 0x35, 0x2D, 0x32, 0x39, 0x54, 0x32, 0x31, 0x3A, 0x35, 0x38, 0x3A, 0x30, 0x37, 0x2B, 0x30, 0x32, 0x3A, 0x30, 0x30, 0x68, 0xE6, 0xCD, 0x0B, 0x00, 0x00, 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x53, 0x6F, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x00, 0x77, 0x77, 0x77, 0x2E, 0x69, 0x6E, 0x6B, 0x73, 0x63, 0x61, 0x70, 0x65, 0x2E, 0x6F, 0x72, 0x67, 0x9B, 0xEE, 0x3C, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 };
    }
};

#endif

1 个答案:

答案 0 :(得分:2)

不要打电话

png_set_sig_bytes(png_ctx, PNG_SIG_BYTES);

这表示libpng跳过读取签名字节,当您开始读取png数据时导致offset出错。 Offset指向零,libpng假定它已经是8(或者更确切地说,流中没有签名)。

来源:我是通过调试器运行的。