From d13190990ef4d9ed41b833eba250cc409c2522bf Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Mon, 27 Aug 2012 11:50:19 -0400 Subject: [PATCH] Implement read callback for libpng --- IO/ImageLoaderPNG.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/IO/ImageLoaderPNG.cpp b/IO/ImageLoaderPNG.cpp index 77231eb..efe1f80 100644 --- a/IO/ImageLoaderPNG.cpp +++ b/IO/ImageLoaderPNG.cpp @@ -5,6 +5,10 @@ #include +static void ReportError(const char *msg) { + fprintf(stderr, "ERROR: ImageLoaderPNG -- %s\n", msg); +} + class PNGStreamReader { public: static void ReadDataFromStream( @@ -12,23 +16,30 @@ public: png_bytep outBytes, png_size_t byteCountToRead ) { + png_voidp io_ptr = png_get_io_ptr( png_ptr ); + if( io_ptr == NULL ) { + ReportError("Read callback had invalid io pointer.\n"); + return; + } - + ImageLoaderPNG &loader = *(ImageLoaderPNG *)(io_ptr); + + const unsigned char *stream = &(loader.m_RawData[loader.m_StreamPosition]); + memcpy(outBytes, stream, byteCountToRead); + + loader.m_StreamPosition += byteCountToRead; } }; ImageLoaderPNG::ImageLoaderPNG(const unsigned char *rawData) : ImageLoader(rawData) + , m_StreamPosition(8) // We start at position 8 because of PNG header. { } ImageLoaderPNG::~ImageLoaderPNG() { } -static void ReportError(const char *msg) { - fprintf(stderr, "ERROR: ImageLoaderPNG -- %s\n", msg); -} - bool ImageLoaderPNG::ReadData() { const int kNumSigBytesToRead = 8;