25
#include "NuxCore/NKernel.h"
29
#include "BitmapFormats.h"
39
#define FAILMSGANDRETURN \
41
nuxAssertMsg(0, TEXT("[PNG Read Error] Cannot open %s for read."), FileNameAnsi); \
46
#define PNG_FREERESOURCE if(hglobal) FreeResource(hglobal);
47
#define PNG_DECLARE_RESVAR \
50
HGLOBAL hglobal = NULL;\
52
#define PNG_SETREADFN \
53
if(!fp) png_set_read_fn(png_ptr, (void *)resptr, png_read_resource_fn); else
54
#define PNG_READHEADER \
57
memcpy(sig, resptr, 8);\
58
resptr = (void*)((char*)resptr + 8);\
63
#define PNG_FREERESOURCE
64
#define PNG_DECLARE_RESVAR
66
#define PNG_READHEADER
75
struct png_resource_access
77
png_resource_access() : hModule(NULL), res_type_id(0) {}
79
std::string res_type_name;
80
unsigned long res_type_id;
82
png_resource_access png_resource;
86
void png_read_resource_fn(png_structp png_ptr, png_bytep data, png_size_t leng);
89
void set_png_module_handle(unsigned long hM)
91
png_resource.hModule = (HMODULE)hM;
93
void set_png_module_restypename(const char * restypename)
95
if(HIWORD(restypename))
97
png_resource.res_type_name = restypename;
98
png_resource.res_type_id = 0;
102
//resource.res_type_name.clear(); should exist (cf STL Doc)
103
png_resource.res_type_id = (unsigned long)restypename;
110
HRSRC get_resource(const char * filename)
114
unsigned int count = strlen(filename);
115
wchar_t *wfilename = new wchar_t[count];
116
mbstowcs(wfilename, filename, count);
118
count = png_resource.res_type_name.size();
119
wchar_t *wres_type_name = new wchar_t[count];
120
mbstowcs(wres_type_name, png_resource.res_type_name.c_str(), count);
122
HRes = FindResource(png_resource.hModule, wfilename,
123
png_resource.res_type_id ? (LPCWSTR)png_resource.res_type_id : wres_type_name);
126
delete [] wres_type_name;
128
HRes = FindResource(png_resource.hModule, filename,
129
png_resource.res_type_id ? (LPCSTR)png_resource.res_type_id : png_resource.res_type_name.c_str());
135
NBitmapData* read_png_rgba(const TCHAR* filename)
137
//--------- Resource stuff
142
int bit_depth, color_type;
144
png_uint_32 channels, row_bytes;
145
png_structp png_ptr = 0;
146
png_infop info_ptr = 0;
148
ANSICHAR* FileNameAnsi = (ANSICHAR*) TCHAR_TO_ANSI(filename);
151
nuxAssertMsg(0, TEXT("[read_png_rgba] Incorrect file name: %s."), filename);
157
struct _stat file_info;
159
struct stat file_info;
163
// System call: check if the file exist
165
if(_stat(FileNameAnsi, &file_info) != 0)
167
if(stat(FileNameAnsi, &file_info) != 0)
170
nuxAssert(TEXT("[read_png_rgba] File not found: %s."));
174
#if (defined INL_VISUAL_STUDIO_2005) || (defined INL_VISUAL_STUDIO_2008)
175
fopen_s(&fp, FileNameAnsi, "rb");
177
fp = fopen(FileNameAnsi, "rb");
183
// Try resource access
184
HRes = get_resource(FileNameAnsi);
188
hglobal = LoadResource(png_resource.hModule, HRes);
191
ressz = SizeofResource(png_resource.hModule, HRes);
192
resptr = (char*)LockResource(hglobal);
200
// first check the eight byte PNG signature
201
PNG_READHEADER fread(sig, 1, 8, fp);
202
if (!png_check_sig(sig, 8))
210
// start back here!!!!
212
// create the two png(-info) structures
214
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
222
info_ptr = png_create_info_struct(png_ptr);
225
png_destroy_read_struct(&png_ptr, 0, 0);
230
// initialize the png structure
231
PNG_SETREADFN png_init_io(png_ptr, fp);
233
png_set_sig_bytes(png_ptr, 8);
235
// read all PNG info up to image data
236
png_read_info(png_ptr, info_ptr);
238
// get width, height, bit-depth and color-type
239
unsigned long width, height;
240
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
242
// expand images of all color-type and bit-depth to 3x8 bit RGB images
243
// let the library process things like alpha, transparency, background
245
if (bit_depth == 16) png_set_strip_16(png_ptr);
246
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
247
if (bit_depth < 8) png_set_expand(png_ptr);
248
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
249
if (color_type == PNG_COLOR_TYPE_GRAY ||
250
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
251
png_set_gray_to_rgb(png_ptr);
253
// if required set gamma conversion
254
if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, (double) 2.2, gamma);
256
// after the transformations have been registered update info_ptr data
257
png_read_update_info(png_ptr, info_ptr);
259
// get again width, height and the new bit-depth and color-type
260
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
263
// row_bytes is the width x number of channels
264
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
265
channels = png_get_channels(png_ptr, info_ptr);
269
//nuxAssertMsg(0, TEXT("[read_png_rgba] Unable to open image of type RGB %s using read_png_rgba()."), filename);
270
//nuxAssertMsg(0, TEXT("[read_png_rgba] Try using read_png_rgb() instead!"));
275
// now we can allocate memory to store the image
276
png_byte * img = new png_byte[row_bytes * height];
277
// and allocate memory for an array of row-pointers
278
png_byte ** row = new png_byte * [height];
281
// set the individual row-pointers to point at the correct offsets
282
for (unsigned int i = 0; i < height; i++)
283
row[i] = img + i * row_bytes;
285
// now we can go ahead and just read the whole image
286
png_read_image(png_ptr, row);
288
// read the additional chunks in the PNG file (not really needed)
289
png_read_end(png_ptr, NULL);
291
//image = array2<vec4ub>(w, h);
292
NTextureData* TextureObjectData = new NTextureData(BITFMT_R8G8B8A8, width, height, 1);
294
for(unsigned int i=0; i < width; i++)
295
for(unsigned int j=0; j < height; j++)
297
BYTE* png_data_pointer = img + ((height-j-1)*row_bytes + i * 4);
299
(*(png_data_pointer + 3) << 24) | // a
300
(*(png_data_pointer + 2) << 16) | // b
301
(*(png_data_pointer + 1) << 8) | // g
302
*(png_data_pointer + 0); // r
304
TextureObjectData->GetSurface(0).Write32b(i, j, value); // = vec4ub(img + ((h-j-1)*row_bytes + i * 4));
311
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
313
if(TextureObjectData)
314
TextureObjectData->GetSurface(0).FlipVertical();
319
return TextureObjectData;
322
NBitmapData* read_png_rgb(const TCHAR* filename)
324
//--------- Resource stuff
329
int bit_depth, color_type;
331
png_uint_32 channels, row_bytes;
332
png_structp png_ptr = 0;
333
png_infop info_ptr = 0;
335
// open the PNG input file
336
ANSICHAR* FileNameAnsi = (ANSICHAR*) TCHAR_TO_ANSI(filename);
339
nuxAssertMsg(0, TEXT("[read_png_rgb] Incorrect file name: %s."), filename);
344
struct _stat file_info;
346
struct stat file_info;
350
// System call: check if the file exist
352
if(_stat(FileNameAnsi, &file_info) != 0)
354
if(stat(FileNameAnsi, &file_info) != 0)
357
nuxAssert(TEXT("[read_png_rgb] File not found: %s."));
361
#if (defined INL_VISUAL_STUDIO_2005) || (defined INL_VISUAL_STUDIO_2008)
362
fopen_s(&fp, FileNameAnsi, "rb");
364
fp = fopen(FileNameAnsi, "rb");
370
// Try resource access
371
HRes = get_resource(FileNameAnsi);
375
hglobal = LoadResource(png_resource.hModule, HRes);
378
ressz = SizeofResource(png_resource.hModule, HRes);
379
resptr = (char*)LockResource(hglobal);
387
// first check the eight byte PNG signature
388
PNG_READHEADER fread(sig, 1, 8, fp);
389
if (!png_check_sig(sig, 8))
396
// start back here!!!!
398
// create the two png(-info) structures
400
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
407
info_ptr = png_create_info_struct(png_ptr);
410
png_destroy_read_struct(&png_ptr, 0, 0);
415
// initialize the png structure
416
PNG_SETREADFN png_init_io(png_ptr, fp);
418
png_set_sig_bytes(png_ptr, 8);
420
// read all PNG info up to image data
421
png_read_info(png_ptr, info_ptr);
423
// get width, height, bit-depth and color-type
425
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
427
// expand images of all color-type and bit-depth to 3x8 bit RGB images
428
// let the library process things like alpha, transparency, background
430
if (bit_depth == 16) png_set_strip_16(png_ptr);
431
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
432
if (bit_depth < 8) png_set_expand(png_ptr);
433
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
434
if (color_type == PNG_COLOR_TYPE_GRAY ||
435
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
436
png_set_gray_to_rgb(png_ptr);
437
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
438
png_set_strip_alpha(png_ptr);
440
// if required set gamma conversion
441
if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, (double) 2.2, gamma);
443
// after the transformations have been registered update info_ptr data
444
png_read_update_info(png_ptr, info_ptr);
446
// get again width, height and the new bit-depth and color-type
447
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
450
// row_bytes is the width x number of channels
451
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
452
channels = png_get_channels(png_ptr, info_ptr);
456
//nuxAssertMsg(0, TEXT("[read_png_rgba] Unable to open image of type RGBA %s using read_png_rgb()."), filename);
457
//nuxAssertMsg(0, TEXT("[read_png_rgba] Try using read_png_rgba() instead!"));
462
// now we can allocate memory to store the image
464
png_byte * img = new png_byte[row_bytes * h];
466
// and allocate memory for an array of row-pointers
468
png_byte ** row = new png_byte * [h];
471
// set the individual row-pointers to point at the correct offsets
473
for (unsigned int i = 0; i < h; i++)
474
row[i] = img + i * row_bytes;
476
// now we can go ahead and just read the whole image
478
png_read_image(png_ptr, row);
480
// read the additional chunks in the PNG file (not really needed)
482
png_read_end(png_ptr, NULL);
484
//image = array2<vec3ub>(w, h);
485
NTextureData* TextureObjectData = new NTextureData(BITFMT_R8G8B8, w, h, 1);
487
for(unsigned int i=0; i < w; i++)
488
for(unsigned int j=0; j < h; j++)
490
BYTE* png_data_pointer = img + ((h-j-1)*row_bytes + i * 3);
493
(*(png_data_pointer + 2) << 16) | //r
494
(*(png_data_pointer + 1) << 8) | //g
495
*(png_data_pointer + 0); //b
497
TextureObjectData->GetSurface(0).Write24b(i, j, value); // vec3ub(img + ((h-j-1)*row_bytes + i * 3));
505
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
507
if(TextureObjectData)
508
TextureObjectData->GetSurface(0).FlipVertical();
512
return TextureObjectData;
516
////FIXME: LIBPNG expands to RGB and only R is fetched...
517
//bool read_png_grey(const char * filename, glh::array2<unsigned char> & image)
519
// //--------- Resource stuff
520
// PNG_DECLARE_RESVAR
524
// int bit_depth, color_type;
526
// png_uint_32 channels, row_bytes;
527
// png_structp png_ptr = 0;
528
// png_infop info_ptr = 0;
531
// // This is the SDK default path...
532
// if(path.path.size() < 1)
534
// path.path.push_back(""); // added by Ashu, for case where fully qualified path is given
535
// path.path.push_back(".");
536
// path.path.push_back("../../../MEDIA/textures/1D");
537
// path.path.push_back("../../../../MEDIA/textures/1D");
538
// path.path.push_back("../../../../../../../MEDIA/textures/1D");
539
// path.path.push_back("../../../MEDIA/textures/2D");
540
// path.path.push_back("../../../../MEDIA/textures/2D");
541
// path.path.push_back("../../../../../../../MEDIA/textures/2D");
542
// path.path.push_back("../../../MEDIA/textures/rectangles");
543
// path.path.push_back("../../../../MEDIA/textures/rectangles");
544
// path.path.push_back("../../../../../../../MEDIA/textures/rectangles");
545
// path.path.push_back("../../../MEDIA/textures/cubemaps");
546
// path.path.push_back("../../../../MEDIA/textures/cubemaps");
547
// path.path.push_back("../../../../../../../MEDIA/textures/cubemaps");
548
// path.path.push_back("../../../MEDIA/textures/3D");
549
// path.path.push_back("../../../../MEDIA/textures/3D");
550
// path.path.push_back("../../../../../../../MEDIA/textures/3D");
553
// // open the PNG input file
554
// if (!filename) return false;
556
// if (!(fp = path.fopen(filename)))
557
//#if defined(_WIN32)
559
// // Try resource access
560
// HRes = get_resource(filename);
564
// hglobal = LoadResource(png_resource.hModule, HRes);
567
// ressz = SizeofResource(png_resource.hModule, HRes);
568
// resptr = (char*)LockResource(hglobal);
576
// // first check the eight byte PNG signature
577
// PNG_READHEADER fread(sig, 1, 8, fp);
578
// if (!png_check_sig(sig, 8)) {
579
// if(fp) fclose(fp);
585
// // start back here!!!!
587
// // create the two png(-info) structures
589
// png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
591
// if(fp) fclose(fp);
596
// info_ptr = png_create_info_struct(png_ptr);
599
// png_destroy_read_struct(&png_ptr, 0, 0);
600
// if(fp) fclose(fp);
604
// // initialize the png structure
605
// PNG_SETREADFN png_init_io(png_ptr, fp);
607
// png_set_sig_bytes(png_ptr, 8);
609
// // read all PNG info up to image data
610
// png_read_info(png_ptr, info_ptr);
612
// // get width, height, bit-depth and color-type
613
// unsigned long w, h;
614
// png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
616
// // expand images of all color-type and bit-depth to 3x8 bit RGB images
617
// // let the library process things like alpha, transparency, background
619
// if (bit_depth == 16) png_set_strip_16(png_ptr);
620
// if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
621
// if (bit_depth < 8) png_set_expand(png_ptr);
622
// if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
623
// if (color_type == PNG_COLOR_TYPE_GRAY ||
624
// color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
625
// png_set_gray_to_rgb(png_ptr);
627
// // if required set gamma conversion
628
// if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, (double) 2.2, gamma);
630
// // after the transformations have been registered update info_ptr data
631
// png_read_update_info(png_ptr, info_ptr);
633
// // get again width, height and the new bit-depth and color-type
634
// png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
637
// // row_bytes is the width x number of channels
638
// row_bytes = png_get_rowbytes(png_ptr, info_ptr);
639
// channels = png_get_channels(png_ptr, info_ptr);
641
// // now we can allocate memory to store the image
643
// png_byte * img = new png_byte[row_bytes * h];
645
// // and allocate memory for an array of row-pointers
647
// png_byte ** row = new png_byte * [h];
650
// // set the individual row-pointers to point at the correct offsets
652
// for (unsigned int i = 0; i < h; i++)
653
// row[i] = img + i * row_bytes;
655
// // now we can go ahead and just read the whole image
657
// png_read_image(png_ptr, row);
659
// // read the additional chunks in the PNG file (not really needed)
661
// png_read_end(png_ptr, NULL);
663
// image = array2<unsigned char>(w, h);
666
// for(unsigned int i=0; i < w; i++)
667
// for(unsigned int j=0; j < h; j++)
668
// { image(i,j) = *(img + ((h-j-1)*row_bytes + i * 3)); }
674
// png_destroy_read_struct(&png_ptr, &info_ptr, 0);
676
// if(fp) fclose (fp);
682
void png_read_resource_fn(png_structp png_ptr, png_bytep data, png_size_t leng)
684
png_bytep src = (png_bytep)png_ptr->io_ptr;
686
for(unsigned int i=0; i<leng; i++)
688
png_ptr->io_ptr = (void*)src;
25
#include "NuxCore/NKernel.h"
29
#include "BitmapFormats.h"
39
#define FAILMSGANDRETURN \
41
nuxAssertMsg(0, TEXT("[PNG Read Error] Cannot open %s for read."), FileNameAnsi); \
46
#define PNG_FREERESOURCE if(hglobal) FreeResource(hglobal);
47
#define PNG_DECLARE_RESVAR \
50
HGLOBAL hglobal = NULL;\
52
#define PNG_SETREADFN \
53
if(!fp) png_set_read_fn(png_ptr, (void *)resptr, png_read_resource_fn); else
54
#define PNG_READHEADER \
57
memcpy(sig, resptr, 8);\
58
resptr = (void*)((char*)resptr + 8);\
63
#define PNG_FREERESOURCE
64
#define PNG_DECLARE_RESVAR
66
#define PNG_READHEADER
75
struct png_resource_access
77
png_resource_access() : hModule(NULL), res_type_id(0) {}
79
std::string res_type_name;
80
unsigned long res_type_id;
82
png_resource_access png_resource;
86
void png_read_resource_fn(png_structp png_ptr, png_bytep data, png_size_t leng);
89
void set_png_module_handle(unsigned long hM)
91
png_resource.hModule = (HMODULE)hM;
93
void set_png_module_restypename(const char * restypename)
95
if(HIWORD(restypename))
97
png_resource.res_type_name = restypename;
98
png_resource.res_type_id = 0;
102
//resource.res_type_name.clear(); should exist (cf STL Doc)
103
png_resource.res_type_id = (unsigned long)restypename;
110
HRSRC get_resource(const char * filename)
114
unsigned int count = strlen(filename);
115
wchar_t *wfilename = new wchar_t[count];
116
mbstowcs(wfilename, filename, count);
118
count = png_resource.res_type_name.size();
119
wchar_t *wres_type_name = new wchar_t[count];
120
mbstowcs(wres_type_name, png_resource.res_type_name.c_str(), count);
122
HRes = FindResource(png_resource.hModule, wfilename,
123
png_resource.res_type_id ? (LPCWSTR)png_resource.res_type_id : wres_type_name);
126
delete [] wres_type_name;
128
HRes = FindResource(png_resource.hModule, filename,
129
png_resource.res_type_id ? (LPCSTR)png_resource.res_type_id : png_resource.res_type_name.c_str());
135
NBitmapData* read_png_rgba(const TCHAR* filename)
137
//--------- Resource stuff
142
int bit_depth, color_type;
144
png_uint_32 channels, row_bytes;
145
png_structp png_ptr = 0;
146
png_infop info_ptr = 0;
148
ANSICHAR* FileNameAnsi = (ANSICHAR*) TCHAR_TO_ANSI(filename);
151
nuxAssertMsg(0, TEXT("[read_png_rgba] Incorrect file name: %s."), filename);
157
struct _stat file_info;
159
struct stat file_info;
163
// System call: check if the file exist
165
if(_stat(FileNameAnsi, &file_info) != 0)
167
if(stat(FileNameAnsi, &file_info) != 0)
170
nuxAssert(TEXT("[read_png_rgba] File not found: %s."));
174
#if (defined INL_VISUAL_STUDIO_2005) || (defined INL_VISUAL_STUDIO_2008)
175
fopen_s(&fp, FileNameAnsi, "rb");
177
fp = fopen(FileNameAnsi, "rb");
183
// Try resource access
184
HRes = get_resource(FileNameAnsi);
188
hglobal = LoadResource(png_resource.hModule, HRes);
191
ressz = SizeofResource(png_resource.hModule, HRes);
192
resptr = (char*)LockResource(hglobal);
200
// first check the eight byte PNG signature
201
PNG_READHEADER fread(sig, 1, 8, fp);
202
if (!png_check_sig(sig, 8))
210
// start back here!!!!
212
// create the two png(-info) structures
214
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
222
info_ptr = png_create_info_struct(png_ptr);
225
png_destroy_read_struct(&png_ptr, 0, 0);
230
// initialize the png structure
231
PNG_SETREADFN png_init_io(png_ptr, fp);
233
png_set_sig_bytes(png_ptr, 8);
235
// read all PNG info up to image data
236
png_read_info(png_ptr, info_ptr);
238
// get width, height, bit-depth and color-type
239
unsigned long width, height;
240
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
242
// expand images of all color-type and bit-depth to 3x8 bit RGB images
243
// let the library process things like alpha, transparency, background
245
if (bit_depth == 16) png_set_strip_16(png_ptr);
246
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
247
if (bit_depth < 8) png_set_expand(png_ptr);
248
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
249
if (color_type == PNG_COLOR_TYPE_GRAY ||
250
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
251
png_set_gray_to_rgb(png_ptr);
253
// if required set gamma conversion
254
if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, (double) 2.2, gamma);
256
// after the transformations have been registered update info_ptr data
257
png_read_update_info(png_ptr, info_ptr);
259
// get again width, height and the new bit-depth and color-type
260
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
263
// row_bytes is the width x number of channels
264
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
265
channels = png_get_channels(png_ptr, info_ptr);
269
//nuxAssertMsg(0, TEXT("[read_png_rgba] Unable to open image of type RGB %s using read_png_rgba()."), filename);
270
//nuxAssertMsg(0, TEXT("[read_png_rgba] Try using read_png_rgb() instead!"));
275
// now we can allocate memory to store the image
276
png_byte * img = new png_byte[row_bytes * height];
277
// and allocate memory for an array of row-pointers
278
png_byte ** row = new png_byte * [height];
281
// set the individual row-pointers to point at the correct offsets
282
for (unsigned int i = 0; i < height; i++)
283
row[i] = img + i * row_bytes;
285
// now we can go ahead and just read the whole image
286
png_read_image(png_ptr, row);
288
// read the additional chunks in the PNG file (not really needed)
289
png_read_end(png_ptr, NULL);
291
//image = array2<vec4ub>(w, h);
292
NTextureData* TextureObjectData = new NTextureData(BITFMT_R8G8B8A8, width, height, 1);
294
for(unsigned int i=0; i < width; i++)
295
for(unsigned int j=0; j < height; j++)
297
BYTE* png_data_pointer = img + ((height-j-1)*row_bytes + i * 4);
299
(*(png_data_pointer + 3) << 24) | // a
300
(*(png_data_pointer + 2) << 16) | // b
301
(*(png_data_pointer + 1) << 8) | // g
302
*(png_data_pointer + 0); // r
304
TextureObjectData->GetSurface(0).Write32b(i, j, value); // = vec4ub(img + ((h-j-1)*row_bytes + i * 4));
311
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
313
if(TextureObjectData)
314
TextureObjectData->GetSurface(0).FlipVertical();
319
return TextureObjectData;
322
NBitmapData* read_png_rgb(const TCHAR* filename)
324
//--------- Resource stuff
329
int bit_depth, color_type;
331
png_uint_32 channels, row_bytes;
332
png_structp png_ptr = 0;
333
png_infop info_ptr = 0;
335
// open the PNG input file
336
ANSICHAR* FileNameAnsi = (ANSICHAR*) TCHAR_TO_ANSI(filename);
339
nuxAssertMsg(0, TEXT("[read_png_rgb] Incorrect file name: %s."), filename);
344
struct _stat file_info;
346
struct stat file_info;
350
// System call: check if the file exist
352
if(_stat(FileNameAnsi, &file_info) != 0)
354
if(stat(FileNameAnsi, &file_info) != 0)
357
nuxAssert(TEXT("[read_png_rgb] File not found: %s."));
361
#if (defined INL_VISUAL_STUDIO_2005) || (defined INL_VISUAL_STUDIO_2008)
362
fopen_s(&fp, FileNameAnsi, "rb");
364
fp = fopen(FileNameAnsi, "rb");
370
// Try resource access
371
HRes = get_resource(FileNameAnsi);
375
hglobal = LoadResource(png_resource.hModule, HRes);
378
ressz = SizeofResource(png_resource.hModule, HRes);
379
resptr = (char*)LockResource(hglobal);
387
// first check the eight byte PNG signature
388
PNG_READHEADER fread(sig, 1, 8, fp);
389
if (!png_check_sig(sig, 8))
396
// start back here!!!!
398
// create the two png(-info) structures
400
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
407
info_ptr = png_create_info_struct(png_ptr);
410
png_destroy_read_struct(&png_ptr, 0, 0);
415
// initialize the png structure
416
PNG_SETREADFN png_init_io(png_ptr, fp);
418
png_set_sig_bytes(png_ptr, 8);
420
// read all PNG info up to image data
421
png_read_info(png_ptr, info_ptr);
423
// get width, height, bit-depth and color-type
425
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
427
// expand images of all color-type and bit-depth to 3x8 bit RGB images
428
// let the library process things like alpha, transparency, background
430
if (bit_depth == 16) png_set_strip_16(png_ptr);
431
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
432
if (bit_depth < 8) png_set_expand(png_ptr);
433
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
434
if (color_type == PNG_COLOR_TYPE_GRAY ||
435
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
436
png_set_gray_to_rgb(png_ptr);
437
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
438
png_set_strip_alpha(png_ptr);
440
// if required set gamma conversion
441
if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, (double) 2.2, gamma);
443
// after the transformations have been registered update info_ptr data
444
png_read_update_info(png_ptr, info_ptr);
446
// get again width, height and the new bit-depth and color-type
447
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
450
// row_bytes is the width x number of channels
451
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
452
channels = png_get_channels(png_ptr, info_ptr);
456
//nuxAssertMsg(0, TEXT("[read_png_rgba] Unable to open image of type RGBA %s using read_png_rgb()."), filename);
457
//nuxAssertMsg(0, TEXT("[read_png_rgba] Try using read_png_rgba() instead!"));
462
// now we can allocate memory to store the image
464
png_byte * img = new png_byte[row_bytes * h];
466
// and allocate memory for an array of row-pointers
468
png_byte ** row = new png_byte * [h];
471
// set the individual row-pointers to point at the correct offsets
473
for (unsigned int i = 0; i < h; i++)
474
row[i] = img + i * row_bytes;
476
// now we can go ahead and just read the whole image
478
png_read_image(png_ptr, row);
480
// read the additional chunks in the PNG file (not really needed)
482
png_read_end(png_ptr, NULL);
484
//image = array2<vec3ub>(w, h);
485
NTextureData* TextureObjectData = new NTextureData(BITFMT_R8G8B8, w, h, 1);
487
for(unsigned int i=0; i < w; i++)
488
for(unsigned int j=0; j < h; j++)
490
BYTE* png_data_pointer = img + ((h-j-1)*row_bytes + i * 3);
493
(*(png_data_pointer + 2) << 16) | //r
494
(*(png_data_pointer + 1) << 8) | //g
495
*(png_data_pointer + 0); //b
497
TextureObjectData->GetSurface(0).Write24b(i, j, value); // vec3ub(img + ((h-j-1)*row_bytes + i * 3));
505
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
507
if(TextureObjectData)
508
TextureObjectData->GetSurface(0).FlipVertical();
512
return TextureObjectData;
516
////FIXME: LIBPNG expands to RGB and only R is fetched...
517
//bool read_png_grey(const char * filename, glh::array2<unsigned char> & image)
519
// //--------- Resource stuff
520
// PNG_DECLARE_RESVAR
524
// int bit_depth, color_type;
526
// png_uint_32 channels, row_bytes;
527
// png_structp png_ptr = 0;
528
// png_infop info_ptr = 0;
531
// // This is the SDK default path...
532
// if(path.path.size() < 1)
534
// path.path.push_back(""); // added by Ashu, for case where fully qualified path is given
535
// path.path.push_back(".");
536
// path.path.push_back("../../../MEDIA/textures/1D");
537
// path.path.push_back("../../../../MEDIA/textures/1D");
538
// path.path.push_back("../../../../../../../MEDIA/textures/1D");
539
// path.path.push_back("../../../MEDIA/textures/2D");
540
// path.path.push_back("../../../../MEDIA/textures/2D");
541
// path.path.push_back("../../../../../../../MEDIA/textures/2D");
542
// path.path.push_back("../../../MEDIA/textures/rectangles");
543
// path.path.push_back("../../../../MEDIA/textures/rectangles");
544
// path.path.push_back("../../../../../../../MEDIA/textures/rectangles");
545
// path.path.push_back("../../../MEDIA/textures/cubemaps");
546
// path.path.push_back("../../../../MEDIA/textures/cubemaps");
547
// path.path.push_back("../../../../../../../MEDIA/textures/cubemaps");
548
// path.path.push_back("../../../MEDIA/textures/3D");
549
// path.path.push_back("../../../../MEDIA/textures/3D");
550
// path.path.push_back("../../../../../../../MEDIA/textures/3D");
553
// // open the PNG input file
554
// if (!filename) return false;
556
// if (!(fp = path.fopen(filename)))
557
//#if defined(_WIN32)
559
// // Try resource access
560
// HRes = get_resource(filename);
564
// hglobal = LoadResource(png_resource.hModule, HRes);
567
// ressz = SizeofResource(png_resource.hModule, HRes);
568
// resptr = (char*)LockResource(hglobal);
576
// // first check the eight byte PNG signature
577
// PNG_READHEADER fread(sig, 1, 8, fp);
578
// if (!png_check_sig(sig, 8)) {
579
// if(fp) fclose(fp);
585
// // start back here!!!!
587
// // create the two png(-info) structures
589
// png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
591
// if(fp) fclose(fp);
596
// info_ptr = png_create_info_struct(png_ptr);
599
// png_destroy_read_struct(&png_ptr, 0, 0);
600
// if(fp) fclose(fp);
604
// // initialize the png structure
605
// PNG_SETREADFN png_init_io(png_ptr, fp);
607
// png_set_sig_bytes(png_ptr, 8);
609
// // read all PNG info up to image data
610
// png_read_info(png_ptr, info_ptr);
612
// // get width, height, bit-depth and color-type
613
// unsigned long w, h;
614
// png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
616
// // expand images of all color-type and bit-depth to 3x8 bit RGB images
617
// // let the library process things like alpha, transparency, background
619
// if (bit_depth == 16) png_set_strip_16(png_ptr);
620
// if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
621
// if (bit_depth < 8) png_set_expand(png_ptr);
622
// if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
623
// if (color_type == PNG_COLOR_TYPE_GRAY ||
624
// color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
625
// png_set_gray_to_rgb(png_ptr);
627
// // if required set gamma conversion
628
// if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, (double) 2.2, gamma);
630
// // after the transformations have been registered update info_ptr data
631
// png_read_update_info(png_ptr, info_ptr);
633
// // get again width, height and the new bit-depth and color-type
634
// png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
637
// // row_bytes is the width x number of channels
638
// row_bytes = png_get_rowbytes(png_ptr, info_ptr);
639
// channels = png_get_channels(png_ptr, info_ptr);
641
// // now we can allocate memory to store the image
643
// png_byte * img = new png_byte[row_bytes * h];
645
// // and allocate memory for an array of row-pointers
647
// png_byte ** row = new png_byte * [h];
650
// // set the individual row-pointers to point at the correct offsets
652
// for (unsigned int i = 0; i < h; i++)
653
// row[i] = img + i * row_bytes;
655
// // now we can go ahead and just read the whole image
657
// png_read_image(png_ptr, row);
659
// // read the additional chunks in the PNG file (not really needed)
661
// png_read_end(png_ptr, NULL);
663
// image = array2<unsigned char>(w, h);
666
// for(unsigned int i=0; i < w; i++)
667
// for(unsigned int j=0; j < h; j++)
668
// { image(i,j) = *(img + ((h-j-1)*row_bytes + i * 3)); }
674
// png_destroy_read_struct(&png_ptr, &info_ptr, 0);
676
// if(fp) fclose (fp);
682
void png_read_resource_fn(png_structp png_ptr, png_bytep data, png_size_t leng)
684
png_bytep src = (png_bytep)png_ptr->io_ptr;
686
for(unsigned int i=0; i<leng; i++)
688
png_ptr->io_ptr = (void*)src;