1 /****************************************************************************
2  *
3  * 		pngUtils.h: Portable Network Graphics format utilities
4  *      This is part of the yafray package
5  *      Copyright (C) 2010 Rodrigo Placencia Vazquez
6  *
7  *      This library is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU Lesser General Public
9  *      License as published by the Free Software Foundation; either
10  *      version 2.1 of the License, or (at your option) any later version.
11  *
12  *      This library is distributed in the hope that it will be useful,
13  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *      Lesser General Public License for more details.
16  *
17  *      You should have received a copy of the GNU Lesser General Public
18  *      License along with this library; if not, write to the Free Software
19  *      Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22 
23 __BEGIN_YAFRAY
24 
25 #define inv8  0.00392156862745098039 // 1 / 255
26 #define inv16 0.00001525902189669642 // 1 / 65535
27 
28 class pngDataReader_t
29 {
30 public:
31 
pngDataReader_t(const yByte * d,size_t s)32 	pngDataReader_t(const yByte *d, size_t s):size(s), cursor(0)
33 	{
34 		data = new yByte[size];
35 		for(size_t i = 0;i<size;i++)
36 		{
37 			data[i] = d[i];
38 		}
39 	}
40 
~pngDataReader_t()41 	~pngDataReader_t()
42 	{
43 		delete [] data;
44 		data = nullptr;
45 	}
46 
read(yByte * buf,size_t s)47 	size_t read(yByte* buf, size_t s)
48 	{
49 		if(cursor > size) return 0;
50 		size_t i;
51 
52 		for(i = 0;i < s && cursor < size;cursor++, i++)
53 		{
54 			buf[i] = data[cursor];
55 		}
56 
57 		return i;
58 	}
59 
60 private:
61 	yByte *data;
62 	size_t size;
63 	size_t cursor;
64 };
65 
readFromMem(png_structp pngPtr,png_bytep buffer,png_size_t bytesToRead)66 void readFromMem(png_structp pngPtr, png_bytep buffer, png_size_t bytesToRead)
67 {
68    pngDataReader_t *img = (pngDataReader_t*) png_get_io_ptr(pngPtr);
69 
70    if(img == nullptr) png_error(pngPtr, "The image data pointer is null!!");
71 
72    if(img->read((yByte*)buffer, (size_t)bytesToRead) < bytesToRead) png_warning(pngPtr, "EOF Found while reading image data");
73 }
74 
75 __END_YAFRAY
76