More clean up for synch with 0.8.42
This commit is contained in:
43
src/osgPlugins/flt/OldMaterialPaletteRecord.cpp
Normal file
43
src/osgPlugins/flt/OldMaterialPaletteRecord.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
// OldMaterialPaletteRecord.cpp
|
||||
|
||||
#include "flt.h"
|
||||
#include "Registry.h"
|
||||
#include "OldMaterialPaletteRecord.h"
|
||||
|
||||
using namespace flt;
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// OldMaterialPaletteRecord
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
RegisterRecordProxy<OldMaterialPaletteRecord> g_OldMaterialPaletteProxy;
|
||||
|
||||
OldMaterialPaletteRecord::OldMaterialPaletteRecord()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// virtual
|
||||
OldMaterialPaletteRecord::~OldMaterialPaletteRecord()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// virtual
|
||||
void OldMaterialPaletteRecord::endian()
|
||||
{
|
||||
SOldMaterial *pSMaterial = (SOldMaterial*)getData();
|
||||
|
||||
for (int i=0; i < 64; i++)
|
||||
{
|
||||
pSMaterial->mat[i].Ambient.endian();
|
||||
pSMaterial->mat[i].Diffuse.endian();
|
||||
pSMaterial->mat[i].Specular.endian();
|
||||
pSMaterial->mat[i].Emissive.endian();
|
||||
ENDIAN( pSMaterial->mat[i].sfShininess );
|
||||
ENDIAN( pSMaterial->mat[i].sfAlpha );
|
||||
ENDIAN( pSMaterial->mat[i].diFlags );
|
||||
}
|
||||
}
|
||||
61
src/osgPlugins/flt/OldMaterialPaletteRecord.h
Normal file
61
src/osgPlugins/flt/OldMaterialPaletteRecord.h
Normal file
@@ -0,0 +1,61 @@
|
||||
// OldMaterialPaletteRecord.h
|
||||
|
||||
#ifndef __FLT_OLD_MATERIAL_PALETTE_RECORD_H
|
||||
#define __FLT_OLD_MATERIAL_PALETTE_RECORD_H
|
||||
|
||||
|
||||
#include "opcodes.h"
|
||||
#include "Record.h"
|
||||
#include "RecordVisitor.h"
|
||||
|
||||
|
||||
namespace flt {
|
||||
|
||||
|
||||
struct SOldMaterial
|
||||
{
|
||||
SRecHeader RecHeader;
|
||||
struct
|
||||
{
|
||||
float32x3 Ambient; // Ambient component of material
|
||||
float32x3 Diffuse; // Diffuse component of material
|
||||
float32x3 Specular; // Specular component of material
|
||||
float32x3 Emissive; // Emissive component of material
|
||||
float32 sfShininess; // Shininess. [0.0-128.0]
|
||||
float32 sfAlpha; // Alpha. [0.0-1.0], where 1.0 is opaque
|
||||
uint32 diFlags; // bit 0 Materials used
|
||||
// bit 1-31 Spare
|
||||
uint32 spares[31]; // Spares for material
|
||||
} mat[64];
|
||||
};
|
||||
|
||||
|
||||
class OldMaterialPaletteRecord : public AncillaryRecord
|
||||
{
|
||||
public:
|
||||
|
||||
OldMaterialPaletteRecord();
|
||||
|
||||
virtual Record* clone() const { return new OldMaterialPaletteRecord(); }
|
||||
virtual const char* className() const { return "OldMaterialPaletteRecord"; }
|
||||
virtual int classOpcode() const { return OLD_MATERIAL_PALETTE_OP; }
|
||||
virtual int sizeofData() const { return sizeof(SOldMaterial); }
|
||||
virtual void accept(RecordVisitor& rv) { rv.apply(*this); }
|
||||
// virtual void traverse(RecordVisitor& rv);
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~OldMaterialPaletteRecord();
|
||||
|
||||
virtual void endian();
|
||||
// virtual void decode();
|
||||
|
||||
// virtual bool readLocalData(Input& fr);
|
||||
// virtual bool writeLocalData(Output& fw);
|
||||
|
||||
};
|
||||
|
||||
|
||||
}; // end namespace flt
|
||||
|
||||
#endif
|
||||
114
src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp
Normal file
114
src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <osg/Geode>
|
||||
#include <osg/Group>
|
||||
#include <osg/Notify>
|
||||
|
||||
#include <osgDB/Registry>
|
||||
#include <osgDB/ReadFile>
|
||||
#include <osgDB/FileUtils>
|
||||
#include <osgDB/FileNameUtils>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
using namespace osg;
|
||||
|
||||
class sgReaderWriterOSGTGZ : public osgDB::ReaderWriter
|
||||
{
|
||||
public:
|
||||
virtual const char* className() { return "OSGTGZ Database Reader/Writer"; }
|
||||
virtual bool acceptsExtension(const std::string& extension)
|
||||
{
|
||||
return osgDB::equalCaseInsensitive(extension,"osgtgz");
|
||||
}
|
||||
|
||||
virtual Node* readNode(const std::string& fileName)
|
||||
{
|
||||
std::string ext = osgDB::getFileExtension(fileName);
|
||||
if (!acceptsExtension(ext)) return NULL;
|
||||
|
||||
osg::notify(osg::INFO)<<"sgReaderWriterOSGTGZ::readNode( "<<fileName.c_str()<<" )\n";
|
||||
|
||||
char dirname[128];
|
||||
char command[1024];
|
||||
|
||||
#ifdef _WIN32
|
||||
sprintf( dirname, "C:/Windows/Temp/.osgdb_osgtgz");
|
||||
// note, the following C option under windows does not seem to work...
|
||||
// will pursue an better tar.exe later. RO.
|
||||
sprintf( command,
|
||||
"tar xfCz %s %s",
|
||||
fileName.c_str(), dirname );
|
||||
mkdir( dirname);
|
||||
#endif
|
||||
|
||||
#ifdef __linux
|
||||
sprintf( dirname, "/tmp/.osg%06d", getpid());
|
||||
sprintf( command,
|
||||
"tar xfCz %s %s",
|
||||
fileName.c_str(), dirname );
|
||||
mkdir( dirname, 0700 );
|
||||
#endif
|
||||
|
||||
#ifdef __sgi
|
||||
sprintf( dirname, "/tmp/.osg%06d", getpid());
|
||||
sprintf( command,
|
||||
"cp %s %s; cd %s;"
|
||||
"gzcat %s | tar xf -",
|
||||
fileName.c_str(), dirname, dirname,
|
||||
fileName.c_str() );
|
||||
mkdir( dirname, 0700 );
|
||||
#endif
|
||||
|
||||
system( command );
|
||||
|
||||
osg::Group *grp = new osg::Group;
|
||||
osgDB::setFilePath( dirname );
|
||||
|
||||
osgDB::DirectoryContents contents = osgDB::getDirectoryContents(dirname);
|
||||
for(osgDB::DirectoryContents::iterator itr = contents.begin();
|
||||
itr != contents.end();
|
||||
++itr)
|
||||
{
|
||||
std::string file_ext = osgDB::getLowerCaseFileExtension(*itr);
|
||||
if (osgDB::equalCaseInsensitive(file_ext,"osg"))
|
||||
{
|
||||
osg::Node *node = osgDB::readNodeFile( *itr );
|
||||
grp->addChild( node );
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
// note, is this the right command for windows?
|
||||
// is there any way of overiding the Y/N option? RO.
|
||||
sprintf( command, "erase %s", dirname );
|
||||
system( command );
|
||||
#else
|
||||
|
||||
sprintf( command, "rm -rf %s", dirname );
|
||||
system( command );
|
||||
#endif
|
||||
|
||||
if( grp->getNumChildren() == 0 )
|
||||
{
|
||||
grp->unref();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return grp;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// now register with sgRegistry to instantiate the above
|
||||
// reader/writer.
|
||||
osgDB::RegisterReaderWriterProxy<sgReaderWriterOSGTGZ> g_readerWriter_OSGTGZ_Proxy;
|
||||
330
src/osgPlugins/png/new_ReaderWriterPNG.cpp
Normal file
330
src/osgPlugins/png/new_ReaderWriterPNG.cpp
Normal file
@@ -0,0 +1,330 @@
|
||||
#include <osg/Image>
|
||||
#include <osg/Notify>
|
||||
#include <osg/Geode>
|
||||
#include <osg/GL>
|
||||
|
||||
#include <osgDB/Registry>
|
||||
#include <osgDB/FileNameUtils>
|
||||
|
||||
/****************************************************************************
|
||||
*
|
||||
* Follows is code extracted from the simage library. Original Authors:
|
||||
*
|
||||
* Systems in Motion,
|
||||
* <URL:http://www.sim.no>
|
||||
*
|
||||
* Peder Blekken <pederb@sim.no>
|
||||
* Morten Eriksen <mortene@sim.no>
|
||||
* Marius Bugge Monsen <mariusbu@sim.no>
|
||||
*
|
||||
* The original COPYING notice
|
||||
*
|
||||
* All files in this library are public domain, except simage_rgb.cpp which is
|
||||
* Copyright (c) Mark J Kilgard <mjk@nvidia.com>. I will contact Mark
|
||||
* very soon to hear if this source also can become public domain.
|
||||
*
|
||||
* Please send patches for bugs and new features to: <pederb@sim.no>.
|
||||
*
|
||||
* Peder Blekken
|
||||
*
|
||||
*
|
||||
* Ported into the OSG as a plugin, Robert Osfield Decemeber 2000.
|
||||
* Note, reference above to license of simage_rgb is not relevent to the OSG
|
||||
* as the OSG does not use it. Also for patches, bugs and new features
|
||||
* please send them direct to the OSG dev team rather than address above.
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
/*
|
||||
* Based on example code found in the libjpeg archive
|
||||
*
|
||||
*/
|
||||
|
||||
using namespace osg;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <png.h>
|
||||
}
|
||||
|
||||
#define ERR_NO_ERROR 0
|
||||
#define ERR_OPEN 1
|
||||
#define ERR_MEM 2
|
||||
#define ERR_PNGLIB 3
|
||||
|
||||
static int pngerror = ERR_NO_ERROR;
|
||||
|
||||
/* my setjmp buffer */
|
||||
static jmp_buf setjmp_buffer;
|
||||
|
||||
/* called my libpng */
|
||||
static void
|
||||
warn_callback(png_structp /*ps*/, png_const_charp pc)
|
||||
{
|
||||
/*FIXME: notify? */
|
||||
osg::notify(osg::WARN)<<"Warning in .png reader: ";
|
||||
if (pc) osg::notify(osg::WARN)<< pc;
|
||||
osg::notify(osg::WARN)<<endl;
|
||||
}
|
||||
|
||||
static void
|
||||
err_callback(png_structp /*ps*/, png_const_charp pc)
|
||||
{
|
||||
/* FIXME: store error message? */
|
||||
longjmp(setjmp_buffer, 1);
|
||||
|
||||
osg::notify(osg::WARN)<<"Error in .png reader: ";
|
||||
if (pc) osg::notify(osg::WARN)<< pc;
|
||||
osg::notify(osg::WARN)<<endl;
|
||||
}
|
||||
|
||||
int
|
||||
simage_png_error(char * buffer, int buflen)
|
||||
{
|
||||
switch (pngerror) {
|
||||
case ERR_OPEN:
|
||||
strncpy(buffer, "PNG loader: Error opening file", buflen);
|
||||
break;
|
||||
case ERR_MEM:
|
||||
strncpy(buffer, "PNG loader: Out of memory error", buflen);
|
||||
break;
|
||||
case ERR_PNGLIB:
|
||||
strncpy(buffer, "PNG loader: Illegal png file", buflen);
|
||||
break;
|
||||
}
|
||||
return pngerror;
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
simage_png_identify(const char * /*ptr*/,
|
||||
const unsigned char *header,
|
||||
int headerlen)
|
||||
{
|
||||
static unsigned char pngcmp[] = {0x89, 'P', 'N', 'G', 0xd, 0xa, 0x1a, 0xa};
|
||||
if (headerlen < 8) return 0;
|
||||
if (memcmp((const void*)header,
|
||||
(const void*)pngcmp, 8) == 0) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned char *
|
||||
simage_png_load(const char *filename,
|
||||
int *width_ret,
|
||||
int *height_ret,
|
||||
int *numComponents_ret)
|
||||
{
|
||||
png_structp png_ptr;
|
||||
png_infop info_ptr;
|
||||
png_uint_32 width, height;
|
||||
|
||||
int bit_depth, color_type, interlace_type;
|
||||
FILE *fp;
|
||||
unsigned char *buffer;
|
||||
int bytes_per_row;
|
||||
int number_passes;
|
||||
int channels;
|
||||
int format;
|
||||
|
||||
if ((fp = fopen(filename, "rb")) == NULL) {
|
||||
pngerror = ERR_OPEN;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Create and initialize the png_struct with the desired error handler
|
||||
* functions. If you want to use the default stderr and longjump method,
|
||||
* you can supply NULL for the last three parameters. We also supply the
|
||||
* the compiler header file version, so that we know if the application
|
||||
* was compiled with a compatible version of the library. REQUIRED
|
||||
*/
|
||||
/*png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
|
||||
(void *)user_error_ptr, user_error_fn, user_warning_fn);*/
|
||||
|
||||
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
|
||||
NULL, err_callback, warn_callback);
|
||||
|
||||
if (png_ptr == NULL) {
|
||||
pngerror = ERR_MEM;
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Allocate/initialize the memory for image information. REQUIRED. */
|
||||
info_ptr = png_create_info_struct(png_ptr);
|
||||
if (info_ptr == NULL) {
|
||||
pngerror = ERR_MEM;
|
||||
fclose(fp);
|
||||
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Set error handling if you are using the setjmp/longjmp method (this is
|
||||
* the normal method of doing things with libpng). REQUIRED unless you
|
||||
* set up your own error handlers in the png_create_read_struct() earlier.
|
||||
*/
|
||||
|
||||
buffer = NULL;
|
||||
|
||||
if (setjmp(setjmp_buffer)) {
|
||||
pngerror = ERR_PNGLIB;
|
||||
/* Free all of the memory associated with the png_ptr and info_ptr */
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
|
||||
fclose(fp);
|
||||
/* If we get here, we had a problem reading the file */
|
||||
|
||||
if (buffer) free(buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Set up the input control if you are using standard C streams */
|
||||
png_init_io(png_ptr, fp);
|
||||
|
||||
/* The call to png_read_info() gives us all of the information from the
|
||||
* PNG file before the first IDAT (image data chunk). REQUIRED
|
||||
*/
|
||||
png_read_info(png_ptr, info_ptr);
|
||||
|
||||
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
|
||||
&interlace_type, NULL, NULL);
|
||||
|
||||
/**** Set up the data transformations you want. Note that these are all
|
||||
**** optional. Only call them if you want/need them. Many of the
|
||||
**** transformations only work on specific types of images, and many
|
||||
**** are mutually exclusive.
|
||||
****/
|
||||
|
||||
/* tell libpng to strip 16 bit/color files down to 8 bits/color */
|
||||
png_set_strip_16(png_ptr);
|
||||
|
||||
/* strip alpha bytes from the input data without combining with th
|
||||
* background (not recommended) */
|
||||
/* png_set_strip_alpha(png_ptr); */
|
||||
|
||||
/* extract multiple pixels with bit depths of 1, 2, and 4 from a single
|
||||
* byte into separate bytes (useful for paletted and grayscale images).
|
||||
*/
|
||||
/* png_set_packing(png_ptr); */
|
||||
|
||||
/* change the order of packed pixels to least significant bit first
|
||||
* (not useful if you are using png_set_packing). */
|
||||
/* png_set_packswap(png_ptr); */
|
||||
|
||||
/* expand paletted colors into true RGB triplets */
|
||||
if (color_type == PNG_COLOR_TYPE_PALETTE)
|
||||
png_set_expand(png_ptr);
|
||||
|
||||
/* expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
|
||||
png_set_expand(png_ptr);
|
||||
|
||||
/* expand paletted or RGB images with transparency to full alpha channels
|
||||
* so the data will be available as RGBA quartets */
|
||||
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
|
||||
png_set_expand(png_ptr);
|
||||
|
||||
/* Add filler (or alpha) byte (before/after each RGB triplet) */
|
||||
/* png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); */
|
||||
|
||||
/* Turn on interlace handling. REQUIRED if you are not using
|
||||
* png_read_image(). To see how to handle interlacing passes,
|
||||
* see the png_read_row() method below.
|
||||
*/
|
||||
|
||||
png_read_update_info(png_ptr, info_ptr);
|
||||
|
||||
number_passes = png_set_interlace_handling(png_ptr);
|
||||
channels = png_get_channels(png_ptr, info_ptr);
|
||||
|
||||
/* allocate the memory to hold the image using the fields of info_ptr. */
|
||||
|
||||
bytes_per_row = png_get_rowbytes(png_ptr, info_ptr);
|
||||
|
||||
buffer = (unsigned char*) malloc(bytes_per_row*height);
|
||||
|
||||
format = channels; /* this is safer than the above */
|
||||
|
||||
if (buffer) {
|
||||
int pass, y;
|
||||
unsigned char *dummytab[1];
|
||||
for (pass = 0; pass < number_passes; pass++) {
|
||||
for ( y = 0; (unsigned int) y < height; y++ ) {
|
||||
/* flips image upside down */
|
||||
dummytab[0] = &buffer[bytes_per_row*(height-1-y)];
|
||||
png_read_rows(png_ptr, dummytab, NULL, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* read rest of file, and get additional chunks in info_ptr - REQUIRED */
|
||||
png_read_end(png_ptr, info_ptr);
|
||||
}
|
||||
|
||||
/* clean up after the read, and free any memory allocated - REQUIRED */
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
|
||||
|
||||
/* close the file */
|
||||
fclose(fp);
|
||||
|
||||
/* that's it */
|
||||
if (buffer) {
|
||||
*width_ret = width;
|
||||
*height_ret = height;
|
||||
*numComponents_ret = format;
|
||||
pngerror = ERR_NO_ERROR;
|
||||
}
|
||||
else {
|
||||
pngerror = ERR_MEM;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
class ReaderWriterPNG : public osgDB::ReaderWriter
|
||||
{
|
||||
public:
|
||||
virtual const char* className() { return "PNG Image Reader"; }
|
||||
virtual bool acceptsExtension(const std::string& extension)
|
||||
{
|
||||
return osgDB::equalCaseInsensitive(extension,"png");
|
||||
}
|
||||
|
||||
virtual osg::Image* readImage(const std::string& fileName)
|
||||
{
|
||||
|
||||
unsigned char *imageData = NULL;
|
||||
int width_ret;
|
||||
int height_ret;
|
||||
int numComponents_ret;
|
||||
|
||||
imageData = simage_png_load(fileName.c_str(),&width_ret,&height_ret,&numComponents_ret);
|
||||
|
||||
if (imageData==NULL) return NULL;
|
||||
|
||||
int s = width_ret;
|
||||
int t = height_ret;
|
||||
int r = 1;
|
||||
|
||||
int internalFormat = numComponents_ret;
|
||||
|
||||
unsigned int pixelFormat =
|
||||
numComponents_ret == 1 ? GL_LUMINANCE :
|
||||
numComponents_ret == 2 ? GL_LUMINANCE_ALPHA :
|
||||
numComponents_ret == 3 ? GL_RGB :
|
||||
numComponents_ret == 4 ? GL_RGBA : (GLenum)-1;
|
||||
|
||||
unsigned int dataType = GL_UNSIGNED_BYTE;
|
||||
|
||||
osg::Image* pOsgImage = new osg::Image;
|
||||
pOsgImage->setFileName(fileName.c_str());
|
||||
pOsgImage->setImage(s,t,r,
|
||||
internalFormat,
|
||||
pixelFormat,
|
||||
dataType,
|
||||
imageData);
|
||||
|
||||
return pOsgImage;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// now register with Registry to instantiate the above
|
||||
// reader/writer.
|
||||
osgDB::RegisterReaderWriterProxy<ReaderWriterPNG> g_readerWriter_PNG_Proxy;
|
||||
170
src/osgPlugins/png/prev_ReaderWriterPNG.cpp
Normal file
170
src/osgPlugins/png/prev_ReaderWriterPNG.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
#include <osg/Image>
|
||||
#include "osg/Notify"
|
||||
#include <osg/Geode>
|
||||
#include "osg/GL"
|
||||
|
||||
#include "osgDB/Registry"
|
||||
|
||||
using namespace osg;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <png.h>
|
||||
}
|
||||
|
||||
|
||||
/* Transparency parameters */
|
||||
#define PNG_ALPHA -2 /* Use alpha channel in PNG file, if there is one */
|
||||
#define PNG_SOLID -1 /* No transparency */
|
||||
#define PNG_STENCIL 0 /* Sets alpha to 0 for r=g=b=0, 1 otherwise */
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned int Width;
|
||||
unsigned int Height;
|
||||
unsigned int Depth;
|
||||
unsigned int Alpha;
|
||||
} pngInfo;
|
||||
|
||||
class ReaderWriterPNG : public osgDB::ReaderWriter
|
||||
{
|
||||
public:
|
||||
virtual const char* className() { return "PNG Image Reader/Writer"; }
|
||||
virtual bool acceptsExtension(const std::string& extension) { return extension=="png"; }
|
||||
|
||||
virtual Image* readImage(const std::string& fileName)
|
||||
{
|
||||
|
||||
int trans = PNG_ALPHA;
|
||||
FILE *fp = NULL;
|
||||
pngInfo pInfo;
|
||||
pngInfo *pinfo = &pInfo;
|
||||
|
||||
unsigned char header[8];
|
||||
png_structp png;
|
||||
png_infop info;
|
||||
png_infop endinfo;
|
||||
png_bytep data; //, data2;
|
||||
png_bytep *row_p;
|
||||
double fileGamma;
|
||||
|
||||
png_uint_32 width, height;
|
||||
int depth, color;
|
||||
|
||||
png_uint_32 i;
|
||||
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
info = png_create_info_struct(png);
|
||||
endinfo = png_create_info_struct(png);
|
||||
|
||||
fp = fopen(fileName.c_str(), "rb");
|
||||
if (fp && fread(header, 1, 8, fp) && png_check_sig(header, 8))
|
||||
png_init_io(png, fp);
|
||||
else
|
||||
{
|
||||
png_destroy_read_struct(&png, &info, &endinfo);
|
||||
return NULL;
|
||||
}
|
||||
png_set_sig_bytes(png, 8);
|
||||
|
||||
png_read_info(png, info);
|
||||
png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);
|
||||
|
||||
if (pinfo != NULL)
|
||||
{
|
||||
pinfo->Width = width;
|
||||
pinfo->Height = height;
|
||||
pinfo->Depth = depth;
|
||||
}
|
||||
|
||||
if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
|
||||
png_set_gray_to_rgb(png);
|
||||
|
||||
if (color&PNG_COLOR_MASK_ALPHA && trans != PNG_ALPHA)
|
||||
{
|
||||
png_set_strip_alpha(png);
|
||||
color &= ~PNG_COLOR_MASK_ALPHA;
|
||||
}
|
||||
|
||||
// if (!(PalettedTextures && mipmap >= 0 && trans == PNG_SOLID))
|
||||
if (color == PNG_COLOR_TYPE_PALETTE)
|
||||
png_set_expand(png);
|
||||
|
||||
/*--GAMMA--*/
|
||||
// checkForGammaEnv();
|
||||
double screenGamma = 2.2 / 1.0;
|
||||
if (png_get_gAMA(png, info, &fileGamma))
|
||||
png_set_gamma(png, screenGamma, fileGamma);
|
||||
else
|
||||
png_set_gamma(png, screenGamma, 1.0/2.2);
|
||||
|
||||
png_read_update_info(png, info);
|
||||
|
||||
data = (png_bytep) malloc(png_get_rowbytes(png, info)*height);
|
||||
row_p = (png_bytep *) malloc(sizeof(png_bytep)*height);
|
||||
|
||||
bool StandardOrientation = false;
|
||||
for (i = 0; i < height; i++)
|
||||
{
|
||||
if (StandardOrientation)
|
||||
row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
|
||||
else
|
||||
row_p[i] = &data[png_get_rowbytes(png, info)*i];
|
||||
}
|
||||
|
||||
png_read_image(png, row_p);
|
||||
free(row_p);
|
||||
|
||||
int iBitCount;
|
||||
|
||||
if (trans == PNG_SOLID || trans == PNG_ALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA)
|
||||
{
|
||||
switch (color)
|
||||
{
|
||||
case PNG_COLOR_TYPE_GRAY:
|
||||
case PNG_COLOR_TYPE_RGB:
|
||||
case PNG_COLOR_TYPE_PALETTE:
|
||||
iBitCount = 24;
|
||||
if (pinfo != NULL) pinfo->Alpha = 0;
|
||||
break;
|
||||
|
||||
case PNG_COLOR_TYPE_GRAY_ALPHA:
|
||||
case PNG_COLOR_TYPE_RGB_ALPHA:
|
||||
iBitCount = 32;
|
||||
if (pinfo != NULL) pinfo->Alpha = 8;
|
||||
break;
|
||||
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
png_read_end(png, endinfo);
|
||||
png_destroy_read_struct(&png, &info, &endinfo);
|
||||
|
||||
// free(data);
|
||||
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
|
||||
osg::Image* pOsgImage = new osg::Image();
|
||||
|
||||
pOsgImage->setFileName(fileName.c_str());
|
||||
if (iBitCount == 24)
|
||||
pOsgImage->setImage(width, height, 1,
|
||||
iBitCount / 8,// int internalFormat,
|
||||
GL_RGB, // unsigned int pixelFormat
|
||||
GL_UNSIGNED_BYTE,// unsigned int dataType
|
||||
data);
|
||||
else
|
||||
pOsgImage->setImage(width, height, 1,
|
||||
iBitCount / 8,// int internalFormat,
|
||||
GL_RGBA, // unsigned int pixelFormat
|
||||
GL_UNSIGNED_BYTE,// unsigned int dataType
|
||||
data);
|
||||
return pOsgImage;
|
||||
}
|
||||
};
|
||||
|
||||
// now register with Registry to instantiate the above
|
||||
// reader/writer.
|
||||
osgDB::RegisterReaderWriterProxy<ReaderWriterPNG> g_readerWriter_PNG_Proxy;
|
||||
Reference in New Issue
Block a user