pnmimage uses iostream instead of stdio

This commit is contained in:
David Rose 2002-08-05 14:56:25 +00:00
parent c5103b67e5
commit ff61d64eb3
56 changed files with 1007 additions and 2820 deletions

View File

@ -19,17 +19,18 @@
#include "glxDisplay.h"
#include "config_glxdisplay.h"
#include <graphicsPipe.h>
#include <keyboardButton.h>
#include <mouseButton.h>
#include <glGraphicsStateGuardian.h>
#include <pStatTimer.h>
#include <clockObject.h>
#include "graphicsPipe.h"
#include "keyboardButton.h"
#include "mouseButton.h"
#include "glGraphicsStateGuardian.h"
#include "pStatTimer.h"
#include "clockObject.h"
#include <errno.h>
#include <sys/time.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
#include <stdio.h> // for sprintf()
////////////////////////////////////////////////////////////////////
// Static variables

View File

@ -114,7 +114,7 @@ matches_magic_number(const string &) const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileType::
make_reader(FILE *, bool, const string &) {
make_reader(istream *, bool, const string &) {
return NULL;
}
@ -126,7 +126,7 @@ make_reader(FILE *, bool, const string &) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileType::
make_writer(FILE *, bool) {
make_writer(ostream *, bool) {
return NULL;
}
@ -144,7 +144,7 @@ init_pnm() {
if (!_did_init_pnm) {
_did_init_pnm = true;
// No reason to do anything here now.
// No reason to do anything here nowadays.
/*
// Make an argc/argv style copy of the ExecutionEnvironment's

View File

@ -52,9 +52,9 @@ public:
virtual bool has_magic_number() const;
virtual bool matches_magic_number(const string &magic_number) const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
protected:
static void init_pnm();

View File

@ -21,6 +21,8 @@
#include "pnmFileType.h"
#include "pnmReader.h"
#include "config_pnmimage.h"
#include "virtualFileSystem.h"
#include "config_util.h"
////////////////////////////////////////////////////////////////////
// Function: PNMImageHeader::read_header
@ -63,13 +65,12 @@ make_reader(const Filename &filename, PNMFileType *type) const {
pnmimage_cat.debug()
<< "Reading image from " << filename << "\n";
}
Filename actual_name = Filename::binary_filename(filename);
bool owns_file;
FILE *file;
bool owns_file = false;
istream *file = (istream *)NULL;
if (filename == "-") {
owns_file = false;
file = stdin;
file = &cin;
if (pnmimage_cat.is_debug()) {
pnmimage_cat.debug()
@ -77,21 +78,25 @@ make_reader(const Filename &filename, PNMFileType *type) const {
}
} else {
if (!actual_name.exists()) {
if (pnmimage_cat.is_debug()) {
pnmimage_cat.debug()
<< "File " << filename << " does not exist.\n";
if (use_vfs) {
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
owns_file = true;
file = vfs->open_read_file(filename);
} else {
ifstream *new_istream = new ifstream;
Filename actual_name = Filename::binary_filename(filename);
if (!actual_name.open_read(*new_istream)) {
delete new_istream;
} else {
owns_file = true;
file = new_istream;
}
// The file doesn't exist. Don't pass it through libpnm, which
// will abort.
return NULL;
}
owns_file = true;
string os_specific = actual_name.to_os_specific();
file = fopen((char *)os_specific.c_str(), "rb");
}
if (file == (FILE *)NULL) {
if (file == (istream *)NULL) {
if (pnmimage_cat.is_debug()) {
pnmimage_cat.debug()
<< "Unable to open file.\n";
@ -111,10 +116,10 @@ make_reader(const Filename &filename, PNMFileType *type) const {
// or NULL if the file cannot be read for some reason.
//
// owns_file should be set true if the PNMReader is to
// be considered the owner of the FILE stream (in which
// case the stream will be closed on completion, whether
// successful or not), or false if it should not close
// it.
// be considered the owner of the stream pointer (in
// which case the stream will be deleted on completion,
// whether successful or not), or false if it should not
// delete it.
//
// The filename parameter is optional here, since the
// file has already been opened; it is only used to
@ -133,7 +138,7 @@ make_reader(const Filename &filename, PNMFileType *type) const {
// needed.
////////////////////////////////////////////////////////////////////
PNMReader *PNMImageHeader::
make_reader(FILE *file, bool owns_file, const Filename &filename,
make_reader(istream *file, bool owns_file, const Filename &filename,
string magic_number, PNMFileType *type) const {
if (type == (PNMFileType *)NULL) {
if (!read_magic_number(file, magic_number, 2)) {
@ -143,7 +148,7 @@ make_reader(FILE *file, bool owns_file, const Filename &filename,
<< "Image file appears to be empty.\n";
}
if (owns_file) {
fclose(file);
delete file;
}
return NULL;
}
@ -200,14 +205,14 @@ make_reader(FILE *file, bool owns_file, const Filename &filename,
write_types(pnmimage_cat.error(false), 2);
}
if (owns_file) {
fclose(file);
delete file;
}
return NULL;
}
PNMReader *reader = type->make_reader(file, owns_file, magic_number);
if (reader == NULL && owns_file) {
fclose(file);
delete file;
}
if (!reader->is_valid()) {
@ -237,13 +242,12 @@ make_writer(const Filename &filename, PNMFileType *type) const {
pnmimage_cat.debug()
<< "Writing image to " << filename << "\n";
}
Filename actual_name = Filename::binary_filename(filename);
bool owns_file;
FILE *file;
bool owns_file = false;
ostream *file = (ostream *)NULL;
if (filename == "-") {
owns_file = false;
file = stdout;
file = &cout;
if (pnmimage_cat.is_debug()) {
pnmimage_cat.debug()
@ -251,12 +255,18 @@ make_writer(const Filename &filename, PNMFileType *type) const {
}
} else {
owns_file = true;
string os_specific = actual_name.to_os_specific();
file = fopen((char *)os_specific.c_str(), "wb");
ofstream *new_ostream = new ofstream;
Filename actual_name = Filename::binary_filename(filename);
if (!actual_name.open_write(*new_ostream)) {
delete new_ostream;
} else {
owns_file = true;
file = new_ostream;
}
}
if (file == (FILE *)NULL) {
if (file == (ostream *)NULL) {
if (pnmimage_cat.is_debug()) {
pnmimage_cat.debug()
<< "Unable to write to file.\n";
@ -275,10 +285,10 @@ make_writer(const Filename &filename, PNMFileType *type) const {
// NULL if the file cannot be written for some reason.
//
// owns_file should be set true if the PNMWriter is to
// be considered the owner of the FILE stream (in which
// case the stream will be closed on completion, whether
// successful or not), or false if it should not close
// it.
// be considered the owner of the stream pointer (in
// which case the stream will be deleted on completion,
// whether successful or not), or false if it should not
// delete it.
//
// The filename parameter is optional here, since the
// file has already been opened; it is only used to
@ -292,7 +302,7 @@ make_writer(const Filename &filename, PNMFileType *type) const {
// needed.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMImageHeader::
make_writer(FILE *file, bool owns_file, const Filename &filename,
make_writer(ostream *file, bool owns_file, const Filename &filename,
PNMFileType *type) const {
if (type == (PNMFileType *)NULL && !filename.empty()) {
// We don't know the type; attempt to guess it from the filename
@ -328,14 +338,14 @@ make_writer(FILE *file, bool owns_file, const Filename &filename,
<< "Cannot determine type of image file " << filename << ".\n";
}
if (owns_file) {
fclose(file);
delete file;
}
return NULL;
}
PNMWriter *writer = type->make_writer(file, owns_file);
if (writer == NULL && owns_file) {
fclose(file);
delete file;
}
return writer;
@ -352,10 +362,10 @@ make_writer(FILE *file, bool owns_file, const Filename &filename,
// could be read.
////////////////////////////////////////////////////////////////////
bool PNMImageHeader::
read_magic_number(FILE *file, string &magic_number, int num_bytes) {
read_magic_number(istream *file, string &magic_number, int num_bytes) {
while ((int)magic_number.size() < num_bytes) {
int ch = getc(file);
if (ch == EOF) {
int ch = file->get();
if (file->eof() || file->fail()) {
return false;
}
magic_number += (char)ch;

View File

@ -80,18 +80,18 @@ public:
PNMReader *make_reader(const Filename &filename,
PNMFileType *type = NULL) const;
PNMReader *make_reader(FILE *file, bool owns_file = true,
PNMReader *make_reader(istream *file, bool owns_file = true,
const Filename &filename = Filename(),
string magic_number = string(),
PNMFileType *type = NULL) const;
PNMWriter *make_writer(const Filename &filename,
PNMFileType *type = NULL) const;
PNMWriter *make_writer(FILE *file, bool owns_file = true,
PNMWriter *make_writer(ostream *file, bool owns_file = true,
const Filename &filename = Filename(),
PNMFileType *type = NULL) const;
static bool read_magic_number(FILE *file, string &magic_number,
static bool read_magic_number(istream *file, string &magic_number,
int num_bytes);
void output(ostream &out) const;

View File

@ -22,7 +22,7 @@
// Description:
////////////////////////////////////////////////////////////////////
INLINE PNMReader::
PNMReader(PNMFileType *type, FILE *file, bool owns_file) :
PNMReader(PNMFileType *type, istream *file, bool owns_file) :
_type(type),
_owns_file(owns_file),
_file(file),

View File

@ -26,8 +26,9 @@
PNMReader::
~PNMReader() {
if (_owns_file) {
fclose(_file);
delete _file;
}
_file = (istream *)NULL;
}
////////////////////////////////////////////////////////////////////

View File

@ -34,7 +34,7 @@
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA PNMReader : public PNMImageHeader {
protected:
INLINE PNMReader(PNMFileType *type, FILE *file, bool owns_file);
INLINE PNMReader(PNMFileType *type, istream *file, bool owns_file);
public:
virtual ~PNMReader();
@ -52,7 +52,7 @@ public:
protected:
PNMFileType *_type;
bool _owns_file;
FILE *_file;
istream *_file;
bool _is_valid;
};

View File

@ -1,172 +0,0 @@
// Filename: pnmReaderTypes.h
// Created by: drose (14Jun00)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license. You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://www.panda3d.org/license.txt .
//
// To contact the maintainers of this program write to
// panda3d@yahoogroups.com .
//
////////////////////////////////////////////////////////////////////
#ifndef PNMREADERTYPES_H
#define PNMREADERTYPES_H
#include <pandabase.h>
#include "pnmReader.h"
class EXPCL_PANDA PNMReaderPNM : public PNMReader {
public:
PNMReaderPNM(FILE *file, int already_read_magic);
virtual FileType Type() const {
return PNMImageHeader::PNM;
}
virtual bool ReadRow(xel *row_data, xelval *alpha_data);
int ftype;
};
class EXPCL_PANDA PNMReaderSGI : public PNMReader {
public:
PNMReaderSGI(FILE *file, int already_read_magic);
virtual FileType Type() const {
return PNMImageHeader::SGI;
}
virtual bool ReadRow(xel *row_data, xelval *alpha_data);
~PNMReaderSGI();
typedef struct {
long start; /* offset in file */
long length; /* length of compressed scanline */
} TabEntry;
TabEntry *table;
long table_start;
int current_row;
int zsize, bpc;
};
class EXPCL_PANDA PNMReaderAlias : public PNMReader {
public:
PNMReaderAlias(FILE *file, int already_read_magic);
virtual FileType Type() const {
return PNMImageHeader::Alias;
}
virtual bool ReadRow(xel *row_data, xelval *alpha_data);
};
class EXPCL_PANDA PNMReaderRadiance : public PNMReader {
public:
PNMReaderRadiance(FILE *file, int already_read_magic);
virtual FileType Type() const {
return PNMImageHeader::Radiance;
}
virtual bool ReadRow(xel *row_data, xelval *alpha_data);
};
#ifndef WIN32_VC
class EXPCL_PANDA PNMReaderTIFF : public PNMReader {
public:
PNMReaderTIFF(FILE *file, int already_read_magic);
virtual FileType Type() const {
return PNMImageHeader::TIFF;
}
virtual bool ReadRow(xel *row_data, xelval *alpha_data);
~PNMReaderTIFF();
unsigned short photomet;
unsigned short bps, spp;
xel colormap[1024]; // == MAXCOLORS in pnm-image-tiff.cxx
int current_row;
struct tiff *tif;
};
#endif
class EXPCL_PANDA PNMReaderIMG : public PNMReader {
public:
PNMReaderIMG(FILE *file, int already_read_magic);
virtual FileType Type() const {
return PNMImageHeader::IMG;
}
virtual bool ReadRow(xel *row_data, xelval *alpha_data);
};
class EXPCL_PANDA PNMReaderSoftImage : public PNMReader {
public:
PNMReaderSoftImage(FILE *file, int already_read_magic);
virtual FileType Type() const {
return PNMImageHeader::SoftImage;
}
virtual bool ReadRow(xel *row_data, xelval *alpha_data);
enum { unknown, rgb, rgba, rgb_a } soft_color;
int rgb_ctype, alpha_ctype;
};
class EXPCL_PANDA PNMReaderYUV : public PNMReader {
public:
PNMReaderYUV(FILE *file, int already_read_magic);
virtual FileType Type() const {
return PNMImageHeader::YUV;
}
virtual bool ReadRow(xel *row_data, xelval *alpha_data);
~PNMReaderYUV();
long *yuvbuf;
};
class EXPCL_PANDA PNMReaderBMP : public PNMReader {
public:
PNMReaderBMP(FILE *file, int already_read_magic);
virtual FileType Type() const {
return PNMImageHeader::BMP;
}
virtual int ReadData(xel *array, xelval *alpha);
virtual bool SupportsStreaming() const {
return false;
}
unsigned long pos;
unsigned long offBits;
unsigned short cBitCount;
int indexed;
int classv;
pixval R[256]; /* reds */
pixval G[256]; /* greens */
pixval B[256]; /* blues */
};
#endif

View File

@ -23,7 +23,7 @@
// Description:
////////////////////////////////////////////////////////////////////
INLINE PNMWriter::
PNMWriter(PNMFileType *type, FILE *file, bool owns_file) :
PNMWriter(PNMFileType *type, ostream *file, bool owns_file) :
_type(type),
_owns_file(owns_file),
_file(file)

View File

@ -26,8 +26,9 @@
PNMWriter::
~PNMWriter() {
if (_owns_file) {
fclose(_file);
delete _file;
}
_file = (ostream *)NULL;
}
////////////////////////////////////////////////////////////////////

View File

@ -33,7 +33,7 @@
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA PNMWriter : public PNMImageHeader {
protected:
INLINE PNMWriter(PNMFileType *type, FILE *file, bool owns_file);
INLINE PNMWriter(PNMFileType *type, ostream *file, bool owns_file);
public:
@ -62,7 +62,7 @@ public:
protected:
PNMFileType *_type;
bool _owns_file;
FILE *_file;
ostream *_file;
};
#include "pnmWriter.I"

View File

@ -1,184 +0,0 @@
// Filename: pnmWriterTypes.h
// Created by: drose (14Jun00)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license. You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://www.panda3d.org/license.txt .
//
// To contact the maintainers of this program write to
// panda3d@yahoogroups.com .
//
////////////////////////////////////////////////////////////////////
#ifndef PNMWRITERTYPES_H
#define PNMWRITERTYPES_H
#include <pandabase.h>
#include "pnmWriter.h"
class EXPCL_PANDA PNMWriterPNM : public PNMWriter {
public:
PNMWriterPNM(FILE *file) : PNMWriter(file) {}
virtual FileType Type() const {
return PNMImageHeader::PNM;
}
virtual bool WriteHeader();
virtual bool WriteRow(xel *row_data, xelval *alpha_data);
int pnm_format;
};
class EXPCL_PANDA PNMWriterSGI : public PNMWriter {
public:
PNMWriterSGI(FILE *file) : PNMWriter(file) {}
~PNMWriterSGI();
virtual FileType Type() const {
return PNMImageHeader::SGI;
}
virtual bool WriteHeader();
virtual bool WriteRow(xel *row_data, xelval *alpha_data);
protected:
typedef struct {
long start; /* offset in file */
long length; /* length of compressed scanline */
} TabEntry;
typedef short ScanElem;
typedef struct {
ScanElem * data;
long length;
} ScanLine;
TabEntry &Table(int chan) {
return table[chan * rows + current_row];
}
void write_header(char *imagename);
void WriteTable();
void WriteChannels(ScanLine channel[], void (*put)(FILE *, short));
void BuildScanline(ScanLine output[], xel *row_data, xelval *alpha_data);
ScanElem *Compress(ScanElem *temp, ScanLine &output);
int RLECompress(ScanElem *inbuf, int size);
TabEntry *table;
long table_start;
int current_row;
int zsize, bpc;
int channels, dimensions;
int new_maxval;
ScanElem *rletemp;
};
class EXPCL_PANDA PNMWriterAlias : public PNMWriter {
public:
PNMWriterAlias(FILE *file) : PNMWriter(file) {}
virtual FileType Type() const {
return PNMImageHeader::Alias;
}
virtual bool WriteHeader();
virtual bool WriteRow(xel *row_data, xelval *alpha_data);
};
class EXPCL_PANDA PNMWriterRadiance : public PNMWriter {
public:
PNMWriterRadiance(FILE *file) : PNMWriter(file) {}
virtual FileType Type() const {
return PNMImageHeader::Radiance;
}
virtual bool WriteHeader();
virtual bool WriteRow(xel *row_data, xelval *alpha_data);
};
#ifndef WIN32_VC
class EXPCL_PANDA PNMWriterTIFF : public PNMWriter {
public:
PNMWriterTIFF(FILE *file) : PNMWriter(file) {}
virtual FileType Type() const {
return PNMImageHeader::TIFF;
}
virtual int WriteData(xel *array, xelval *alpha);
virtual bool SupportsStreaming() const {
return false;
}
};
#endif
class EXPCL_PANDA PNMWriterIMG : public PNMWriter {
public:
PNMWriterIMG(FILE *file) : PNMWriter(file) {}
virtual FileType Type() const {
return PNMImageHeader::IMG;
}
virtual bool WriteHeader();
virtual bool WriteRow(xel *row_data, xelval *alpha_data);
};
class EXPCL_PANDA PNMWriterSoftImage : public PNMWriter {
public:
PNMWriterSoftImage(FILE *file) : PNMWriter(file) {}
virtual FileType Type() const {
return PNMImageHeader::SoftImage;
}
virtual bool WriteHeader();
virtual bool WriteRow(xel *row_data, xelval *alpha_data);
};
class EXPCL_PANDA PNMWriterYUV : public PNMWriter {
public:
PNMWriterYUV(FILE *file) : PNMWriter(file) {
yuvbuf = NULL;
}
virtual FileType Type() const {
return PNMImageHeader::YUV;
}
virtual bool WriteHeader();
virtual bool WriteRow(xel *row_data, xelval *alpha_data);
~PNMWriterYUV();
unsigned char *yuvbuf;
};
class EXPCL_PANDA PNMWriterBMP : public PNMWriter {
public:
PNMWriterBMP(FILE *file) : PNMWriter(file) {}
virtual FileType Type() const {
return PNMImageHeader::BMP;
}
virtual int WriteData(xel *array, xelval *alpha);
virtual bool SupportsStreaming() const {
return false;
}
};
#endif

View File

@ -17,8 +17,8 @@
#include <assert.h>
struct bitstream
{
FILE *
f; /* bytestream */
istream *inf;
ostream *outf;
unsigned long
bitbuf; /* bit buffer */
int
@ -46,19 +46,39 @@ struct bitstream
*/
EXPCL_PANDA struct bitstream *
pm_bitinit(FILE *f, char *mode)
pm_bitinit(istream *f, char *mode)
{
struct bitstream *ans = (struct bitstream *)0;
if(!f || !mode || !*mode)
return ans;
if(strcmp(mode, "r") && strcmp(mode, "w"))
if(strcmp(mode, "r"))
return ans;
ans = (struct bitstream *)calloc(1, sizeof(struct bitstream));
if(ans)
{
ans->f = f;
ans->inf = f;
ans->mode = *mode;
}
return ans;
}
EXPCL_PANDA struct bitstream *
pm_bitinit(ostream *f, char *mode)
{
struct bitstream *ans = (struct bitstream *)0;
if(!f || !mode || !*mode)
return ans;
if(strcmp(mode, "w"))
return ans;
ans = (struct bitstream *)calloc(1, sizeof(struct bitstream));
if(ans)
{
ans->outf = f;
ans->mode = *mode;
}
@ -102,7 +122,7 @@ pm_bitfini(struct bitstream *b)
BitPut(b, 0, (long)8-(b->nbitbuf));
c = (char) BitGet(b, (long)8);
if(putc(c, b->f) == EOF)
if(!b->outf->put(c))
{
return -1;
}
@ -137,7 +157,7 @@ pm_bitread(struct bitstream *b, unsigned long nbits, unsigned long *val)
while (b->nbitbuf < (signed long)nbits)
{
if((c = getc(b->f)) == EOF)
if((c = b->inf->get()) == EOF)
{
return -1;
}
@ -173,7 +193,7 @@ pm_bitwrite(struct bitstream *b, unsigned long nbits, unsigned long val)
{
c = (char) BitGet(b, (long)8);
if(putc(c, b->f) == EOF)
if(!b->outf->put(c))
{
return -1;
}

View File

@ -19,7 +19,7 @@
#include "pandabase.h"
#include "pnmimage_base.h"
typedef struct bitstream *BITSTREAM;
typedef struct bitstream *BITSTREAM;
/*
* pm_bitinit() - allocate and return a BITSTREAM for the given FILE*.
@ -30,7 +30,8 @@ typedef struct bitstream *BITSTREAM;
* Returns 0 on error.
*/
extern EXPCL_PANDA BITSTREAM pm_bitinit(FILE *f, char *mode);
extern EXPCL_PANDA BITSTREAM pm_bitinit(istream *f, char *mode);
extern EXPCL_PANDA BITSTREAM pm_bitinit(ostream *f, char *mode);
/*
* pm_bitfini() - deallocate the given BITSTREAM.

View File

@ -17,9 +17,12 @@
////////////////////////////////////////////////////////////////////
#include "pnmimage_base.h"
#include "streamReader.h"
#include "streamWriter.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdio.h> // for sprintf()
////////////////////////////////////////////////////////////////////
// Function: pm_message
@ -124,105 +127,57 @@ pm_freerow(char *itrow) {
*/
int
pm_readbigshort(FILE* in, short* sP)
{
int c;
if ( (c = getc( in )) == EOF )
return -1;
*sP = ( c & 0xff ) << 8;
if ( (c = getc( in )) == EOF )
return -1;
*sP |= c & 0xff;
return 0;
}
pm_readbigshort(istream *in, short *sP) {
StreamReader reader(in);
*sP = reader.get_be_int16();
return (!in->eof() && !in->fail()) ? 0 : -1;
}
int
pm_writebigshort( FILE* out, short s )
{
(void) putc( ( s >> 8 ) & 0xff, out );
(void) putc( s & 0xff, out );
return 0;
}
pm_writebigshort(ostream *out, short s) {
StreamWriter writer(out);
writer.add_be_int16(s);
return (!out->fail()) ? 0 : -1;
}
int
pm_readbiglong(FILE* in, long* lP)
{
int c;
if ( (c = getc( in )) == EOF )
return -1;
*lP = ( c & 0xff ) << 24;
if ( (c = getc( in )) == EOF )
return -1;
*lP |= ( c & 0xff ) << 16;
if ( (c = getc( in )) == EOF )
return -1;
*lP |= ( c & 0xff ) << 8;
if ( (c = getc( in )) == EOF )
return -1;
*lP |= c & 0xff;
return 0;
}
pm_readbiglong(istream *in, long *lP) {
StreamReader reader(in);
*lP = reader.get_be_int32();
return (!in->eof() && !in->fail()) ? 0 : -1;
}
int
pm_writebiglong(FILE* out, long l)
{
(void) putc( ( l >> 24 ) & 0xff, out );
(void) putc( ( l >> 16 ) & 0xff, out );
(void) putc( ( l >> 8 ) & 0xff, out );
(void) putc( l & 0xff, out );
return 0;
}
pm_writebiglong(ostream *out, long l) {
StreamWriter writer(out);
writer.add_be_int32(l);
return (!out->fail()) ? 0 : -1;
}
int
pm_readlittleshort(FILE* in, short* sP)
{
int c;
if ( (c = getc( in )) == EOF )
return -1;
*sP = c & 0xff;
if ( (c = getc( in )) == EOF )
return -1;
*sP |= ( c & 0xff ) << 8;
return 0;
}
pm_readlittleshort(istream *in, short *sP) {
StreamReader reader(in);
*sP = reader.get_int16();
return (!in->eof() && !in->fail()) ? 0 : -1;
}
int
pm_writelittleshort( FILE* out, short s )
{
(void) putc( s & 0xff, out );
(void) putc( ( s >> 8 ) & 0xff, out );
return 0;
}
pm_writelittleshort(ostream *out, short s) {
StreamWriter writer(out);
writer.add_int16(s);
return (!out->fail()) ? 0 : -1;
}
int
pm_readlittlelong(FILE* in, long* lP)
{
int c;
if ( (c = getc( in )) == EOF )
return -1;
*lP = c & 0xff;
if ( (c = getc( in )) == EOF )
return -1;
*lP |= ( c & 0xff ) << 8;
if ( (c = getc( in )) == EOF )
return -1;
*lP |= ( c & 0xff ) << 16;
if ( (c = getc( in )) == EOF )
return -1;
*lP |= ( c & 0xff ) << 24;
return 0;
}
pm_readlittlelong(istream *in, long *lP) {
StreamReader reader(in);
*lP = reader.get_int32();
return (!in->eof() && !in->fail()) ? 0 : -1;
}
int
pm_writelittlelong(FILE* out, long l)
{
(void) putc( l & 0xff, out );
(void) putc( ( l >> 8 ) & 0xff, out );
(void) putc( ( l >> 16 ) & 0xff, out );
(void) putc( ( l >> 24 ) & 0xff, out );
return 0;
}
pm_writelittlelong(ostream *out, long l) {
StreamWriter writer(out);
writer.add_int32(l);
return (!out->fail()) ? 0 : -1;
}

View File

@ -25,7 +25,6 @@
#include "pandabase.h"
#include <string>
#include <stdio.h>
// Since we no longer include pnm.h directly, we have to provide our
// own definitions for xel and xelval.
@ -85,14 +84,14 @@ EXPCL_PANDA int pm_bitstomaxval(int bits);
EXPCL_PANDA char *pm_allocrow(int cols, int size);
EXPCL_PANDA void pm_freerow(char *itrow);
EXPCL_PANDA int pm_readbigshort(FILE *in, short *sP);
EXPCL_PANDA int pm_writebigshort(FILE *out, short s);
EXPCL_PANDA int pm_readbiglong(FILE *in, long *lP);
EXPCL_PANDA int pm_writebiglong(FILE *out, long l);
EXPCL_PANDA int pm_readlittleshort(FILE *in, short *sP);
EXPCL_PANDA int pm_writelittleshort(FILE *out, short s);
EXPCL_PANDA int pm_readlittlelong(FILE *in, long *lP);
EXPCL_PANDA int pm_writelittlelong(FILE *out, long l);
EXPCL_PANDA int pm_readbigshort(istream *in, short *sP);
EXPCL_PANDA int pm_writebigshort(ostream *out, short s);
EXPCL_PANDA int pm_readbiglong(istream *in, long *lP);
EXPCL_PANDA int pm_writebiglong(ostream *out, long l);
EXPCL_PANDA int pm_readlittleshort(istream *in, short *sP);
EXPCL_PANDA int pm_writelittleshort(ostream *out, short s);
EXPCL_PANDA int pm_readlittlelong(istream *in, long *lP);
EXPCL_PANDA int pm_writelittlelong(ostream *out, long l);
// These ratios are used to compute the brightness of a colored pixel; they

View File

@ -14,10 +14,9 @@
#define SOURCES \
config_pnmimagetypes.h pnmFileTypeAlias.h pnmFileTypeBMP.h \
pnmFileTypeIMG.h pnmFileTypeRadiance.h \
pnmFileTypeIMG.h \
pnmFileTypeSGI.h pnmFileTypeSoftImage.h \
pnmFileTypeTGA.h pnmFileTypeYUV.h color.c colrops.c resolu.c \
header.c \
pnmFileTypeTGA.h \
$[if $[HAVE_TIFF], pnmFileTypeTIFF.cxx pnmFileTypeTIFF.h] \
$[if $[HAVE_JPEG], pnmFileTypeJPG.h] \
$[if $[HAVE_JPEG2000], pnmFileTypeJPG2000.h]
@ -26,10 +25,10 @@
config_pnmimagetypes.cxx pnmFileTypeAlias.cxx \
pnmFileTypeBMPReader.cxx pnmFileTypeBMPWriter.cxx \
pnmFileTypeIMG.cxx pnmFileTypeBMP.cxx \
pnmFileTypeRadiance.cxx pnmFileTypeSGI.cxx \
pnmFileTypeSGI.cxx \
pnmFileTypeSGIReader.cxx pnmFileTypeSGIWriter.cxx \
pnmFileTypeSoftImage.cxx \
pnmFileTypeTGA.cxx pnmFileTypeYUV.cxx \
pnmFileTypeTGA.cxx \
$[if $[HAVE_JPEG], pnmFileTypeJPG.cxx pnmFileTypeJPGReader.cxx pnmFileTypeJPGWriter.cxx] \
$[if $[HAVE_JPEG2000], pnmFileTypeJPG2000.cxx pnmFileTypeJPG2000Reader.cxx pnmFileTypeJPG2000Writer.cxx]

View File

@ -1,292 +0,0 @@
/* Filename: color.c
* Created by:
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* PANDA 3D SOFTWARE
* Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
*
* All use of this software is subject to the terms of the Panda 3d
* Software license. You should have received a copy of this license
* along with this source code; you will also find a current copy of
* the license at http://www.panda3d.org/license.txt .
*
* To contact the maintainers of this program write to
* panda3d@yahoogroups.com .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* color.c - routines for color calculations.
*
* 10/10/85
*/
#include <stdio.h>
#include <math.h>
#include "color.h"
#ifdef WIN32_VC
#include <malloc.h>
#endif
#define MINELEN 8 /* minimum scanline length for encoding */
#define MAXELEN 0x7fff /* maximum scanline length for encoding */
#define MINRUN 4 /* minimum run length */
void setcolr(register COLR, double, double, double);
void colr_color(register COLOR, register COLR);
char *
tempbuffer(unsigned len) /* get a temporary buffer */
{
#ifndef WIN32_VC
extern char *malloc(size_t), *realloc(void *, size_t);
#endif
static char *tempbuf = NULL;
static unsigned tempbuflen = 0;
if (len > tempbuflen) {
if (tempbuflen > 0)
tempbuf = (char *)realloc(tempbuf, len);
else
tempbuf = (char *)malloc(len);
tempbuflen = tempbuf==NULL ? 0 : len;
}
return(tempbuf);
}
int fwritecolrs(register COLR *scanline, unsigned len, register FILE *fp)
/* write out a colr scanline */
{
register int i, j, beg, cnt;
int c2;
if (len < MINELEN || len > MAXELEN) /* OOBs, write out flat */
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
/* put magic header */
putc(2, fp);
putc(2, fp);
putc(len>>8, fp);
putc(len&255, fp);
/* put components seperately */
for (i = 0; i < 4; i++) {
for (j = 0; j < (int)len; j += cnt) { /* find next run */
for (beg = j; beg < (int)len; beg += cnt) {
for (cnt = 1; cnt < 127 && ((beg+cnt) < (int)len) &&
(scanline[beg+cnt][i] == scanline[beg][i]); cnt++)
;
if (cnt >= MINRUN)
break; /* long enough */
}
if (beg-j > 1 && beg-j < MINRUN) {
c2 = j+1;
while (scanline[c2++][i] == scanline[j][i])
if (c2 == beg) { /* short run */
putc(128+beg-j, fp);
putc(scanline[j][i], fp);
j = beg;
break;
}
}
while (j < beg) { /* write out non-run */
if ((c2 = beg-j) > 128) c2 = 128;
putc(c2, fp);
while (c2--)
putc(scanline[j++][i], fp);
}
if (cnt >= MINRUN) { /* write out run */
putc(128+cnt, fp);
putc(scanline[beg][i], fp);
} else
cnt = 0;
}
}
return(ferror(fp) ? -1 : 0);
}
int oldreadcolrs(register COLR *scanline, int len, register FILE *fp);
int freadcolrs(register COLR *scanline, int len, register FILE *fp)
/* read in an encoded colr scanline */
{
register int i, j;
int code, val;
/* determine scanline type */
if (len < MINELEN || len > MAXELEN)
return(oldreadcolrs(scanline, len, fp));
if ((i = getc(fp)) == EOF)
return(-1);
if (i != 2) {
ungetc(i, fp);
return(oldreadcolrs(scanline, len, fp));
}
scanline[0][GRN] = getc(fp);
scanline[0][BLU] = getc(fp);
if ((i = getc(fp)) == EOF)
return(-1);
if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
scanline[0][RED] = 2;
scanline[0][EXP] = i;
return(oldreadcolrs(scanline+1, len-1, fp));
}
if ((scanline[0][BLU]<<8 | i) != len)
return(-1); /* length mismatch! */
/* read each component */
for (i = 0; i < 4; i++)
for (j = 0; j < len; ) {
if ((code = getc(fp)) == EOF)
return(-1);
if (code > 128) { /* run */
code &= 127;
val = getc(fp);
while (code--)
scanline[j++][i] = val;
} else /* non-run */
while (code--)
scanline[j++][i] = getc(fp);
}
return(feof(fp) ? -1 : 0);
}
/* read in an old colr scanline */
int oldreadcolrs(register COLR *scanline, int len, register FILE *fp)
{
int rshift;
register int i;
rshift = 0;
while (len > 0) {
scanline[0][RED] = getc(fp);
scanline[0][GRN] = getc(fp);
scanline[0][BLU] = getc(fp);
scanline[0][EXP] = getc(fp);
if (feof(fp) || ferror(fp))
return(-1);
if (scanline[0][RED] == 1 &&
scanline[0][GRN] == 1 &&
scanline[0][BLU] == 1) {
for (i = scanline[0][EXP] << rshift; i > 0; i--) {
copycolr(scanline[0], scanline[-1]);
scanline++;
len--;
}
rshift += 8;
} else {
scanline++;
len--;
rshift = 0;
}
}
return(0);
}
/* write out a scanline */
int fwritescan(register COLOR *scanline, int len, FILE *fp)
{
COLR *clrscan;
int n;
register COLR *sp;
/* get scanline buffer */
if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
return(-1);
clrscan = sp;
/* convert scanline */
n = len;
while (n-- > 0) {
setcolr(sp[0], scanline[0][RED],
scanline[0][GRN],
scanline[0][BLU]);
scanline++;
sp++;
}
return(fwritecolrs(clrscan, len, fp));
}
int freadscan(register COLOR *scanline, int len, FILE *fp)
/* read in a scanline */
{
register COLR *clrscan;
if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
return(-1);
if (freadcolrs(clrscan, len, fp) < 0)
return(-1);
/* convert scanline */
colr_color(scanline[0], clrscan[0]);
while (--len > 0) {
scanline++; clrscan++;
if (clrscan[0][RED] == clrscan[-1][RED] &&
clrscan[0][GRN] == clrscan[-1][GRN] &&
clrscan[0][BLU] == clrscan[-1][BLU] &&
clrscan[0][EXP] == clrscan[-1][EXP])
copycolor(scanline[0], scanline[-1]);
else
colr_color(scanline[0], clrscan[0]);
}
return(0);
}
void
setcolr(register COLR clr, double r, double g, double b)
/* assign a short color value */
{
double d;
int e;
d = r > g ? r : g;
if (b > d) d = b;
if (d <= 1e-32) {
clr[RED] = clr[GRN] = clr[BLU] = 0;
clr[EXP] = 0;
return;
}
d = frexp(d, &e) * 255.9999 / d;
clr[RED] = (BYTE) (r * d);
clr[GRN] = (BYTE) (g * d);
clr[BLU] = (BYTE) (b * d);
clr[EXP] = (BYTE) (e) + COLXS;
}
void
colr_color(register COLOR col, register COLR clr)
/* convert short to float color */
{
double f;
if (clr[EXP] == 0)
col[RED] = col[GRN] = col[BLU] = 0.0;
else {
f = ldexp(1.0, (int)clr[EXP]-(COLXS+8));
col[RED] = (float) ((clr[RED] + 0.5)*f);
col[GRN] = (float) ((clr[GRN] + 0.5)*f);
col[BLU] = (float) ((clr[BLU] + 0.5)*f);
}
}
int
bigdiff(register COLOR c1, register COLOR c2, double md)
/* c1 delta c2 > md? */
{
register int i;
for (i = 0; i < 3; i++)
if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
colval(c2,i)-colval(c1,i) > md*colval(c1,i))
return(1);
return(0);
}

View File

@ -1,158 +0,0 @@
/* Filename: color.h
* Created by:
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* PANDA 3D SOFTWARE
* Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
*
* All use of this software is subject to the terms of the Panda 3d
* Software license. You should have received a copy of this license
* along with this source code; you will also find a current copy of
* the license at http://www.panda3d.org/license.txt .
*
* To contact the maintainers of this program write to
* panda3d@yahoogroups.com .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* SCCSid "@(#)color.h 2.9 9/2/94 LBL" */
/*
* color.h - header for routines using pixel color values.
*
* 12/31/85
*
* Two color representations are used, one for calculation and
* another for storage. Calculation is done with three floats
* for speed. Stored color values use 4 bytes which contain
* three single byte mantissas and a common exponent.
*/
#define RED 0
#define GRN 1
#define BLU 2
#define EXP 3
#define COLXS 128 /* excess used for exponent */
typedef unsigned char BYTE; /* 8-bit unsigned integer */
typedef BYTE COLR[4]; /* red, green, blue, exponent */
#define copycolr(c1,c2) (c1[0]=c2[0],c1[1]=c2[1], \
c1[2]=c2[2],c1[3]=c2[3])
typedef float COLOR[3]; /* red, green, blue */
#define colval(col,pri) ((col)[pri])
#define setcolor(col,r,g,b) ((col)[RED]=(r),(col)[GRN]=(g),(col)[BLU]=(b))
#define copycolor(c1,c2) ((c1)[0]=(c2)[0],(c1)[1]=(c2)[1],(c1)[2]=(c2)[2])
#define scalecolor(col,sf) ((col)[0]*=(sf),(col)[1]*=(sf),(col)[2]*=(sf))
#define addcolor(c1,c2) ((c1)[0]+=(c2)[0],(c1)[1]+=(c2)[1],(c1)[2]+=(c2)[2])
#define multcolor(c1,c2) ((c1)[0]*=(c2)[0],(c1)[1]*=(c2)[1],(c1)[2]*=(c2)[2])
#ifdef NTSC
#define CIE_x_r 0.670 /* standard NTSC primaries */
#define CIE_y_r 0.330
#define CIE_x_g 0.210
#define CIE_y_g 0.710
#define CIE_x_b 0.140
#define CIE_y_b 0.080
#define CIE_x_w 0.3333 /* use true white */
#define CIE_y_w 0.3333
#else
#define CIE_x_r 0.640 /* nominal CRT primaries */
#define CIE_y_r 0.330
#define CIE_x_g 0.290
#define CIE_y_g 0.600
#define CIE_x_b 0.150
#define CIE_y_b 0.060
#define CIE_x_w 0.3333 /* use true white */
#define CIE_y_w 0.3333
#endif
#define CIE_D ( CIE_x_r*(CIE_y_g - CIE_y_b) + \
CIE_x_g*(CIE_y_b - CIE_y_r) + \
CIE_x_b*(CIE_y_r - CIE_y_g) )
#define CIE_C_rD ( (1./CIE_y_w) * \
( CIE_x_w*(CIE_y_g - CIE_y_b) - \
CIE_y_w*(CIE_x_g - CIE_x_b) + \
CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g ) )
#define CIE_C_gD ( (1./CIE_y_w) * \
( CIE_x_w*(CIE_y_b - CIE_y_r) - \
CIE_y_w*(CIE_x_b - CIE_x_r) - \
CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r ) )
#define CIE_C_bD ( (1./CIE_y_w) * \
( CIE_x_w*(CIE_y_r - CIE_y_g) - \
CIE_y_w*(CIE_x_r - CIE_x_g) + \
CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r ) )
#define CIE_rf (CIE_y_r*CIE_C_rD/CIE_D)
#define CIE_gf (CIE_y_g*CIE_C_gD/CIE_D)
#define CIE_bf (CIE_y_b*CIE_C_bD/CIE_D)
/* As of 9-94, CIE_rf=.265074126, CIE_gf=.670114631 and CIE_bf=.064811243 */
#define bright(col) (CIE_rf*(col)[RED]+CIE_gf*(col)[GRN]+CIE_bf*(col)[BLU])
#define normbright(c) ( ( (long)(CIE_rf*256.+.5)*(c)[RED] + \
(long)(CIE_gf*256.+.5)*(c)[GRN] + \
(long)(CIE_bf*256.+.5)*(c)[BLU] ) >> 8 )
/* luminous efficacies over visible spectrum */
#define MAXEFFICACY 683. /* defined maximum at 550 nm */
#define WHTEFFICACY 179. /* uniform white light */
#define D65EFFICACY 203. /* standard illuminant D65 */
#define INCEFFICACY 160. /* illuminant A (incand.) */
#define SUNEFFICACY 208. /* illuminant B (solar dir.) */
#define SKYEFFICACY D65EFFICACY /* skylight */
#define DAYEFFICACY D65EFFICACY /* combined sky and solar */
#define luminance(col) (WHTEFFICACY * bright(col))
#define intens(col) ( (col)[0] > (col)[1] \
? (col)[0] > (col)[2] ? (col)[0] : (col)[2] \
: (col)[1] > (col)[2] ? (col)[1] : (col)[2] )
#define colrval(c,p) ( (c)[EXP] ? \
ldexp((c)[p]+.5,(int)(c)[EXP]-(COLXS+8)) : \
0. )
#define WHTCOLOR {1.0,1.0,1.0}
#define BLKCOLOR {0.0,0.0,0.0}
#define WHTCOLR {128,128,128,COLXS+1}
#define BLKCOLR {0,0,0,0}
/* picture format identifier */
#define COLRFMT "32-bit_rle_rgbe"
/* macros for exposures */
#define EXPOSSTR "EXPOSURE="
#define LEXPOSSTR 9
#define isexpos(hl) (!strncmp(hl,EXPOSSTR,LEXPOSSTR))
#define exposval(hl) atof((hl)+LEXPOSSTR)
#define fputexpos(ex,fp) fprintf(fp,"%s%e\n",EXPOSSTR,ex)
/* macros for pixel aspect ratios */
#define ASPECTSTR "PIXASPECT="
#define LASPECTSTR 10
#define isaspect(hl) (!strncmp(hl,ASPECTSTR,LASPECTSTR))
#define aspectval(hl) atof((hl)+LASPECTSTR)
#define fputaspect(pa,fp) fprintf(fp,"%s%f\n",ASPECTSTR,pa)
/* macros for color correction */
#define COLCORSTR "COLORCORR="
#define LCOLCORSTR 10
#define iscolcor(hl) (!strncmp(hl,COLCORSTR,LCOLCORSTR))
#define colcorval(cc,hl) sscanf(hl+LCOLCORSTR,"%f %f %f", \
&(cc)[RED],&(cc)[GRN],&(cc)[BLU])
#define fputcolcor(cc,fp) fprintf(fp,"%s %f %f %f\n",COLCORSTR, \
(cc)[RED],(cc)[GRN],(cc)[BLU])
#ifdef DCL_ATOF
extern double atof(), ldexp(), frexp();
#endif

View File

@ -19,9 +19,7 @@
#include "config_pnmimagetypes.h"
#include "pnmFileTypeSGI.h"
#include "pnmFileTypeAlias.h"
#include "pnmFileTypeRadiance.h"
#include "pnmFileTypeTGA.h"
#include "pnmFileTypeYUV.h"
#include "pnmFileTypeIMG.h"
#include "pnmFileTypeSoftImage.h"
#include "pnmFileTypeBMP.h"
@ -42,10 +40,8 @@ Configure(config_pnmimagetypes);
NotifyCategoryDef(pnmimage_pnm, pnmimage_cat);
NotifyCategoryDef(pnmimage_sgi, pnmimage_cat);
NotifyCategoryDef(pnmimage_alias, pnmimage_cat);
NotifyCategoryDef(pnmimage_radiance, pnmimage_cat);
NotifyCategoryDef(pnmimage_tiff, pnmimage_cat);
NotifyCategoryDef(pnmimage_tga, pnmimage_cat);
NotifyCategoryDef(pnmimage_yuv, pnmimage_cat);
NotifyCategoryDef(pnmimage_img, pnmimage_cat);
NotifyCategoryDef(pnmimage_soft, pnmimage_cat);
NotifyCategoryDef(pnmimage_bmp, pnmimage_cat);
@ -54,15 +50,6 @@ NotifyCategoryDef(pnmimage_jpg2000, pnmimage_cat);
int sgi_storage_type = STORAGE_RLE;
const string sgi_imagename = config_pnmimagetypes.GetString("sgi-imagename", "");
const double radiance_gamma_correction = config_pnmimagetypes.GetDouble("radiance-gamma-correction", 2.2);
const int radiance_brightness_adjustment = config_pnmimagetypes.GetInt("radiance-brightness-adjustment", 0);
// YUV format doesn't include an image size specification, so the
// image size must be specified externally. The defaults here are
// likely candidates, since this is the Abekas native size; the ysize
// is automatically adjusted down to account for a short file.
const int yuv_xsize = config_pnmimagetypes.GetInt("yuv-xsize", 720);
const int yuv_ysize = config_pnmimagetypes.GetInt("yuv-ysize", 486);
// TGA supports RLE compression, as well as colormapping and/or
// grayscale images. Set these true to enable these features, if
@ -124,9 +111,7 @@ init_libpnmimagetypes() {
init_libpnmimage();
PNMFileTypeSGI::init_type();
PNMFileTypeAlias::init_type();
PNMFileTypeRadiance::init_type();
PNMFileTypeTGA::init_type();
PNMFileTypeYUV::init_type();
PNMFileTypeIMG::init_type();
PNMFileTypeSoftImage::init_type();
PNMFileTypeBMP::init_type();
@ -169,9 +154,7 @@ init_libpnmimagetypes() {
tr->register_type(new PNMFileTypeSGI);
tr->register_type(new PNMFileTypeAlias);
tr->register_type(new PNMFileTypeRadiance);
tr->register_type(new PNMFileTypeTGA);
tr->register_type(new PNMFileTypeYUV);
tr->register_type(new PNMFileTypeIMG);
tr->register_type(new PNMFileTypeSoftImage);
tr->register_type(new PNMFileTypeBMP);
@ -188,9 +171,7 @@ init_libpnmimagetypes() {
// Also register with the Bam reader.
PNMFileTypeSGI::register_with_read_factory();
PNMFileTypeAlias::register_with_read_factory();
PNMFileTypeRadiance::register_with_read_factory();
PNMFileTypeTGA::register_with_read_factory();
PNMFileTypeYUV::register_with_read_factory();
PNMFileTypeIMG::register_with_read_factory();
PNMFileTypeSoftImage::register_with_read_factory();
PNMFileTypeBMP::register_with_read_factory();

View File

@ -1,242 +0,0 @@
/* Filename: header.c
* Created by:
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* PANDA 3D SOFTWARE
* Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
*
* All use of this software is subject to the terms of the Panda 3d
* Software license. You should have received a copy of this license
* along with this source code; you will also find a current copy of
* the license at http://www.panda3d.org/license.txt .
*
* To contact the maintainers of this program write to
* panda3d@yahoogroups.com .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* header.c - routines for reading and writing information headers.
*
* 8/19/88
*
* newheader(t,fp) start new information header identified by string t
* isheadid(s) returns true if s is a header id line
* headidval(r,s) copy header identifier value in s to r
* printargs(ac,av,fp) print an argument list to fp, followed by '\n'
* isformat(s) returns true if s is of the form "FORMAT=*"
* formatval(r,s) copy the format value in s to r
* fputformat(s,fp) write "FORMAT=%s" to fp
* getheader(fp,f,p) read header from fp, calling f(s,p) on each line
* checkheader(i,p,o) check header format from i against p and copy to o
*
* To copy header from input to output, use getheader(fin, fputs, fout)
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXLINE 512
char HDRSTR[] = "#?"; /* information header magic number */
char FMTSTR[] = "FORMAT="; /* format identifier */
void
newheader(char *s, register FILE *fp) /* identifying line of information header */
{
fputs(HDRSTR, fp);
fputs(s, fp);
putc('\n', fp);
}
int
headidval(register char *r, register char *s) /* get header id (return true if is id) */
{
register char *cp = HDRSTR;
while (*cp) if (*cp++ != *s++) return(0);
if (r == NULL) return(1);
while (*s) *r++ = *s++;
*r = '\0';
return(1);
}
int
isheadid(char *s) /* check to see if line is header id */
{
return(headidval(NULL, s));
}
void
printargs(int ac, char **av, register FILE *fp) /* print arguments to a file */
{
int quote;
while (ac-- > 0) {
if (strchr(*av, ' ') != NULL) { /* quote it */
if (strchr(*av, '\'') != NULL)
quote = '"';
else
quote = '\'';
putc(quote, fp);
fputs(*av++, fp);
putc(quote, fp);
} else
fputs(*av++, fp);
putc(' ', fp);
}
putc('\n', fp);
}
int
formatval(register char *r, register char *s) /* get format value (return true if format) */
{
register char *cp = FMTSTR;
while (*cp) if (*cp++ != *s++) return(0);
while (isspace(*s)) s++;
if (!*s) return(0);
if (r == NULL) return(1);
while(*s) *r++ = *s++;
while (isspace(r[-1])) r--;
*r = '\0';
return(1);
}
int
isformat(char *s) /* is line a format line? */
{
return(formatval(NULL, s));
}
void
fputformat(char *s, FILE *fp) /* put out a format value */
{
fputs(FMTSTR, fp);
fputs(s, fp);
putc('\n', fp);
}
int
getheader(FILE *fp, int (*f)(char *, char *), char *p) /* get header from file */
{
char buf[MAXLINE];
for ( ; ; ) {
buf[MAXLINE-2] = '\n';
if (fgets(buf, MAXLINE, fp) == NULL)
return(-1);
if (buf[0] == '\n')
return(0);
#ifdef MSDOS
if (buf[0] == '\r' && buf[1] == '\n')
return(0);
#endif
if (buf[MAXLINE-2] != '\n') {
ungetc(buf[MAXLINE-2], fp); /* prevent false end */
buf[MAXLINE-2] = '\0';
}
if (f != NULL)
(*f)(buf, p);
}
}
struct check {
FILE *fp;
char fs[64];
};
static void
mycheck(char *s, register struct check *cp) /* check a header line for format info. */
{
if (!formatval(cp->fs, s) && cp->fp != NULL)
fputs(s, cp->fp);
}
/*
* Copymatch(pat,str) checks pat for wildcards, and
* copies str into pat if there is a match (returning true).
*/
#ifdef COPYMATCH
copymatch(char *pat, char *str)
{
int docopy = 0;
register char *p = pat, *s = str;
do {
switch (*p) {
case '?': /* match any character */
if (!*s++)
return(0);
docopy++;
break;
case '*': /* match any string */
while (p[1] == '*') p++;
do
if ( (p[1]=='?' || p[1]==*s)
&& copymatch(p+1,s) ) {
strcpy(pat, str);
return(1);
}
while (*s++);
return(0);
case '\\': /* literal next */
p++;
/* fall through */
default: /* normal character */
if (*p != *s)
return(0);
s++;
break;
}
} while (*p++);
if (docopy)
strcpy(pat, str);
return(1);
}
#else
#define copymatch(pat, s) (!strcmp(pat, s))
#endif
/*
* Checkheader(fin,fmt,fout) returns a value of 1 if the input format
* matches the specification in fmt, 0 if no input format was found,
* and -1 if the input format does not match or there is an
* error reading the header. If fmt is empty, then -1 is returned
* if any input format is found (or there is an error), and 0 otherwise.
* If fmt contains any '*' or '?' characters, then checkheader
* does wildcard expansion and copies a matching result into fmt.
* Be sure that fmt is big enough to hold the match in such cases!
* The input header (minus any format lines) is copied to fout
* if fout is not NULL.
*/
int
checkheader(FILE *fin, char *fmt, FILE *fout)
{
struct check cdat;
cdat.fp = fout;
cdat.fs[0] = '\0';
if (getheader(fin, (int (*)(char *, char *))mycheck, (char *)&cdat) < 0)
return(-1);
if (cdat.fs[0] != '\0')
return(copymatch(fmt, cdat.fs) ? 1 : -1);
return(0);
}

View File

@ -27,10 +27,10 @@
// or height is larger than this, it must be bogus.
#define INSANE_SIZE 20000
static const char * const extensions_ALIAS[] = {
static const char * const extensions_alias[] = {
"pix", "als"
};
static const int num_extensions_ALIAS = sizeof(extensions_ALIAS) / sizeof(const char *);
static const int num_extensions_alias = sizeof(extensions_alias) / sizeof(const char *);
TypeHandle PNMFileTypeAlias::_type_handle;
@ -57,11 +57,11 @@ get_name() const {
// Function: PNMFileTypeAlias::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_ALIAS associated with this particular file type.
// extensions associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeAlias::
get_num_extensions() const {
return num_extensions_ALIAS;
return num_extensions_alias;
}
////////////////////////////////////////////////////////////////////
@ -73,8 +73,8 @@ get_num_extensions() const {
////////////////////////////////////////////////////////////////////
string PNMFileTypeAlias::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_ALIAS, string());
return extensions_ALIAS[n];
nassertr(n >= 0 && n < num_extensions_alias, string());
return extensions_alias[n];
}
////////////////////////////////////////////////////////////////////
@ -97,7 +97,7 @@ get_suggested_extension() const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeAlias::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
make_reader(istream *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
@ -110,7 +110,7 @@ make_reader(FILE *file, bool owns_file, const string &magic_number) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeAlias::
make_writer(FILE *file, bool owns_file) {
make_writer(ostream *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}
@ -118,26 +118,26 @@ make_writer(FILE *file, bool owns_file) {
inline unsigned short
read_ushort(FILE *file) {
read_ushort(istream *file) {
unsigned short x;
return pm_readbigshort(file, (short *)&x)==0 ? x : 0;
}
inline unsigned char
read_uchar_ALIAS(FILE *file) {
read_uchar_ALIAS(istream *file) {
int x;
x = getc(file);
x = file->get();
return (x!=EOF) ? (unsigned char)x : 0;
}
inline void
write_ushort(FILE *file, unsigned short x) {
write_ushort(ostream *file, unsigned short x) {
pm_writebigshort(file, (short)x);
}
inline void
write_uchar_ALIAS(FILE *file, unsigned char x) {
putc(x, file);
write_uchar_ALIAS(ostream *file, unsigned char x) {
file->put(x);
}
////////////////////////////////////////////////////////////////////
@ -146,7 +146,7 @@ write_uchar_ALIAS(FILE *file, unsigned char x) {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeAlias::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
if (!read_magic_number(_file, magic_number, 4)) {
@ -270,7 +270,7 @@ static unsigned char last_red = 0, last_blu = 0, last_grn = 0;
static int num_count = 0;
static void
flush_color(FILE *file) {
flush_color(ostream *file) {
if (num_count>0) {
write_uchar_ALIAS(file, num_count);
write_uchar_ALIAS(file, last_blu);
@ -281,7 +281,7 @@ flush_color(FILE *file) {
}
static void
write_color(FILE *file,
write_color(ostream *file,
unsigned char red, unsigned char blu, unsigned char grn) {
if (red==last_red && blu==last_blu && grn==last_grn && num_count<0377) {
num_count++;
@ -301,7 +301,7 @@ write_color(FILE *file,
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeAlias::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
Writer(PNMFileType *type, ostream *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
}

View File

@ -19,11 +19,11 @@
#ifndef PNMFILETYPEALIAS_H
#define PNMFILETYPEALIAS_H
#include <pandabase.h>
#include "pandabase.h"
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
#include "pnmFileType.h"
#include "pnmReader.h"
#include "pnmWriter.h"
////////////////////////////////////////////////////////////////////
// Class : PNMFileTypeAlias
@ -39,14 +39,14 @@ public:
virtual string get_extension(int n) const;
virtual string get_suggested_extension() const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
virtual bool supports_read_row() const;
virtual bool read_row(xel *array, xelval *alpha);
@ -54,7 +54,7 @@ public:
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
Writer(PNMFileType *type, ostream *file, bool owns_file);
virtual bool supports_write_row() const;
virtual bool write_header();

View File

@ -19,13 +19,13 @@
#include "pnmFileTypeBMP.h"
#include "config_pnmimagetypes.h"
#include <pnmFileTypeRegistry.h>
#include <bamReader.h>
#include "pnmFileTypeRegistry.h"
#include "bamReader.h"
static const char * const extensions_BMP[] = {
static const char * const extensions_bmp[] = {
"bmp"
};
static const int num_extensions_BMP = sizeof(extensions_BMP) / sizeof(const char *);
static const int num_extensions_bmp = sizeof(extensions_bmp) / sizeof(const char *);
TypeHandle PNMFileTypeBMP::_type_handle;
@ -52,11 +52,11 @@ get_name() const {
// Function: PNMFileTypeBMP::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_BMP associated with this particular file type.
// extensions associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeBMP::
get_num_extensions() const {
return num_extensions_BMP;
return num_extensions_bmp;
}
////////////////////////////////////////////////////////////////////
@ -68,8 +68,8 @@ get_num_extensions() const {
////////////////////////////////////////////////////////////////////
string PNMFileTypeBMP::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_BMP, string());
return extensions_BMP[n];
nassertr(n >= 0 && n < num_extensions_bmp, string());
return extensions_bmp[n];
}
////////////////////////////////////////////////////////////////////
@ -116,7 +116,7 @@ matches_magic_number(const string &magic_number) const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeBMP::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
make_reader(istream *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
@ -129,7 +129,7 @@ make_reader(FILE *file, bool owns_file, const string &magic_number) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeBMP::
make_writer(FILE *file, bool owns_file) {
make_writer(ostream *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}

View File

@ -19,11 +19,11 @@
#ifndef PNMFILETYPEBMP_H
#define PNMFILETYPEBMP_H
#include <pandabase.h>
#include "pandabase.h"
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
#include "pnmFileType.h"
#include "pnmReader.h"
#include "pnmWriter.h"
////////////////////////////////////////////////////////////////////
// Class : PNMFileTypeBMP
@ -42,14 +42,14 @@ public:
virtual bool has_magic_number() const;
virtual bool matches_magic_number(const string &magic_number) const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
virtual int read_data(xel *array, xelval *alpha);
@ -69,7 +69,7 @@ public:
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
Writer(PNMFileType *type, ostream *file, bool owns_file);
virtual int write_data(xel *array, xelval *alpha);
};

View File

@ -43,16 +43,16 @@
* Utilities
*/
static int GetByte (FILE * fp);
static short GetShort (FILE * fp);
static long GetLong (FILE * fp);
static void readto (FILE *fp, unsigned long *ppos, unsigned long dst);
static void BMPreadfileheader (FILE *fp, unsigned long *ppos,
static int GetByte (istream * fp);
static short GetShort (istream * fp);
static long GetLong (istream * fp);
static void readto (istream *fp, unsigned long *ppos, unsigned long dst);
static void BMPreadfileheader (istream *fp, unsigned long *ppos,
unsigned long *poffBits);
static void BMPreadinfoheader (FILE *fp, unsigned long *ppos,
static void BMPreadinfoheader (istream *fp, unsigned long *ppos,
unsigned long *pcx, unsigned long *pcy, unsigned short *pcBitCount,
int *pclassv);
static int BMPreadrgbtable (FILE *fp, unsigned long *ppos,
static int BMPreadrgbtable (istream *fp, unsigned long *ppos,
unsigned short cBitCount, int classv, pixval *R, pixval *G, pixval *B);
static const char *ifname = "BMP";
@ -60,11 +60,11 @@ static char er_read[] = "%s: read error";
//static char er_seek[] = "%s: seek error";
static int
GetByte(FILE *fp)
GetByte(istream *fp)
{
int v;
if ((v = getc(fp)) == EOF)
if ((v = fp->get()) == EOF)
{
pm_error(er_read, ifname);
}
@ -73,7 +73,7 @@ GetByte(FILE *fp)
}
static short
GetShort(FILE *fp)
GetShort(istream *fp)
{
short v;
@ -86,7 +86,7 @@ GetShort(FILE *fp)
}
static long
GetLong(FILE *fp)
GetLong(istream *fp)
{
long v;
@ -104,7 +104,7 @@ GetLong(FILE *fp)
*/
static void
readto(FILE *fp,
readto(istream *fp,
unsigned long *ppos, /* pointer to number of bytes read from fp */
unsigned long dst)
{
@ -120,7 +120,7 @@ readto(FILE *fp,
for(; pos < dst; pos++)
{
if (getc(fp) == EOF)
if (fp->get() == EOF)
{
pm_error(er_read, ifname);
}
@ -136,7 +136,7 @@ readto(FILE *fp,
static void
BMPreadfileheader(
FILE *fp,
istream *fp,
unsigned long *ppos, /* number of bytes read from fp */
unsigned long *poffBits)
{
@ -171,7 +171,7 @@ BMPreadfileheader(
static void
BMPreadinfoheader(
FILE *fp,
istream *fp,
unsigned long *ppos, /* number of bytes read from fp */
unsigned long *pcx,
unsigned long *pcy,
@ -269,7 +269,7 @@ BMPreadinfoheader(
*/
static int
BMPreadrgbtable(
FILE *fp,
istream *fp,
unsigned long *ppos, /* number of bytes read from fp */
unsigned short cBitCount,
int classv,
@ -305,7 +305,7 @@ BMPreadrgbtable(
*/
static int
BMPreadrow(
FILE *fp,
istream *fp,
unsigned long *ppos, /* number of bytes read from fp */
pixel *row,
unsigned long cx,
@ -371,7 +371,7 @@ BMPreadrow(
static void
BMPreadbits(xel *array,
FILE *fp,
istream *fp,
unsigned long *ppos, /* number of bytes read from fp */
unsigned long offBits,
unsigned long cx,
@ -424,7 +424,7 @@ BMPreadbits(xel *array,
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeBMP::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
if (!read_magic_number(_file, magic_number, 2)) {

View File

@ -53,25 +53,25 @@
static char er_write[] = "stdout: write error";
/* prototypes */
static void PutByte (FILE *fp, char v);
static void PutShort (FILE *fp, short v);
static void PutLong (FILE *fp, long v);
static int BMPwritefileheader (FILE *fp, int classv, unsigned long bitcount,
static void PutByte (ostream *fp, char v);
static void PutShort (ostream *fp, short v);
static void PutLong (ostream *fp, long v);
static int BMPwritefileheader (ostream *fp, int classv, unsigned long bitcount,
unsigned long x, unsigned long y);
static int BMPwriteinfoheader (FILE *fp, int classv, unsigned long bitcount,
static int BMPwriteinfoheader (ostream *fp, int classv, unsigned long bitcount,
unsigned long x, unsigned long y);
static int BMPwritergb (FILE *fp, int classv, pixval R, pixval G, pixval B);
static int BMPwritergbtable (FILE *fp, int classv, int bpp, int colors,
static int BMPwritergb (ostream *fp, int classv, pixval R, pixval G, pixval B);
static int BMPwritergbtable (ostream *fp, int classv, int bpp, int colors,
pixval *R, pixval *G, pixval *B);
static int colorstobpp (int colors);
static void BMPEncode (FILE *fp, int classv, int x, int y, pixel **pixels,
static void BMPEncode (ostream *fp, int classv, int x, int y, pixel **pixels,
int colors, colorhash_table cht, pixval *R, pixval *G, pixval *B);
static void
PutByte(
FILE *fp,
ostream *fp,
char v)
{
if (putc(v, fp) == EOF)
if (!fp->put(v))
{
pm_error(er_write);
}
@ -79,7 +79,7 @@ PutByte(
static void
PutShort(
FILE *fp,
ostream *fp,
short v)
{
if (pm_writelittleshort(fp, v) == -1)
@ -90,7 +90,7 @@ PutShort(
static void
PutLong(
FILE *fp,
ostream *fp,
long v)
{
if (pm_writelittlelong(fp, v) == -1)
@ -108,7 +108,7 @@ PutLong(
*/
static int
BMPwritefileheader(
FILE *fp,
ostream *fp,
int classv,
unsigned long bitcount,
unsigned long x,
@ -137,7 +137,7 @@ BMPwritefileheader(
*/
static int
BMPwriteinfoheader(
FILE *fp,
ostream *fp,
int classv,
unsigned long bitcount,
unsigned long x,
@ -201,7 +201,7 @@ BMPwriteinfoheader(
*/
static int
BMPwritergb(
FILE *fp,
ostream *fp,
int classv,
pixval R,
pixval G,
@ -231,7 +231,7 @@ BMPwritergb(
*/
static int
BMPwritergbtable(
FILE *fp,
ostream *fp,
int classv,
int bpp,
int colors,
@ -263,7 +263,7 @@ BMPwritergbtable(
*/
static int
BMPwriterow(
FILE *fp,
ostream *fp,
pixel *row,
unsigned long cx,
unsigned short bpp,
@ -323,7 +323,7 @@ BMPwriterow(
*/
static int
BMPwritebits(
FILE *fp,
ostream *fp,
unsigned long cx,
unsigned long cy,
unsigned short cBitCount,
@ -397,7 +397,7 @@ colorstobpp(int colors)
*/
static void
BMPEncode(
FILE *fp,
ostream *fp,
int classv,
int x,
int y,
@ -472,7 +472,7 @@ BMPEncode(
*/
static void
BMPEncode24(
FILE *fp,
ostream *fp,
int classv,
int x,
int y,
@ -507,7 +507,7 @@ BMPEncode24(
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeBMP::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
Writer(PNMFileType *type, ostream *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
}

View File

@ -19,18 +19,18 @@
#include "pnmFileTypeIMG.h"
#include "config_pnmimagetypes.h"
#include <pnmFileTypeRegistry.h>
#include <bamReader.h>
#include "pnmFileTypeRegistry.h"
#include "bamReader.h"
// Since raw image files don't have a magic number, we'll make a little
// sanity check on the size of the image. If either the width or height is
// larger than this, it must be bogus.
#define INSANE_SIZE 20000
static const char * const extensionsIMG[] = {
static const char * const extensions_img[] = {
"img"
};
static const int num_extensions_IMG = sizeof(extensionsIMG) / sizeof(const char *);
static const int num_extensions_img = sizeof(extensions_img) / sizeof(const char *);
TypeHandle PNMFileTypeIMG::_type_handle;
@ -61,7 +61,7 @@ get_name() const {
////////////////////////////////////////////////////////////////////
int PNMFileTypeIMG::
get_num_extensions() const {
return num_extensions_IMG;
return num_extensions_img;
}
////////////////////////////////////////////////////////////////////
@ -73,8 +73,8 @@ get_num_extensions() const {
////////////////////////////////////////////////////////////////////
string PNMFileTypeIMG::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_IMG, string());
return extensionsIMG[n];
nassertr(n >= 0 && n < num_extensions_img, string());
return extensions_img[n];
}
////////////////////////////////////////////////////////////////////
@ -97,7 +97,7 @@ get_suggested_extension() const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeIMG::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
make_reader(istream *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
@ -110,44 +110,44 @@ make_reader(FILE *file, bool owns_file, const string &magic_number) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeIMG::
make_writer(FILE *file, bool owns_file) {
make_writer(ostream *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}
inline unsigned long
read_ulong(FILE *file) {
read_ulong(istream *file) {
unsigned long x;
return pm_readbiglong(file, (long *)&x)==0 ? x : 0;
}
inline unsigned short
read_ushort_IMG(FILE *file) {
read_ushort_IMG(istream *file) {
unsigned short x;
return pm_readbigshort(file, (short *)&x)==0 ? x : 0;
}
inline unsigned char
read_uchar_IMG(FILE *file) {
read_uchar_IMG(istream *file) {
int x;
x = getc(file);
x = file->get();
return (x!=EOF) ? (unsigned char)x : 0;
}
inline void
write_ulong(FILE *file, unsigned long x) {
write_ulong(ostream *file, unsigned long x) {
pm_writebiglong(file, (long)x);
}
inline void
write_ushort_IMG(FILE *file, unsigned long x) {
write_ushort_IMG(ostream *file, unsigned long x) {
pm_writebigshort(file, (short)(long)x);
}
inline void
write_uchar_IMG(FILE *file, unsigned char x) {
putc(x, file);
write_uchar_IMG(ostream *file, unsigned char x) {
file->put(x);
}
////////////////////////////////////////////////////////////////////
@ -156,7 +156,7 @@ write_uchar_IMG(FILE *file, unsigned char x) {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeIMG::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
if (img_header_type == IHT_long) {
@ -274,7 +274,7 @@ read_row(xel *row_data, xelval *) {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeIMG::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
Writer(PNMFileType *type, ostream *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
}

View File

@ -19,11 +19,11 @@
#ifndef PNMFILETYPEIMG_H
#define PNMFILETYPEIMG_H
#include <pandabase.h>
#include "pandabase.h"
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
#include "pnmFileType.h"
#include "pnmReader.h"
#include "pnmWriter.h"
////////////////////////////////////////////////////////////////////
// Class : PNMFileTypeIMG
@ -39,14 +39,14 @@ public:
virtual string get_extension(int n) const;
virtual string get_suggested_extension() const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
virtual bool supports_read_row() const;
virtual bool read_row(xel *array, xelval *alpha);
@ -54,7 +54,7 @@ public:
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
Writer(PNMFileType *type, ostream *file, bool owns_file);
virtual bool supports_write_row() const;
virtual bool write_header();

View File

@ -19,13 +19,13 @@
#include "pnmFileTypeJPG.h"
#include "config_pnmimagetypes.h"
#include <pnmFileTypeRegistry.h>
#include <bamReader.h>
#include "pnmFileTypeRegistry.h"
#include "bamReader.h"
static const char * const extensions_JPG[] = {
static const char *const extensions_jpg[] = {
"jpg", "jpeg"
};
static const int num_extensions_JPG = sizeof(extensions_JPG) / sizeof(const char *);
static const int num_extensions_jpg = sizeof(extensions_jpg) / sizeof(const char *);
TypeHandle PNMFileTypeJPG::_type_handle;
@ -52,11 +52,11 @@ get_name() const {
// Function: PNMFileTypeJPG::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_JPG associated with this particular file type.
// extensions associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeJPG::
get_num_extensions() const {
return num_extensions_JPG;
return num_extensions_jpg;
}
////////////////////////////////////////////////////////////////////
@ -68,8 +68,8 @@ get_num_extensions() const {
////////////////////////////////////////////////////////////////////
string PNMFileTypeJPG::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_JPG, string());
return extensions_JPG[n];
nassertr(n >= 0 && n < num_extensions_jpg, string());
return extensions_jpg[n];
}
////////////////////////////////////////////////////////////////////
@ -117,7 +117,7 @@ matches_magic_number(const string &magic_number) const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeJPG::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
make_reader(istream *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
@ -130,7 +130,7 @@ make_reader(FILE *file, bool owns_file, const string &magic_number) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeJPG::
make_writer(FILE *file, bool owns_file) {
make_writer(ostream *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}

View File

@ -19,17 +19,18 @@
#ifndef PNMFILETYPEJPG_H
#define PNMFILETYPEJPG_H
#include <pandabase.h>
#include "pandabase.h"
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
#include "pnmFileType.h"
#include "pnmReader.h"
#include "pnmWriter.h"
#if defined(_WIN32)
#include <windows.h> // we need to include this before jpeglib.
#endif
extern "C" {
#include <stdio.h> // jpeglib requires this to be included first.
#include <jpeglib.h>
#include <setjmp.h>
}
@ -51,14 +52,14 @@ public:
virtual bool has_magic_number() const;
virtual bool matches_magic_number(const string &magic_number) const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
~Reader(void);
virtual int read_data(xel *array, xelval *alpha);
@ -86,7 +87,7 @@ public:
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
Writer(PNMFileType *type, ostream *file, bool owns_file);
virtual int write_data(xel *array, xelval *alpha);
};

View File

@ -19,13 +19,13 @@
#include "pnmFileTypeJPG2000.h"
#include "config_pnmimagetypes.h"
#include <pnmFileTypeRegistry.h>
#include <bamReader.h>
#include "pnmFileTypeRegistry.h"
#include "bamReader.h"
static const char * const extensions_JPG2000[] = {
static const char * const extensions_jpg2000[] = {
"JP2","JPC"
};
static const int num_extensions_JPG2000 = sizeof(extensions_JPG2000) / sizeof(const char *);
static const int num_extensions_jpg2000 = sizeof(extensions_jpg2000) / sizeof(const char *);
TypeHandle PNMFileTypeJPG2000::_type_handle;
@ -45,18 +45,18 @@ PNMFileTypeJPG2000() {
////////////////////////////////////////////////////////////////////
string PNMFileTypeJPG2000::
get_name() const {
return "JPEG_2000";
return "JPEG 2000";
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeJPG2000::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_JPG2000 associated with this particular file type.
// extensions_jpg2000 associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeJPG2000::
get_num_extensions() const {
return num_extensions_JPG2000;
return num_extensions_jpg2000;
}
////////////////////////////////////////////////////////////////////
@ -68,8 +68,8 @@ get_num_extensions() const {
////////////////////////////////////////////////////////////////////
string PNMFileTypeJPG2000::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_JPG2000, string());
return extensions_JPG2000[n];
nassertr(n >= 0 && n < num_extensions_jpg2000, string());
return extensions_jpg2000[n];
}
////////////////////////////////////////////////////////////////////
@ -117,7 +117,7 @@ matches_magic_number(const string &magic_number) const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeJPG2000::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
make_reader(istream *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
@ -130,7 +130,7 @@ make_reader(FILE *file, bool owns_file, const string &magic_number) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeJPG2000::
make_writer(FILE *file, bool owns_file) {
make_writer(ostream *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}

View File

@ -19,11 +19,11 @@
#ifndef PNMFILETYPEJPG2000_H
#define PNMFILETYPEJPG2000_H
#include <pandabase.h>
#include "pandabase.h"
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
#include "pnmFileType.h"
#include "pnmReader.h"
#include "pnmWriter.h"
#if defined(_WIN32)
#include <windows.h> // we need to include this before jpeglib.
@ -31,16 +31,9 @@
#include <jasper/jasper.h>
/*
extern "C" {
#include <jpeglib.h>
#include <setjmp.h>
}
*/
////////////////////////////////////////////////////////////////////
// Class : PNMFileTypeJPG2000
// Description : For reading and writing Jpeg files.
// Description : For reading and writing Jpeg2000 files.
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA PNMFileTypeJPG2000 : public PNMFileType {
public:
@ -55,14 +48,14 @@ public:
virtual bool has_magic_number() const;
virtual bool matches_magic_number(const string &magic_number) const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
~Reader(void);
virtual int read_data(xel *array, xelval *alpha);
@ -97,7 +90,7 @@ public:
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
Writer(PNMFileType *type, ostream *file, bool owns_file);
virtual int write_data(xel *array, xelval *alpha);
};

View File

@ -18,7 +18,6 @@
#include "pnmFileTypeJPG2000.h"
#include "config_pnmimagetypes.h"
#include <typedef.h>
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeJPG2000::Reader::Constructor
@ -26,11 +25,11 @@
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeJPG2000::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
// Put the magic number bytes back into the file
fseek(file, 0, SEEK_SET);
file->seekg(0);
/* Step 1: allocate and initialize JPEG decompression object */
@ -41,7 +40,8 @@ Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
jpeg_create_decompress(&_cinfo);
/* Step 2: specify data source (eg, a file) */
// This is broken, and won't compile. We need to drop in an
// iostream replacement, like we did for JPG.
jpeg_stdio_src(&_cinfo, file);
/* Step 3: read file parameters with jpeg_read_header() */

View File

@ -19,8 +19,8 @@
#include "pnmFileTypeJPG2000.h"
#include "config_pnmimagetypes.h"
#include <pnmImage.h>
#include <pnmWriter.h>
#include "pnmImage.h"
#include "pnmWriter.h"
extern "C" {
#include <jpeglib.h>
@ -32,7 +32,7 @@ extern "C" {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeJPG2000::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
Writer(PNMFileType *type, ostream *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
}
@ -106,6 +106,8 @@ write_data(xel *array, xelval *) {
* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
* requires it in order to write binary files.
*/
// This is broken, and won't compile. We need to drop in an
// iostream replacement, like we did for JPG.
jpeg_stdio_dest(&cinfo, _file);
/* Step 3: set parameters for compression */

View File

@ -18,7 +18,234 @@
#include "pnmFileTypeJPG.h"
#include "config_pnmimagetypes.h"
#include <typedef.h>
//
// The following bit of code, for setting up jpeg_istream_src(), was
// lifted from jpeglib, and modified to work with istream istead of
// stdio.
//
/*
* jdatasrc.c
*
* Copyright (C) 1994-1996, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains decompression data source routines for the case of
* reading JPEG data from a file (or any stdio stream). While these routines
* are sufficient for most applications, some will want to use a different
* source manager.
* IMPORTANT: we assume that fread() will correctly transcribe an array of
* JOCTETs from 8-bit-wide elements on external storage. If char is wider
* than 8 bits on your machine, you may need to do some tweaking.
*/
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
extern "C" {
#include <jpeglib.h>
#include <jerror.h>
}
/* Expanded data source object for stdio input */
typedef struct {
struct jpeg_source_mgr pub; /* public fields */
istream * infile; /* source stream */
JOCTET * buffer; /* start of buffer */
boolean start_of_file; /* have we gotten any data yet? */
} my_source_mgr;
typedef my_source_mgr * my_src_ptr;
#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
/*
* Initialize source --- called by jpeg_read_header
* before any data is actually read.
*/
METHODDEF(void)
init_source (j_decompress_ptr cinfo)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
/* We reset the empty-input-file flag for each image,
* but we don't clear the input buffer.
* This is correct behavior for reading a series of images from one source.
*/
src->start_of_file = TRUE;
}
/*
* Fill the input buffer --- called whenever buffer is emptied.
*
* In typical applications, this should read fresh data into the buffer
* (ignoring the current state of next_input_byte & bytes_in_buffer),
* reset the pointer & count to the start of the buffer, and return TRUE
* indicating that the buffer has been reloaded. It is not necessary to
* fill the buffer entirely, only to obtain at least one more byte.
*
* There is no such thing as an EOF return. If the end of the file has been
* reached, the routine has a choice of ERREXIT() or inserting fake data into
* the buffer. In most cases, generating a warning message and inserting a
* fake EOI marker is the best course of action --- this will allow the
* decompressor to output however much of the image is there. However,
* the resulting error message is misleading if the real problem is an empty
* input file, so we handle that case specially.
*
* In applications that need to be able to suspend compression due to input
* not being available yet, a FALSE return indicates that no more data can be
* obtained right now, but more may be forthcoming later. In this situation,
* the decompressor will return to its caller (with an indication of the
* number of scanlines it has read, if any). The application should resume
* decompression after it has loaded more data into the input buffer. Note
* that there are substantial restrictions on the use of suspension --- see
* the documentation.
*
* When suspending, the decompressor will back up to a convenient restart point
* (typically the start of the current MCU). next_input_byte & bytes_in_buffer
* indicate where the restart point will be if the current call returns FALSE.
* Data beyond this point must be rescanned after resumption, so move it to
* the front of the buffer rather than discarding it.
*/
METHODDEF(boolean)
fill_input_buffer (j_decompress_ptr cinfo)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
size_t nbytes;
src->infile->read(src->buffer, INPUT_BUF_SIZE);
nbytes = src->infile->gcount();
if (nbytes <= 0) {
if (src->start_of_file) /* Treat empty input file as fatal error */
ERREXIT(cinfo, JERR_INPUT_EMPTY);
WARNMS(cinfo, JWRN_JPEG_EOF);
/* Insert a fake EOI marker */
src->buffer[0] = (JOCTET) 0xFF;
src->buffer[1] = (JOCTET) JPEG_EOI;
nbytes = 2;
}
src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer = nbytes;
src->start_of_file = FALSE;
return TRUE;
}
/*
* Skip data --- used to skip over a potentially large amount of
* uninteresting data (such as an APPn marker).
*
* Writers of suspendable-input applications must note that skip_input_data
* is not granted the right to give a suspension return. If the skip extends
* beyond the data currently in the buffer, the buffer can be marked empty so
* that the next read will cause a fill_input_buffer call that can suspend.
* Arranging for additional bytes to be discarded before reloading the input
* buffer is the application writer's problem.
*/
METHODDEF(void)
skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
/* Just a dumb implementation for now. Could use fseek() except
* it doesn't work on pipes. Not clear that being smart is worth
* any trouble anyway --- large skips are infrequent.
*/
if (num_bytes > 0) {
while (num_bytes > (long) src->pub.bytes_in_buffer) {
num_bytes -= (long) src->pub.bytes_in_buffer;
(void) fill_input_buffer(cinfo);
/* note we assume that fill_input_buffer will never return FALSE,
* so suspension need not be handled.
*/
}
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
}
}
/*
* An additional method that can be provided by data source modules is the
* resync_to_restart method for error recovery in the presence of RST markers.
* For the moment, this source module just uses the default resync method
* provided by the JPEG library. That method assumes that no backtracking
* is possible.
*/
/*
* Terminate source --- called by jpeg_finish_decompress
* after all data has been read. Often a no-op.
*
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
* application must deal with any cleanup that should happen even
* for error exit.
*/
METHODDEF(void)
term_source (j_decompress_ptr cinfo)
{
/* no work necessary here */
}
/*
* Prepare for input from a stdio stream.
* The caller must have already opened the stream, and is responsible
* for closing it after finishing decompression.
*/
GLOBAL(void)
jpeg_istream_src (j_decompress_ptr cinfo, istream * infile)
{
my_src_ptr src;
/* The source object and input buffer are made permanent so that a series
* of JPEG images can be read from the same file by calling jpeg_stdio_src
* only before the first one. (If we discarded the buffer at the end of
* one image, we'd likely lose the start of the next one.)
* This makes it unsafe to use this manager and a different source
* manager serially with the same JPEG object. Caveat programmer.
*/
if (cinfo->src == NULL) { /* first time for this JPEG object? */
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(my_source_mgr));
src = (my_src_ptr) cinfo->src;
src->buffer = (JOCTET *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
INPUT_BUF_SIZE * sizeof(JOCTET));
}
src = (my_src_ptr) cinfo->src;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
src->pub.term_source = term_source;
src->infile = infile;
src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
src->pub.next_input_byte = NULL; /* until buffer loaded */
}
//
// The rest of the code in this file is new to Panda.
//
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeJPG::Reader::Constructor
@ -26,11 +253,11 @@
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeJPG::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
// Put the magic number bytes back into the file
fseek(file, 0, SEEK_SET);
file->seekg(0);
/* Step 1: allocate and initialize JPEG decompression object */
@ -41,8 +268,7 @@ Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
jpeg_create_decompress(&_cinfo);
/* Step 2: specify data source (eg, a file) */
jpeg_stdio_src(&_cinfo, file);
jpeg_istream_src(&_cinfo, file);
/* Step 3: read file parameters with jpeg_read_header() */

View File

@ -19,20 +19,181 @@
#include "pnmFileTypeJPG.h"
#include "config_pnmimagetypes.h"
#include <pnmImage.h>
#include <pnmWriter.h>
#include "pnmImage.h"
#include "pnmWriter.h"
//
// The following bit of code, for setting up jpeg_istream_src(), was
// lifted from jpeglib, and modified to work with istream istead of
// stdio.
//
/*
* jdatadst.c
*
* Copyright (C) 1994-1996, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains compression data destination routines for the case of
* emitting JPEG data to a file (or any stdio stream). While these routines
* are sufficient for most applications, some will want to use a different
* destination manager.
* IMPORTANT: we assume that fwrite() will correctly transcribe an array of
* JOCTETs into 8-bit-wide elements on external storage. If char is wider
* than 8 bits on your machine, you may need to do some tweaking.
*/
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
extern "C" {
#include <jpeglib.h>
#include <jerror.h>
}
/* Expanded data destination object for stdio output */
typedef struct {
struct jpeg_destination_mgr pub; /* public fields */
ostream * outfile; /* target stream */
JOCTET * buffer; /* start of buffer */
} my_destination_mgr;
typedef my_destination_mgr * my_dest_ptr;
#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
/*
* Initialize destination --- called by jpeg_start_compress
* before any data is actually written.
*/
METHODDEF(void)
init_destination (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
/* Allocate the output buffer --- it will be released when done with image */
dest->buffer = (JOCTET *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
OUTPUT_BUF_SIZE * sizeof(JOCTET));
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
/*
* Empty the output buffer --- called whenever buffer fills up.
*
* In typical applications, this should write the entire output buffer
* (ignoring the current state of next_output_byte & free_in_buffer),
* reset the pointer & count to the start of the buffer, and return TRUE
* indicating that the buffer has been dumped.
*
* In applications that need to be able to suspend compression due to output
* overrun, a FALSE return indicates that the buffer cannot be emptied now.
* In this situation, the compressor will return to its caller (possibly with
* an indication that it has not accepted all the supplied scanlines). The
* application should resume compression after it has made more room in the
* output buffer. Note that there are substantial restrictions on the use of
* suspension --- see the documentation.
*
* When suspending, the compressor will back up to a convenient restart point
* (typically the start of the current MCU). next_output_byte & free_in_buffer
* indicate where the restart point will be if the current call returns FALSE.
* Data beyond this point will be regenerated after resumption, so do not
* write it out when emptying the buffer externally.
*/
METHODDEF(boolean)
empty_output_buffer (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
if (!dest->outfile->write(dest->buffer, OUTPUT_BUF_SIZE))
ERREXIT(cinfo, JERR_FILE_WRITE);
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
/*
* Terminate destination --- called by jpeg_finish_compress
* after all data has been written. Usually needs to flush buffer.
*
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
* application must deal with any cleanup that should happen even
* for error exit.
*/
METHODDEF(void)
term_destination (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
/* Write any data remaining in the buffer */
if (datacount > 0) {
if (!dest->outfile->write(dest->buffer, datacount))
ERREXIT(cinfo, JERR_FILE_WRITE);
}
dest->outfile->flush();
/* Make sure we wrote the output file OK */
if (dest->outfile->fail())
ERREXIT(cinfo, JERR_FILE_WRITE);
}
/*
* Prepare for output to a stdio stream.
* The caller must have already opened the stream, and is responsible
* for closing it after finishing compression.
*/
GLOBAL(void)
jpeg_ostream_dest (j_compress_ptr cinfo, ostream * outfile)
{
my_dest_ptr dest;
/* The destination object is made permanent so that multiple JPEG images
* can be written to the same file without re-executing jpeg_stdio_dest.
* This makes it dangerous to use this manager and a different destination
* manager serially with the same JPEG object, because their private object
* sizes may be different. Caveat programmer.
*/
if (cinfo->dest == NULL) { /* first time for this JPEG object? */
cinfo->dest = (struct jpeg_destination_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(my_destination_mgr));
}
dest = (my_dest_ptr) cinfo->dest;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
dest->pub.term_destination = term_destination;
dest->outfile = outfile;
}
//
// The rest of the code in this file is new to Panda.
//
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeJPG::Writer::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeJPG::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
Writer(PNMFileType *type, ostream *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
}
@ -100,13 +261,7 @@ write_data(xel *array, xelval *) {
/* Step 2: specify data destination (eg, a file) */
/* Note: steps 2 and 3 can be done in either order. */
/* Here we use the library-supplied code to send compressed data to a
* stdio stream. You can also write your own code to do something else.
* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
* requires it in order to write binary files.
*/
jpeg_stdio_dest(&cinfo, _file);
jpeg_ostream_dest(&cinfo, _file);
/* Step 3: set parameters for compression */

View File

@ -1,382 +0,0 @@
// Filename: pnmFileTypeRadiance.cxx
// Created by: drose (17Jun00)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license. You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://www.panda3d.org/license.txt .
//
// To contact the maintainers of this program write to
// panda3d@yahoogroups.com .
//
////////////////////////////////////////////////////////////////////
#include "pnmFileTypeRadiance.h"
#include "config_pnmimagetypes.h"
#include <pnmFileTypeRegistry.h>
#include <bamReader.h>
extern "C" {
#include "color.h"
#include "resolu.h"
void setcolrgam(double);
int checkheader(FILE *, char *, FILE *);
int fgetresolu(int *, int *, FILE *);
int freadcolrs(COLR *, int, FILE *);
int fwritecolrs(COLR *, unsigned, FILE *);
void fputformat(char *, FILE *);
void shiftcolrs(COLR *, int, int);
void colrs_gambs(COLR *, int);
void newheader(char *, FILE *);
void printargs(int, char **, FILE *);
void gambs_colrs(COLR *, int);
}
static const char * const extensions_RAD[] = {
"pic", "rad"
};
static const int num_extensions_RAD = sizeof(extensions_RAD) / sizeof(const char *);
TypeHandle PNMFileTypeRadiance::_type_handle;
static const int COLR_MAX = 255;
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeRadiance::
PNMFileTypeRadiance() {
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::get_name
// Access: Public, Virtual
// Description: Returns a few words describing the file type.
////////////////////////////////////////////////////////////////////
string PNMFileTypeRadiance::
get_name() const {
return "Radiance";
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_RAD associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeRadiance::
get_num_extensions() const {
return num_extensions_RAD;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::get_extension
// Access: Public, Virtual
// Description: Returns the nth possible filename extension
// associated with this particular file type, without a
// leading dot.
////////////////////////////////////////////////////////////////////
string PNMFileTypeRadiance::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_RAD, string());
return extensions_RAD[n];
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::get_suggested_extension
// Access: Public, Virtual
// Description: Returns a suitable filename extension (without a
// leading dot) to suggest for files of this type, or
// empty string if no suggestions are available.
////////////////////////////////////////////////////////////////////
string PNMFileTypeRadiance::
get_suggested_extension() const {
return "rad";
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::has_magic_number
// Access: Public, Virtual
// Description: Returns true if this particular file type uses a
// magic number to identify it, false otherwise.
////////////////////////////////////////////////////////////////////
bool PNMFileTypeRadiance::
has_magic_number() const {
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::matches_magic_number
// Access: Public, Virtual
// Description: Returns true if the indicated "magic number" byte
// stream (the initial few bytes read from the file)
// matches this particular file type, false otherwise.
////////////////////////////////////////////////////////////////////
bool PNMFileTypeRadiance::
matches_magic_number(const string &magic_number) const {
nassertr(magic_number.size() >= 2, false);
return (magic_number.substr(0, 2) == "#?");
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::make_reader
// Access: Public, Virtual
// Description: Allocates and returns a new PNMReader suitable for
// reading from this file type, if possible. If reading
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeRadiance::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::make_writer
// Access: Public, Virtual
// Description: Allocates and returns a new PNMWriter suitable for
// reading from this file type, if possible. If writing
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeRadiance::
make_writer(FILE *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::Reader::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeRadiance::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
setcolrgam(radiance_gamma_correction);
// Hope we can ungetc() more than one character.
for (string::reverse_iterator mi = magic_number.rbegin();
mi != magic_number.rend();
mi++) {
ungetc(*mi, _file);
}
/* get our header */
if (checkheader(file, COLRFMT, NULL) < 0 ||
fgetresolu(&_x_size, &_y_size, _file) < 0) {
_is_valid = false;
pnmimage_radiance_cat.debug()
<< "File is not a valid Radiance image.\n";
return;
}
_num_channels = 3;
_maxval = COLR_MAX;
if (pnmimage_radiance_cat.is_debug()) {
pnmimage_radiance_cat.debug()
<< "Reading Radiance image: " << _x_size << " by " << _y_size
<< " pixels.\n"
<< "gamma correction is " << radiance_gamma_correction
<< ", brightness adjustment is " << radiance_brightness_adjustment
<< "\n";
}
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::Reader::supports_read_row
// Access: Public, Virtual
// Description: Returns true if this particular PNMReader supports a
// streaming interface to reading the data: that is, it
// is capable of returning the data one row at a time,
// via repeated calls to read_row(). Returns false if
// the only way to read from this file is all at once,
// via read_data().
////////////////////////////////////////////////////////////////////
bool PNMFileTypeRadiance::Reader::
supports_read_row() const {
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::Reader::read_row
// Access: Public, Virtual
// Description: If supports_read_row(), above, returns true, this
// function may be called repeatedly to read the image,
// one horizontal row at a time, beginning from the top.
// Returns true if the row is successfully read, false
// if there is an error or end of file.
////////////////////////////////////////////////////////////////////
bool PNMFileTypeRadiance::Reader::
read_row(xel *row_data, xelval *) {
if (!is_valid()) {
return false;
}
COLR *scanin;
int x;
scanin = (COLR *)alloca(_x_size * sizeof(COLR));
if (freadcolrs(scanin, _x_size, _file) < 0) {
return false;
}
if (radiance_brightness_adjustment) {
shiftcolrs(scanin, _x_size, radiance_brightness_adjustment);
}
colrs_gambs(scanin, _x_size); /* gamma correction */
for (x = 0; x < _x_size; x++) {
PPM_ASSIGN(row_data[x], scanin[x][RED], scanin[x][GRN], scanin[x][BLU]);
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::Writer::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeRadiance::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::Writer::supports_write_row
// Access: Public, Virtual
// Description: Returns true if this particular PNMWriter supports a
// streaming interface to writing the data: that is, it
// is capable of writing the image one row at a time,
// via repeated calls to write_row(). Returns false if
// the only way to write from this file is all at once,
// via write_data().
////////////////////////////////////////////////////////////////////
bool PNMFileTypeRadiance::Writer::
supports_write_row() const {
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::Writer::write_header
// Access: Public, Virtual
// Description: If supports_write_row(), above, returns true, this
// function may be called to write out the image header
// in preparation to writing out the image data one row
// at a time. Returns true if the header is
// successfully written, false if there is an error.
//
// It is the user's responsibility to fill in the header
// data via calls to set_x_size(), set_num_channels(),
// etc., or copy_header_from(), before calling
// write_header().
////////////////////////////////////////////////////////////////////
bool PNMFileTypeRadiance::Writer::
write_header() {
setcolrgam(radiance_gamma_correction);
/* put our header */
newheader("RADIANCE", _file);
fputs("Generated via pnmimage\n", _file);
fputformat(COLRFMT, _file);
putc('\n', _file);
fprtresolu(_x_size, _y_size, _file);
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::Writer::write_row
// Access: Public, Virtual
// Description: If supports_write_row(), above, returns true, this
// function may be called repeatedly to write the image,
// one horizontal row at a time, beginning from the top.
// Returns true if the row is successfully written,
// false if there is an error.
//
// You must first call write_header() before writing the
// individual rows. It is also important to delete the
// PNMWriter class after successfully writing the last
// row. Failing to do this may result in some data not
// getting flushed!
////////////////////////////////////////////////////////////////////
bool PNMFileTypeRadiance::Writer::
write_row(xel *row_data, xelval *) {
/* convert file */
bool grayscale = is_grayscale();
COLR *scanout;
int x;
/* allocate scanline */
scanout = (COLR *)alloca(_x_size * sizeof(COLR));
/* convert image */
for (x = 0; x < _x_size; x++) {
if (grayscale) {
scanout[x][RED] =
scanout[x][GRN] =
scanout[x][BLU] = (BYTE)((int)COLR_MAX * PPM_GETB(row_data[x]) / _maxval);
} else {
scanout[x][RED] = (BYTE)((int)COLR_MAX * PPM_GETR(row_data[x]) / _maxval);
scanout[x][GRN] = (BYTE)((int)COLR_MAX * PPM_GETG(row_data[x]) / _maxval);
scanout[x][BLU] = (BYTE)((int)COLR_MAX * PPM_GETB(row_data[x]) / _maxval);
}
}
/* undo gamma */
gambs_colrs(scanout, _x_size);
if (radiance_brightness_adjustment)
shiftcolrs(scanout, _x_size, radiance_brightness_adjustment);
if (fwritecolrs(scanout, _x_size, _file) < 0)
return false;
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::register_with_read_factory
// Access: Public, Static
// Description: Registers the current object as something that can be
// read from a Bam file.
////////////////////////////////////////////////////////////////////
void PNMFileTypeRadiance::
register_with_read_factory() {
BamReader::get_factory()->
register_factory(get_class_type(), make_PNMFileTypeRadiance);
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeRadiance::make_PNMFileTypeRadiance
// Access: Protected, Static
// Description: This method is called by the BamReader when an object
// of this type is encountered in a Bam file; it should
// allocate and return a new object with all the data
// read.
//
// In the case of the PNMFileType objects, since these
// objects are all shared, we just pull the object from
// the registry.
////////////////////////////////////////////////////////////////////
TypedWritable *PNMFileTypeRadiance::
make_PNMFileTypeRadiance(const FactoryParams &params) {
return PNMFileTypeRegistry::get_ptr()->get_type_by_handle(get_class_type());
}

View File

@ -1,95 +0,0 @@
// Filename: pnmFileTypeRadiance.h
// Created by: drose (17Jun00)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license. You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://www.panda3d.org/license.txt .
//
// To contact the maintainers of this program write to
// panda3d@yahoogroups.com .
//
////////////////////////////////////////////////////////////////////
#ifndef PNMFILETYPERADIANCE_H
#define PNMFILETYPERADIANCE_H
#include <pandabase.h>
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
////////////////////////////////////////////////////////////////////
// Class : PNMFileTypeRadiance
// Description : For reading and Radiance native image files.
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA PNMFileTypeRadiance : public PNMFileType {
public:
PNMFileTypeRadiance();
virtual string get_name() const;
virtual int get_num_extensions() const;
virtual string get_extension(int n) const;
virtual string get_suggested_extension() const;
virtual bool has_magic_number() const;
virtual bool matches_magic_number(const string &magic_number) const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
virtual bool supports_read_row() const;
virtual bool read_row(xel *array, xelval *alpha);
};
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
virtual bool supports_write_row() const;
virtual bool write_header();
virtual bool write_row(xel *array, xelval *alpha);
};
// The TypedWritable interface follows.
public:
static void register_with_read_factory();
protected:
static TypedWritable *make_PNMFileTypeRadiance(const FactoryParams &params);
public:
static TypeHandle get_class_type() {
return _type_handle;
}
static void init_type() {
PNMFileType::init_type();
register_type(_type_handle, "PNMFileTypeRadiance",
PNMFileType::get_class_type());
}
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
private:
static TypeHandle _type_handle;
};
#endif

View File

@ -20,13 +20,13 @@
#include "config_pnmimagetypes.h"
#include "sgi.h"
#include <pnmFileTypeRegistry.h>
#include <bamReader.h>
#include "pnmFileTypeRegistry.h"
#include "bamReader.h"
static const char * const extensions_SGI[] = {
static const char * const extensions_sgi[] = {
"rgb", "rgba", "sgi"
};
static const int num_extensions_SGI = sizeof(extensions_SGI) / sizeof(const char *);
static const int num_extensions_sgi = sizeof(extensions_sgi) / sizeof(const char *);
TypeHandle PNMFileTypeSGI::_type_handle;
@ -53,11 +53,11 @@ get_name() const {
// Function: PNMFileTypeSGI::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_SGI associated with this particular file type.
// extensions associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeSGI::
get_num_extensions() const {
return num_extensions_SGI;
return num_extensions_sgi;
}
////////////////////////////////////////////////////////////////////
@ -69,8 +69,8 @@ get_num_extensions() const {
////////////////////////////////////////////////////////////////////
string PNMFileTypeSGI::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_SGI, string());
return extensions_SGI[n];
nassertr(n >= 0 && n < num_extensions_sgi, string());
return extensions_sgi[n];
}
////////////////////////////////////////////////////////////////////
@ -120,7 +120,7 @@ matches_magic_number(const string &magic_number) const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeSGI::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
make_reader(istream *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
@ -133,7 +133,7 @@ make_reader(FILE *file, bool owns_file, const string &magic_number) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeSGI::
make_writer(FILE *file, bool owns_file) {
make_writer(ostream *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}

View File

@ -19,11 +19,11 @@
#ifndef PNMFILETYPESGI_H
#define PNMFILETYPESGI_H
#include <pandabase.h>
#include "pandabase.h"
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
#include "pnmFileType.h"
#include "pnmReader.h"
#include "pnmWriter.h"
////////////////////////////////////////////////////////////////////
// Class : PNMFileTypeSGI
@ -42,14 +42,14 @@ public:
virtual bool has_magic_number() const;
virtual bool matches_magic_number(const string &magic_number) const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
virtual ~Reader();
virtual bool supports_read_row() const;
@ -69,7 +69,7 @@ public:
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
Writer(PNMFileType *type, ostream *file, bool owns_file);
virtual ~Writer();
virtual bool supports_write_row() const;
@ -94,7 +94,7 @@ public:
void write_rgb_header(const char *imagename);
void write_table();
void write_channels(ScanLine channel[], void (*put)(FILE *, short));
void write_channels(ScanLine channel[], void (*put)(ostream *, short));
void build_scanline(ScanLine output[], xel *row_data, xelval *alpha_data);
ScanElem *compress(ScanElem *temp, ScanLine &output);
int rle_compress(ScanElem *inbuf, int size);

View File

@ -20,10 +20,10 @@
#include "config_pnmimagetypes.h"
#include "sgi.h"
#include <pnmImage.h>
#include <pnmReader.h>
#include "pnmImage.h"
#include "pnmReader.h"
#include <notify.h>
#include "notify.h"
// Much code in this file is borrowed from Netpbm, specifically sgitopnm.c.
@ -52,18 +52,18 @@ typedef short ScanElem;
typedef ScanElem * ScanLine;
/* prototypes */
static unsigned char get_byte ( FILE* f );
static long get_big_long (FILE *f);
static short get_big_short (FILE *f);
static short get_byte_as_short (FILE *f);
static int readerr (FILE *f);
static unsigned char get_byte ( istream* f );
static long get_big_long (istream *f);
static short get_big_short (istream *f);
static short get_byte_as_short (istream *f);
static int readerr (istream *f);
static void * xmalloc (int bytes);
#define MALLOC(n, type) (type *)xmalloc((n) * sizeof(type))
static char * compression_name (char compr);
static void read_bytes (FILE *ifp, int n, char *buf);
static bool read_header(FILE *ifp, Header *head, const string &magic_number);
static TabEntry * read_table (FILE *ifp, int tablen);
static void read_channel (FILE *ifp, int xsize, int ysize,
static void read_bytes (istream *ifp, int n, char *buf);
static bool read_header(istream *ifp, Header *head, const string &magic_number);
static TabEntry * read_table (istream *ifp, int tablen);
static void read_channel (istream *ifp, int xsize, int ysize,
int zsize, int bpc, TabEntry *table,
ScanElem *channel_data, long table_start,
int channel, int row);
@ -86,7 +86,7 @@ static bool eof_err = false;
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeSGI::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
eof_err = false;
@ -119,7 +119,7 @@ Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
_maxval = (xelval)pixmax;
table_start = ftell(file);
table_start = file->tellg();
if( head.storage != STORAGE_VERBATIM )
table = read_table(file, head.ysize * head.zsize);
@ -230,7 +230,7 @@ read_row(xel *row_data, xelval *alpha_data) {
static bool
read_header(FILE *ifp, Header *head, const string &magic_number) {
read_header(istream *ifp, Header *head, const string &magic_number) {
nassertr(magic_number.size() == 4, false);
head->magic =
((unsigned char)magic_number[0] << 8) |
@ -318,7 +318,7 @@ read_header(FILE *ifp, Header *head, const string &magic_number) {
static TabEntry *
read_table(FILE *ifp, int tablen) {
read_table(istream *ifp, int tablen) {
TabEntry *table;
int i;
@ -337,7 +337,7 @@ read_table(FILE *ifp, int tablen) {
static void
read_channel(FILE *ifp,
read_channel(istream *ifp,
int xsize, int ysize, int, int bpc,
TabEntry *table,
ScanElem *channel_data, long table_start,
@ -346,7 +346,7 @@ read_channel(FILE *ifp,
int sgi_index, i;
long offset, length;
short (*func)(FILE *);
short (*func)(istream *);
func = (bpc==1) ? get_byte_as_short : get_big_short;
if ( table ) {
@ -359,7 +359,7 @@ read_channel(FILE *ifp,
length = table[sgi_index].length;
if( bpc == 2 )
length /= 2; /* doc says length is in bytes, we are reading words */
if( fseek(ifp, offset, SEEK_SET) != 0 )
if(!ifp->seekg(offset))
pm_error("seek error for offset %ld", offset);
nassertv(length <= WORSTCOMPR(xsize));
@ -370,7 +370,7 @@ read_channel(FILE *ifp,
}
else {
offset = sgi_index * xsize + table_start;
if( fseek(ifp, offset, SEEK_SET) != 0 )
if(!ifp->seekg(offset))
pm_error("seek error for offset %ld", offset);
for( i = 0; i < xsize; i++ )
channel_data[i] = (*func)(ifp);
@ -420,7 +420,7 @@ rle_decompress(ScanElem *src,
/* basic I/O functions, taken from ilbmtoppm.c */
static short
get_big_short(FILE *ifp) {
get_big_short(istream *ifp) {
short s;
if( pm_readbigshort(ifp, &s) == -1 )
@ -430,7 +430,7 @@ get_big_short(FILE *ifp) {
}
static long
get_big_long(FILE *ifp) {
get_big_long(istream *ifp) {
long l;
if( pm_readbiglong(ifp, &l) == -1 )
@ -440,10 +440,10 @@ get_big_long(FILE *ifp) {
}
static unsigned char
get_byte(FILE *ifp) {
get_byte(istream *ifp) {
int i;
i = getc(ifp);
i = ifp->get();
if( i == EOF )
i = readerr(ifp);
@ -452,13 +452,15 @@ get_byte(FILE *ifp) {
static int
readerr(FILE *f) {
// This will return only if the error is EOF.
if( ferror(f) )
pm_error("read error");
readerr(istream *f) {
if (!eof_err) {
fprintf(stderr, "Warning: premature EOF on file\n");
if (!f->eof()) {
pnmimage_sgi_cat.warning()
<< "Read error on file.\n";
} else {
pnmimage_sgi_cat.warning()
<< "Premature EOF on file.\n";
}
eof_err = true;
}
@ -467,12 +469,13 @@ readerr(FILE *f) {
static void
read_bytes(FILE *ifp,
read_bytes(istream *ifp,
int n,
char *buf) {
int r;
r = fread((void *)buf, 1, n, ifp);
ifp->read(buf, n);
r = ifp->gcount();
if( r != n ) {
readerr(ifp);
memset(buf+r, 0, n-r);
@ -481,7 +484,7 @@ read_bytes(FILE *ifp,
static short
get_byte_as_short(FILE *ifp) {
get_byte_as_short(istream *ifp) {
return (short)get_byte(ifp);
}

View File

@ -20,8 +20,8 @@
#include "config_pnmimagetypes.h"
#include "sgi.h"
#include <pnmImage.h>
#include <pnmWriter.h>
#include "pnmImage.h"
#include "pnmWriter.h"
// Much code in this file originally came from from Netpbm,
// specifically pnmtosgi.c. It has since been fairly heavily
@ -53,26 +53,26 @@
#define MAXVAL_WORD 65535
inline void
put_byte(FILE *out_file, unsigned char b) {
putc(b, out_file);
put_byte(ostream *out_file, unsigned char b) {
out_file->put(b);
}
static void
put_big_short(FILE *out_file, short s) {
put_big_short(ostream *out_file, short s) {
if ( pm_writebigshort( out_file, s ) == -1 )
pm_error( "write error" );
}
static void
put_big_long(FILE *out_file, long l) {
put_big_long(ostream *out_file, long l) {
if ( pm_writebiglong( out_file, l ) == -1 )
pm_error( "write error" );
}
static void
put_short_as_byte(FILE *out_file, short s) {
put_short_as_byte(ostream *out_file, short s) {
put_byte(out_file, (unsigned char)s);
}
@ -83,7 +83,7 @@ put_short_as_byte(FILE *out_file, short s) {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeSGI::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
Writer(PNMFileType *type, ostream *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
}
@ -97,7 +97,7 @@ PNMFileTypeSGI::Writer::
~Writer() {
if (table!=NULL) {
// Rewrite the table with the correct values in it.
fseek(_file, table_start, SEEK_SET);
_file->seekp(table_start);
write_table();
delete[] table;
}
@ -171,7 +171,7 @@ write_header() {
write_rgb_header(sgi_imagename.c_str());
if (table!=NULL) {
table_start = ftell(_file);
table_start = _file->tellp();
// The first time we write the table, it has zeroes. We'll correct
// this later.
@ -257,11 +257,11 @@ write_table() {
void PNMFileTypeSGI::Writer::
write_channels(ScanLine channel[], void (*put)(FILE *, short)) {
write_channels(ScanLine channel[], void (*put)(ostream *, short)) {
int i, col;
for( i = 0; i < _num_channels; i++ ) {
Table(i).start = ftell(_file);
Table(i).start = _file->tellp();
Table(i).length = channel[i].length * bpc;
for( col = 0; col < channel[i].length; col++ ) {

View File

@ -19,8 +19,8 @@
#include "pnmFileTypeSoftImage.h"
#include "config_pnmimagetypes.h"
#include <pnmFileTypeRegistry.h>
#include <bamReader.h>
#include "pnmFileTypeRegistry.h"
#include "bamReader.h"
static const float imageVersionNumber = 3.0;
static const int imageCommentLength = 80;
@ -39,15 +39,15 @@ static const char imageComment[imageCommentLength+1] =
#define SOFTIMAGE_MAGIC1 0x5380
#define SOFTIMAGE_MAGIC2 0xf634
static const char * const extensions_SI[] = {
static const char * const extensions_softimage[] = {
"pic", "soft"
};
static const int num_extensions_SI = sizeof(extensions_SI) / sizeof(const char *);
static const int num_extensions_softimage = sizeof(extensions_softimage) / sizeof(const char *);
TypeHandle PNMFileTypeSoftImage::_type_handle;
inline float
read_float(FILE *file) {
read_float(istream *file) {
long l;
if (pm_readbiglong(file, &l)==0) {
@ -58,42 +58,42 @@ read_float(FILE *file) {
}
inline unsigned short
read_ushort_SI(FILE *file) {
read_ushort_SI(istream *file) {
unsigned short x;
return pm_readbigshort(file, (short *)&x)==0 ? x : 0;
}
inline unsigned char
read_uchar_SI(FILE *file) {
read_uchar_SI(istream *file) {
int x;
x = getc(file);
x = file->get();
return (x!=EOF) ? (unsigned char)x : 0;
}
inline void
write_ushort_SI(FILE *file, unsigned short x) {
write_ushort_SI(ostream *file, unsigned short x) {
pm_writebigshort(file, (short)x);
}
inline void
write_uchar_SI(FILE *file, unsigned char x) {
putc(x, file);
write_uchar_SI(ostream *file, unsigned char x) {
file->put(x);
}
inline void
write_float(FILE *file, float x) {
write_float(ostream *file, float x) {
pm_writebiglong(file, *(long *)&x);
}
static int
read_channel_pkt(FILE *file,
read_channel_pkt(istream *file,
int &chained, int &size, int &type, int &channel) {
chained = read_uchar_SI(file);
size = read_uchar_SI(file);
type = read_uchar_SI(file);
channel = read_uchar_SI(file);
if (feof(file)) {
if (file->eof() || file->fail()) {
return false;
}
@ -107,7 +107,7 @@ read_channel_pkt(FILE *file,
}
static void
read_rgb(xel *row_data, xelval *, FILE *file, int x, int repeat) {
read_rgb(xel *row_data, xelval *, istream *file, int x, int repeat) {
xelval red, grn, blu;
red = read_uchar_SI(file);
grn = read_uchar_SI(file);
@ -121,7 +121,7 @@ read_rgb(xel *row_data, xelval *, FILE *file, int x, int repeat) {
}
static void
read_alpha(xel *, xelval *alpha_data, FILE *file, int x, int repeat) {
read_alpha(xel *, xelval *alpha_data, istream *file, int x, int repeat) {
xelval alpha = read_uchar_SI(file);
while (repeat>0) {
@ -132,7 +132,7 @@ read_alpha(xel *, xelval *alpha_data, FILE *file, int x, int repeat) {
}
static void
read_rgba(xel *row_data, xelval *alpha_data, FILE *file, int x, int repeat) {
read_rgba(xel *row_data, xelval *alpha_data, istream *file, int x, int repeat) {
xelval red, grn, blu, alpha;
red = read_uchar_SI(file);
grn = read_uchar_SI(file);
@ -149,8 +149,8 @@ read_rgba(xel *row_data, xelval *alpha_data, FILE *file, int x, int repeat) {
static int
read_scanline(xel *row_data, xelval *alpha_data, int cols, FILE *file,
void (*read_data)(xel *row_data, xelval *alpha_data, FILE *file,
read_scanline(xel *row_data, xelval *alpha_data, int cols, istream *file,
void (*read_data)(xel *row_data, xelval *alpha_data, istream *file,
int x, int repeat),
int ctype) {
if (ctype==UNCOMPRESSED) {
@ -174,7 +174,7 @@ read_scanline(xel *row_data, xelval *alpha_data, int cols, FILE *file,
}
while (num>0) {
read_data(row_data, alpha_data, file, x, 1);
if (feof(file)) {
if (file->eof() || file->fail()) {
return false;
}
x++;
@ -191,7 +191,7 @@ read_scanline(xel *row_data, xelval *alpha_data, int cols, FILE *file,
return false;
}
read_data(row_data, alpha_data, file, x, num);
if (feof(file)) {
if (file->eof() || file->fail()) {
return false;
}
x += num;
@ -225,11 +225,11 @@ get_name() const {
// Function: PNMFileTypeSoftImage::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_SI associated with this particular file type.
// extensions_softimage associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeSoftImage::
get_num_extensions() const {
return num_extensions_SI;
return num_extensions_softimage;
}
////////////////////////////////////////////////////////////////////
@ -241,8 +241,8 @@ get_num_extensions() const {
////////////////////////////////////////////////////////////////////
string PNMFileTypeSoftImage::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_SI, string());
return extensions_SI[n];
nassertr(n >= 0 && n < num_extensions_softimage, string());
return extensions_softimage[n];
}
////////////////////////////////////////////////////////////////////
@ -292,7 +292,7 @@ matches_magic_number(const string &magic_number) const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeSoftImage::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
make_reader(istream *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
@ -305,7 +305,7 @@ make_reader(FILE *file, bool owns_file, const string &magic_number) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeSoftImage::
make_writer(FILE *file, bool owns_file) {
make_writer(ostream *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}
@ -317,7 +317,7 @@ make_writer(FILE *file, bool owns_file) {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeSoftImage::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
if (!read_magic_number(_file, magic_number, 4)) {
@ -346,10 +346,11 @@ Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
read_float(_file);
// Skip comment
fseek(_file, imageCommentLength, SEEK_CUR);
_file->seekg(imageCommentLength, ios::cur);
char pict_id[4];
if (fread(pict_id, 1, 4, _file) < 4) {
_file->read(pict_id, 4);
if (_file->gcount() < 4) {
_is_valid = false;
return;
}
@ -492,7 +493,7 @@ read_row(xel *row_data, xelval *alpha_data) {
static void
write_channel_pkt(FILE *file,
write_channel_pkt(ostream *file,
int chained, int size, int type, int channel) {
write_uchar_SI(file, chained);
write_uchar_SI(file, size);
@ -501,7 +502,7 @@ write_channel_pkt(FILE *file,
}
static void
write_rgb(xel *row_data, xelval *, FILE *file, int x) {
write_rgb(xel *row_data, xelval *, ostream *file, int x) {
write_uchar_SI(file, PPM_GETR(row_data[x]));
write_uchar_SI(file, PPM_GETG(row_data[x]));
write_uchar_SI(file, PPM_GETB(row_data[x]));
@ -513,7 +514,7 @@ compare_rgb(xel *row_data, xelval *, int x1, int x2) {
}
static void
write_gray(xel *row_data, xelval *, FILE *file, int x) {
write_gray(xel *row_data, xelval *, ostream *file, int x) {
write_uchar_SI(file, PPM_GETB(row_data[x]));
write_uchar_SI(file, PPM_GETB(row_data[x]));
write_uchar_SI(file, PPM_GETB(row_data[x]));
@ -525,7 +526,7 @@ compare_gray(xel *row_data, xelval *, int x1, int x2) {
}
static void
write_alpha(xel *, xelval *alpha_data, FILE *file, int x) {
write_alpha(xel *, xelval *alpha_data, ostream *file, int x) {
write_uchar_SI(file, alpha_data[x]);
}
@ -535,8 +536,8 @@ compare_alpha(xel *, xelval *alpha_data, int x1, int x2) {
}
static void
write_diff(xel *row_data, xelval *alpha_data, FILE *file,
void (*write_data)(xel *row_data, xelval *alpha_data, FILE *file,
write_diff(xel *row_data, xelval *alpha_data, ostream *file,
void (*write_data)(xel *row_data, xelval *alpha_data, ostream *file,
int x),
int tox, int length) {
if (length>0) {
@ -551,8 +552,8 @@ write_diff(xel *row_data, xelval *alpha_data, FILE *file,
}
static void
write_same(xel *row_data, xelval *alpha_data, FILE *file,
void (*write_data)(xel *row_data, xelval *alpha_data, FILE *file,
write_same(xel *row_data, xelval *alpha_data, ostream *file,
void (*write_data)(xel *row_data, xelval *alpha_data, ostream *file,
int x),
int tox, int length) {
if (length==1) {
@ -571,11 +572,11 @@ write_same(xel *row_data, xelval *alpha_data, FILE *file,
static void
write_scanline(xel *row_data, xelval *alpha_data, int cols, FILE *file,
write_scanline(xel *row_data, xelval *alpha_data, int cols, ostream *file,
int (*compare_data)(xel *row_data, xelval *alpha_data,
int x1, int x2),
void (*write_data)(xel *row_data, xelval *alpha_data,
FILE *file, int x)) {
ostream *file, int x)) {
int run_length = 0;
int x = 0;
@ -666,7 +667,7 @@ write_scanline(xel *row_data, xelval *alpha_data, int cols, FILE *file,
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeSoftImage::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
Writer(PNMFileType *type, ostream *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
}
@ -706,8 +707,8 @@ write_header() {
write_ushort_SI(_file, SOFTIMAGE_MAGIC2);
write_float(_file, imageVersionNumber);
fwrite(imageComment, 1, imageCommentLength, _file);
fwrite("PICT", 1, 4, _file);
_file->write(imageComment, imageCommentLength);
_file->write("PICT", 4);
write_ushort_SI(_file, _x_size);
write_ushort_SI(_file, _y_size);
@ -757,7 +758,7 @@ write_row(xel *row_data, xelval *alpha_data) {
write_scanline(row_data, alpha_data, _x_size, _file, compare_alpha, write_alpha);
}
return !ferror(_file);
return !_file->fail();
}

View File

@ -19,11 +19,11 @@
#ifndef PNMFILETYPESOFTIMAGE_H
#define PNMFILETYPESOFTIMAGE_H
#include <pandabase.h>
#include "pandabase.h"
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
#include "pnmFileType.h"
#include "pnmReader.h"
#include "pnmWriter.h"
////////////////////////////////////////////////////////////////////
// Class : PNMFileTypeSoftImage
@ -42,14 +42,14 @@ public:
virtual bool has_magic_number() const;
virtual bool matches_magic_number(const string &magic_number) const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
virtual bool supports_read_row() const;
virtual bool read_row(xel *array, xelval *alpha);
@ -61,7 +61,7 @@ public:
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
Writer(PNMFileType *type, ostream *file, bool owns_file);
virtual bool supports_write_row() const;
virtual bool write_header();

View File

@ -55,10 +55,10 @@
#include <string.h>
static const char * const extensions_TGA[] = {
static const char * const extensions_tga[] = {
"tga"
};
static const int num_extensions_TGA = sizeof(extensions_TGA) / sizeof(const char *);
static const int num_extensions_tga = sizeof(extensions_tga) / sizeof(const char *);
TypeHandle PNMFileTypeTGA::_type_handle;
@ -123,11 +123,11 @@ get_name() const {
// Function: PNMFileTypeTGA::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_TGA associated with this particular file type.
// extensions_tga associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeTGA::
get_num_extensions() const {
return num_extensions_TGA;
return num_extensions_tga;
}
////////////////////////////////////////////////////////////////////
@ -139,8 +139,8 @@ get_num_extensions() const {
////////////////////////////////////////////////////////////////////
string PNMFileTypeTGA::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_TGA, string());
return extensions_TGA[n];
nassertr(n >= 0 && n < num_extensions_tga, string());
return extensions_tga[n];
}
////////////////////////////////////////////////////////////////////
@ -163,7 +163,7 @@ get_suggested_extension() const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeTGA::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
make_reader(istream *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
@ -176,7 +176,7 @@ make_reader(FILE *file, bool owns_file, const string &magic_number) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeTGA::
make_writer(FILE *file, bool owns_file) {
make_writer(ostream *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}
@ -188,7 +188,7 @@ make_writer(FILE *file, bool owns_file) {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeTGA::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
tga_head = new ImageHeader;
@ -369,7 +369,7 @@ read_data(xel *array, xelval *alpha) {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeTGA::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
Writer(PNMFileType *type, ostream *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
tgaHeader = new ImageHeader;
@ -538,14 +538,14 @@ write_data(xel *array, xelval *) {
{
if ( runlength[col] > 0 )
{
fputc( 0x80 + runlength[col] - 1, _file );
_file->put( 0x80 + runlength[col] - 1 );
put_pixel(&(array[realrow * cols + col]),
tgaHeader->ImgType, _maxval, cht );
col += runlength[col];
}
else if ( runlength[col] < 0 )
{
fputc( -runlength[col] - 1, _file );
_file->put( -runlength[col] - 1 );
for ( i = 0; i < -runlength[col]; ++i )
put_pixel(&(array[realrow * cols + (col + i)]),
tgaHeader->ImgType, _maxval, cht );
@ -597,7 +597,7 @@ make_PNMFileTypeTGA(const FactoryParams &params) {
}
void PNMFileTypeTGA::Reader::
readtga( FILE *ifp, struct ImageHeader *tgaP, const string &magic_number ) {
readtga( istream *ifp, struct ImageHeader *tgaP, const string &magic_number ) {
unsigned char flags;
ImageIDField junk;
@ -641,11 +641,11 @@ readtga( FILE *ifp, struct ImageHeader *tgaP, const string &magic_number ) {
tgaP->IntrLve = ( flags & 0xc0 ) >> 6;
if ( tgaP->IDLength != 0 )
fread( junk, 1, (int) tgaP->IDLength, ifp );
ifp->read(junk, (int) tgaP->IDLength);
}
void PNMFileTypeTGA::Reader::
get_map_entry( FILE *ifp, pixel *Value, int Size, gray *Alpha ) {
get_map_entry( istream *ifp, pixel *Value, int Size, gray *Alpha ) {
unsigned char j, k, r, g, b, a;
/* Read appropriate number of bytes, break into rgb & put in map. */
@ -687,7 +687,7 @@ get_map_entry( FILE *ifp, pixel *Value, int Size, gray *Alpha ) {
void PNMFileTypeTGA::Reader::
get_pixel( FILE *ifp, pixel *dest, int Size, gray *alpha_p) {
get_pixel( istream *ifp, pixel *dest, int Size, gray *alpha_p) {
static pixval Red, Grn, Blu;
static pixval Alpha;
unsigned char j, k;
@ -769,10 +769,11 @@ PixEncode:
unsigned char PNMFileTypeTGA::Reader::
getbyte( FILE *ifp ) {
getbyte( istream *ifp ) {
unsigned char c;
if ( fread( (char*) &c, 1, 1, ifp ) != 1 )
c = ifp->get();
if (ifp->fail() || ifp->eof())
pm_error( "EOF / read error" );
return c;
@ -783,28 +784,28 @@ writetga( struct ImageHeader *tgaP, char *id )
{
unsigned char flags;
fputc( tgaP->IDLength, _file );
fputc( tgaP->CoMapType, _file );
fputc( tgaP->ImgType, _file );
fputc( tgaP->Index_lo, _file );
fputc( tgaP->Index_hi, _file );
fputc( tgaP->Length_lo, _file );
fputc( tgaP->Length_hi, _file );
fputc( tgaP->CoSize, _file );
fputc( tgaP->X_org_lo, _file );
fputc( tgaP->X_org_hi, _file );
fputc( tgaP->Y_org_lo, _file );
fputc( tgaP->Y_org_hi, _file );
fputc( tgaP->Width_lo, _file );
fputc( tgaP->Width_hi, _file );
fputc( tgaP->Height_lo, _file );
fputc( tgaP->Height_hi, _file );
fputc( tgaP->PixelSize, _file );
_file->put( tgaP->IDLength );
_file->put( tgaP->CoMapType );
_file->put( tgaP->ImgType );
_file->put( tgaP->Index_lo );
_file->put( tgaP->Index_hi );
_file->put( tgaP->Length_lo );
_file->put( tgaP->Length_hi );
_file->put( tgaP->CoSize );
_file->put( tgaP->X_org_lo );
_file->put( tgaP->X_org_hi );
_file->put( tgaP->Y_org_lo );
_file->put( tgaP->Y_org_hi );
_file->put( tgaP->Width_lo );
_file->put( tgaP->Width_hi );
_file->put( tgaP->Height_lo );
_file->put( tgaP->Height_hi );
_file->put( tgaP->PixelSize );
flags = ( tgaP->AttBits & 0xf ) | ( ( tgaP->Rsrvd & 0x1 ) << 4 ) |
( ( tgaP->OrgBit & 0x1 ) << 5 ) | ( ( tgaP->OrgBit & 0x3 ) << 6 );
fputc( flags, _file );
_file->put( flags );
if ( tgaP->IDLength )
fwrite( id, 1, (int) tgaP->IDLength, _file );
_file->write( id, (int) tgaP->IDLength );
}
void PNMFileTypeTGA::Writer::
@ -824,8 +825,8 @@ put_map_entry( pixel* valueP, int size, pixval maxval )
PPM_DEPTH( p, *valueP, maxval, 31 );
j = (int) PPM_GETB( p ) | ( (int) PPM_GETG( p ) << 5 ) |
( (int) PPM_GETR( p ) << 10 );
fputc( j % 256, _file );
fputc( j / 256, _file );
_file->put( j % 256 );
_file->put( j / 256 );
break;
case 32:
@ -907,25 +908,25 @@ put_mono( pixel* pP, pixval maxval )
{
pixel p;
PPM_DEPTH( p, *pP, maxval, (pixval) 255 );
fputc( PPM_GETB( p ), _file );
_file->put( PPM_GETB( p ) );
}
void PNMFileTypeTGA::Writer::
put_map( pixel *pP, colorhash_table cht )
{
fputc( ppm_lookupcolor( cht, pP ), _file );
_file->put( ppm_lookupcolor( cht, pP ) );
}
void PNMFileTypeTGA::Writer::
put_rgb( pixel* pP, pixval maxval ) {
pixel p;
PPM_DEPTH( p, *pP, maxval, (pixval) 255 );
fputc( PPM_GETB( p ), _file );
_file->put( PPM_GETB( p ) );
if (is_grayscale()) {
fputc( PPM_GETB( p ), _file );
fputc( PPM_GETB( p ), _file );
_file->put( PPM_GETB( p ) );
_file->put( PPM_GETB( p ) );
} else {
fputc( PPM_GETG( p ), _file );
fputc( PPM_GETR( p ), _file );
_file->put( PPM_GETG( p ) );
_file->put( PPM_GETR( p ) );
}
}

View File

@ -47,24 +47,24 @@ public:
virtual string get_extension(int n) const;
virtual string get_suggested_extension() const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
virtual ~Reader();
virtual int read_data(xel *array, xelval *alpha);
private:
void readtga ( FILE* ifp, struct ImageHeader* tgaP, const string &magic_number );
void get_map_entry ( FILE* ifp, pixel* Value, int Size,
void readtga ( istream* ifp, struct ImageHeader* tgaP, const string &magic_number );
void get_map_entry ( istream* ifp, pixel* Value, int Size,
gray* Alpha);
void get_pixel ( FILE* ifp, pixel* dest, int Size, gray* alpha_p);
unsigned char getbyte ( FILE* ifp );
void get_pixel ( istream* ifp, pixel* dest, int Size, gray* alpha_p);
unsigned char getbyte ( istream* ifp );
int rows, cols, rlencoded, mapped;
struct ImageHeader *tga_head;
@ -75,7 +75,7 @@ public:
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
Writer(PNMFileType *type, ostream *file, bool owns_file);
virtual ~Writer();
virtual int write_data(xel *array, xelval *alpha);

View File

@ -32,10 +32,10 @@ extern "C" {
#include <tiffio.h>
}
static const char * const extensions_TIFF[] = {
static const char * const extensions_tiff[] = {
"tiff", "tif"
};
static const int num_extensions_TIFF = sizeof(extensions_TIFF) / sizeof(const char *);
static const int num_extensions_tiff = sizeof(extensions_tiff) / sizeof(const char *);
// These are configurable parameters to specify TIFF details on
// output. See tiff.h or type man pnmtotiff for a better explanation
@ -80,57 +80,115 @@ long tiff_rowsperstrip = 0;
#define PHOTOMETRIC_DEPTH 32768
#endif
// Here's a number of functions to support the stdio-FILE interface
// Here's a number of functions to support the iostream interface
// via the TIFF library.
static tsize_t
stdio_read(thandle_t fd, tdata_t buf, tsize_t size) {
return ((tsize_t)fread((void *)buf, 1, (size_t) size, (FILE *)fd));
istream_read(thandle_t fd, tdata_t buf, tsize_t size) {
istream *in = (istream *)fd;
in->read((char *)buf, size);
return in->gcount();
}
static tsize_t
stdio_write(thandle_t fd, tdata_t buf, tsize_t size) {
return ((tsize_t)fwrite((void *)buf, 1, (size_t) size, (FILE *)fd));
ostream_write(thandle_t fd, tdata_t buf, tsize_t size) {
ostream *out = (ostream *)fd;
out->write((char *)buf, size);
return out->fail() ? (tsize_t)0 : size;
}
static tsize_t
stdio_dont_read(thandle_t, tdata_t, tsize_t) {
// This no-op variant of stdio_read() is passed in when we open the
ostream_dont_read(thandle_t, tdata_t, tsize_t) {
// This no-op variant of istream_read() is passed in when we open the
// file for writing only. Shouldn't mix reads and writes.
return 0;
}
static tsize_t
stdio_dont_write(thandle_t, tdata_t, tsize_t) {
// This no-op variant of stdio_write() is passed in when we open the
istream_dont_write(thandle_t, tdata_t, tsize_t) {
// This no-op variant of ostream_write() is passed in when we open the
// file for reading only. Shouldn't mix reads and writes.
return 0;
}
static toff_t
stdio_seek(thandle_t fd, off_t off, int whence) {
fseek((FILE *)fd, (long)off, whence);
return (toff_t)ftell((FILE *)fd);
istream_seek(thandle_t fd, off_t off, int whence) {
istream *in = (istream *)fd;
ios::seek_dir dir;
switch (whence) {
case SEEK_SET:
dir = ios::beg;
break;
case SEEK_END:
dir = ios::end;
break;
case SEEK_CUR:
dir = ios::cur;
break;
default:
return in->tellg();
}
in->seekg(off, dir);
return in->tellg();
}
static toff_t
ostream_seek(thandle_t fd, off_t off, int whence) {
ostream *out = (ostream *)fd;
ios::seek_dir dir;
switch (whence) {
case SEEK_SET:
dir = ios::beg;
break;
case SEEK_END:
dir = ios::end;
break;
case SEEK_CUR:
dir = ios::cur;
break;
default:
return out->tellp();
}
out->seekp(off, dir);
return out->tellp();
}
static int
stdio_dont_close(thandle_t) {
iostream_dont_close(thandle_t) {
// We don't actually close the file; we'll leave that to PNMReader.
return true;
}
static toff_t
stdio_size(thandle_t fd) {
fseek((FILE *)fd, 0, SEEK_END);
return (toff_t)ftell((FILE *)fd);
istream_size(thandle_t fd) {
istream *in = (istream *)fd;
in->seekg(0, ios::end);
return in->tellg();
}
static toff_t
ostream_size(thandle_t fd) {
ostream *out = (ostream *)fd;
out->seekp(0, ios::end);
return out->tellp();
}
static int
stdio_map(thandle_t, tdata_t*, toff_t*) {
iostream_map(thandle_t, tdata_t*, toff_t*) {
return (0);
}
static void
stdio_unmap(thandle_t, tdata_t, toff_t) {
iostream_unmap(thandle_t, tdata_t, toff_t) {
}
TypeHandle PNMFileTypeTIFF::_type_handle;
@ -158,11 +216,11 @@ get_name() const {
// Function: PNMFileTypeTIFF::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_TIFF associated with this particular file type.
// extensions associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeTIFF::
get_num_extensions() const {
return num_extensions_TIFF;
return num_extensions_tiff;
}
////////////////////////////////////////////////////////////////////
@ -174,8 +232,8 @@ get_num_extensions() const {
////////////////////////////////////////////////////////////////////
string PNMFileTypeTIFF::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_TIFF, string());
return extensions_TIFF[n];
nassertr(n >= 0 && n < num_extensions_tiff, string());
return extensions_tiff[n];
}
////////////////////////////////////////////////////////////////////
@ -225,7 +283,7 @@ matches_magic_number(const string &magic_number) const {
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeTIFF::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
make_reader(istream *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
@ -238,7 +296,7 @@ make_reader(FILE *file, bool owns_file, const string &magic_number) {
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeTIFF::
make_writer(FILE *file, bool owns_file) {
make_writer(ostream *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}
@ -250,7 +308,7 @@ make_writer(FILE *file, bool owns_file) {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeTIFF::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
bool grayscale;
@ -264,18 +322,25 @@ Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
for (string::reverse_iterator mi = magic_number.rbegin();
mi != magic_number.rend();
mi++) {
ungetc(*mi, _file);
_file->putback(*mi);
}
if (_file->fail()) {
pnmimage_tiff_cat.error()
<< "Unable to put back magic number.\n";
_is_valid = false;
}
tif = TIFFClientOpen("TIFF file", "r",
(thandle_t) _file,
stdio_read, stdio_dont_write,
(TIFFSeekProc)stdio_seek,
stdio_dont_close, stdio_size,
stdio_map, stdio_unmap);
if (_is_valid) {
tif = TIFFClientOpen("TIFF file", "r",
(thandle_t) _file,
istream_read, istream_dont_write,
(TIFFSeekProc)istream_seek,
iostream_dont_close, istream_size,
iostream_map, iostream_unmap);
if ( tif == NULL ) {
_is_valid = false;
if ( tif == NULL ) {
_is_valid = false;
}
}
if (_is_valid) {
@ -551,7 +616,7 @@ read_row(xel *row_data, xelval *alpha_data) {
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeTIFF::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
Writer(PNMFileType *type, ostream *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
}
@ -645,10 +710,10 @@ write_data(xel *array, xelval *alpha) {
/* Open output file. */
tif = TIFFClientOpen("TIFF file", "w",
(thandle_t) _file,
stdio_dont_read, stdio_write,
(TIFFSeekProc)stdio_seek,
stdio_dont_close, stdio_size,
stdio_map, stdio_unmap);
ostream_dont_read, ostream_write,
(TIFFSeekProc)ostream_seek,
iostream_dont_close, ostream_size,
iostream_map, iostream_unmap);
if ( tif == NULL ) {
return false;
}

View File

@ -19,11 +19,11 @@
#ifndef PNMFILETYPETIFF_H
#define PNMFILETYPETIFF_H
#include <pandabase.h>
#include "pandabase.h"
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
#include "pnmFileType.h"
#include "pnmReader.h"
#include "pnmWriter.h"
#define TIFF_COLORMAP_MAXCOLORS 1024
@ -44,14 +44,14 @@ public:
virtual bool has_magic_number() const;
virtual bool matches_magic_number(const string &magic_number) const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
virtual PNMReader *make_reader(istream *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
virtual ~Reader();
virtual bool supports_read_row() const;
@ -68,7 +68,7 @@ public:
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
Writer(PNMFileType *type, ostream *file, bool owns_file);
virtual int write_data(xel *array, xelval *alpha);
};

View File

@ -1,428 +0,0 @@
// Filename: pnmFileTypeYUV.cxx
// Created by: drose (19Jun00)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license. You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://www.panda3d.org/license.txt .
//
// To contact the maintainers of this program write to
// panda3d@yahoogroups.com .
//
////////////////////////////////////////////////////////////////////
// Much code in this file is borrowed from Netpbm, specifically yuvtoppm.c
// and ppmtoyuv.c.
/* yuvtoppm.c - convert Abekas YUV bytes into a portable pixmap
**
** by Marc Boucher
** Internet: marc@PostImage.cxxOM
**
** Based on Example Conversion Program, A60/A64 Digital Video Interface
** Manual, page 69
**
** Uses integer arithmetic rather than floating point for better performance
**
** Copyright (C) 1991 by DHD PostImage Inc.
** Copyright (C) 1987 by Abekas Video Systems Inc.
** Copyright (C) 1991 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
/* ppmtoyuv.c - convert a portable pixmap into an Abekas YUV file
**
** by Marc Boucher
** Internet: marc@PostImage.cxxOM
**
** Based on Example Conversion Program, A60/A64 Digital Video Interface
** Manual, page 69.
**
** Copyright (C) 1991 by DHD PostImage Inc.
** Copyright (C) 1987 by Abekas Video Systems Inc.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
#include "pnmFileTypeYUV.h"
#include "config_pnmimagetypes.h"
#include <pnmFileTypeRegistry.h>
#include <bamReader.h>
/* x must be signed for the following to work correctly */
#define limit(x) (xelval)(((x>0xffffff)?0xff0000:((x<=0xffff)?0:x&0xff0000))>>16)
static const char * const extensions_YUV[] = {
"yuv"
};
static const int num_extensions_YUV = sizeof(extensions_YUV) / sizeof(const char *);
TypeHandle PNMFileTypeYUV::_type_handle;
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeYUV::
PNMFileTypeYUV() {
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::get_name
// Access: Public, Virtual
// Description: Returns a few words describing the file type.
////////////////////////////////////////////////////////////////////
string PNMFileTypeYUV::
get_name() const {
return "Abekas YUV";
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::get_num_extensions
// Access: Public, Virtual
// Description: Returns the number of different possible filename
// extensions_YUV associated with this particular file type.
////////////////////////////////////////////////////////////////////
int PNMFileTypeYUV::
get_num_extensions() const {
return num_extensions_YUV;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::get_extension
// Access: Public, Virtual
// Description: Returns the nth possible filename extension
// associated with this particular file type, without a
// leading dot.
////////////////////////////////////////////////////////////////////
string PNMFileTypeYUV::
get_extension(int n) const {
nassertr(n >= 0 && n < num_extensions_YUV, string());
return extensions_YUV[n];
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::get_suggested_extension
// Access: Public, Virtual
// Description: Returns a suitable filename extension (without a
// leading dot) to suggest for files of this type, or
// empty string if no suggestions are available.
////////////////////////////////////////////////////////////////////
string PNMFileTypeYUV::
get_suggested_extension() const {
return "yuv";
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::make_reader
// Access: Public, Virtual
// Description: Allocates and returns a new PNMReader suitable for
// reading from this file type, if possible. If reading
// from this file type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMReader *PNMFileTypeYUV::
make_reader(FILE *file, bool owns_file, const string &magic_number) {
init_pnm();
return new Reader(this, file, owns_file, magic_number);
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::make_writer
// Access: Public, Virtual
// Description: Allocates and returns a new PNMWriter suitable for
// reading from this file type, if possible. If writing
// files of this type is not supported, returns NULL.
////////////////////////////////////////////////////////////////////
PNMWriter *PNMFileTypeYUV::
make_writer(FILE *file, bool owns_file) {
init_pnm();
return new Writer(this, file, owns_file);
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Reader::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeYUV::Reader::
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number) :
PNMReader(type, file, owns_file)
{
yuvbuf = NULL;
// Hope we can ungetc() more than one character.
for (string::reverse_iterator mi = magic_number.rbegin();
mi != magic_number.rend();
mi++) {
ungetc(*mi, _file);
}
_x_size = yuv_xsize;
_y_size = yuv_ysize;
_num_channels = 3;
if (_x_size <= 0 || _y_size <= 0) {
_is_valid = false;
return;
}
nassertv(255 <= PGM_MAXMAXVAL);
yuvbuf = (unsigned char *) pm_allocrow(_x_size, 2);
_maxval = 255;
if (pnmimage_yuv_cat.is_debug()) {
pnmimage_yuv_cat.debug()
<< "Reading YUV " << *this << "\n";
}
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Reader::Destructor
// Access: Public, Virtual
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeYUV::Reader::
~Reader() {
if (yuvbuf!=NULL) {
pm_freerow((char *)yuvbuf);
}
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Reader::supports_read_row
// Access: Public, Virtual
// Description: Returns true if this particular PNMReader supports a
// streaming interface to reading the data: that is, it
// is capable of returning the data one row at a time,
// via repeated calls to read_row(). Returns false if
// the only way to read from this file is all at once,
// via read_data().
////////////////////////////////////////////////////////////////////
bool PNMFileTypeYUV::Reader::
supports_read_row() const {
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Reader::read_row
// Access: Public, Virtual
// Description: If supports_read_row(), above, returns true, this
// function may be called repeatedly to read the image,
// one horizontal row at a time, beginning from the top.
// Returns true if the row is successfully read, false
// if there is an error or end of file.
////////////////////////////////////////////////////////////////////
bool PNMFileTypeYUV::Reader::
read_row(xel *row_data, xelval *) {
long y, u, v, y1, r, g, b;
unsigned char *yuvptr;
int col;
if (fread(yuvbuf, _x_size * 2, 1, _file) != 1) {
// Short file--perhaps it's just a field instead of a full frame.
// Since the YUV format does not include a length designation, we'll
// have to assume this is not a problem and just truncate here.
return false;
}
yuvptr = yuvbuf;
for (col = 0; col < _x_size; col += 2) {
u = (int)yuvptr[0] - 128;
y = (int)yuvptr[1] - 16;
if (y < 0) y = 0;
v = (int)yuvptr[2] - 128;
y1 = (int)yuvptr[3] - 16;
if (y1 < 0) y1 = 0;
r = 104635 * v;
g = -25690 * u + -53294 * v;
b = 132278 * u;
y*=76310; y1*=76310;
PPM_ASSIGN(row_data[col], limit(r+y), limit(g+y), limit(b+y));
PPM_ASSIGN(row_data[col+1], limit(r+y1), limit(g+y1), limit(b+y1));
yuvptr += 4;
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Writer::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeYUV::Writer::
Writer(PNMFileType *type, FILE *file, bool owns_file) :
PNMWriter(type, file, owns_file)
{
yuvbuf = NULL;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Writer::Destructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PNMFileTypeYUV::Writer::
~Writer() {
if (yuvbuf!=NULL) {
pm_freerow((char *)yuvbuf);
}
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Writer::supports_write_row
// Access: Public, Virtual
// Description: Returns true if this particular PNMWriter supports a
// streaming interface to writing the data: that is, it
// is capable of writing the image one row at a time,
// via repeated calls to write_row(). Returns false if
// the only way to write from this file is all at once,
// via write_data().
////////////////////////////////////////////////////////////////////
bool PNMFileTypeYUV::Writer::
supports_write_row() const {
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Writer::write_header
// Access: Public, Virtual
// Description: If supports_write_row(), above, returns true, this
// function may be called to write out the image header
// in preparation to writing out the image data one row
// at a time. Returns true if the header is
// successfully written, false if there is an error.
//
// It is the user's responsibility to fill in the header
// data via calls to set_x_size(), set_num_channels(),
// etc., or copy_header_from(), before calling
// write_header().
////////////////////////////////////////////////////////////////////
bool PNMFileTypeYUV::Writer::
write_header() {
if (yuvbuf!=NULL) {
pm_freerow((char *)yuvbuf);
}
yuvbuf = (unsigned char *) pm_allocrow( _x_size, 2 );
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::Writer::write_row
// Access: Public, Virtual
// Description: If supports_write_row(), above, returns true, this
// function may be called repeatedly to write the image,
// one horizontal row at a time, beginning from the top.
// Returns true if the row is successfully written,
// false if there is an error.
//
// You must first call write_header() before writing the
// individual rows. It is also important to delete the
// PNMWriter class after successfully writing the last
// row. Failing to do this may result in some data not
// getting flushed!
////////////////////////////////////////////////////////////////////
bool PNMFileTypeYUV::Writer::
write_row(xel *row_data, xelval *) {
int col;
unsigned long y1, y2=0, u=0, v=0, u0=0, u1, u2, v0=0, v1, v2;
static const int max_byte = 255;
unsigned char *yuvptr;
for (col = 0, yuvptr=yuvbuf; col < _x_size; col += 2) {
pixval r, g, b;
/* first pixel gives Y and 0.5 of chroma */
r = (pixval)(max_byte * PPM_GETR(row_data[col])/_maxval);
g = (pixval)(max_byte * PPM_GETG(row_data[col])/_maxval);
b = (pixval)(max_byte * PPM_GETB(row_data[col])/_maxval);
y1 = 16829 * r + 33039 * g + 6416 * b + (0xffff & y2);
u1 = -4853 * r - 9530 * g + 14383 * b;
v1 = 14386 * r - 12046 * g - 2340 * b;
/* second pixel just yields a Y and 0.25 U, 0.25 V */
r = (pixval)(max_byte * PPM_GETR(row_data[col])/_maxval);
g = (pixval)(max_byte * PPM_GETG(row_data[col])/_maxval);
b = (pixval)(max_byte * PPM_GETB(row_data[col])/_maxval);
y2 = 16829 * r + 33039 * g + 6416 * b + (0xffff & y1);
u2 = -2426 * r - 4765 * g + 7191 * b;
v2 = 7193 * r - 6023 * g - 1170 * b;
/* filter the chroma */
u = u0 + u1 + u2 + (0xffff & u);
v = v0 + v1 + v2 + (0xffff & v);
u0 = u2;
v0 = v2;
*yuvptr++ = (unsigned char)((u >> 16) + 128);
*yuvptr++ = (unsigned char)((y1 >> 16) + 16);
*yuvptr++ = (unsigned char)((v >> 16) + 128);
*yuvptr++ = (unsigned char)((y2 >> 16) + 16);
}
fwrite(yuvbuf, _x_size*2, 1, _file);
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::register_with_read_factory
// Access: Public, Static
// Description: Registers the current object as something that can be
// read from a Bam file.
////////////////////////////////////////////////////////////////////
void PNMFileTypeYUV::
register_with_read_factory() {
BamReader::get_factory()->
register_factory(get_class_type(), make_PNMFileTypeYUV);
}
////////////////////////////////////////////////////////////////////
// Function: PNMFileTypeYUV::make_PNMFileTypeYUV
// Access: Protected, Static
// Description: This method is called by the BamReader when an object
// of this type is encountered in a Bam file; it should
// allocate and return a new object with all the data
// read.
//
// In the case of the PNMFileType objects, since these
// objects are all shared, we just pull the object from
// the registry.
////////////////////////////////////////////////////////////////////
TypedWritable *PNMFileTypeYUV::
make_PNMFileTypeYUV(const FactoryParams &params) {
return PNMFileTypeRegistry::get_ptr()->get_type_by_handle(get_class_type());
}

View File

@ -1,100 +0,0 @@
// Filename: pnmFileTypeYUV.h
// Created by: drose (17Jun00)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license. You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://www.panda3d.org/license.txt .
//
// To contact the maintainers of this program write to
// panda3d@yahoogroups.com .
//
////////////////////////////////////////////////////////////////////
#ifndef PNMFILETYPEYUV_H
#define PNMFILETYPEYUV_H
#include <pandabase.h>
#include <pnmFileType.h>
#include <pnmReader.h>
#include <pnmWriter.h>
////////////////////////////////////////////////////////////////////
// Class : PNMFileTypeYUV
// Description : For reading and writing Abekas YUV files.
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA PNMFileTypeYUV : public PNMFileType {
public:
PNMFileTypeYUV();
virtual string get_name() const;
virtual int get_num_extensions() const;
virtual string get_extension(int n) const;
virtual string get_suggested_extension() const;
virtual PNMReader *make_reader(FILE *file, bool owns_file = true,
const string &magic_number = string());
virtual PNMWriter *make_writer(FILE *file, bool owns_file = true);
public:
class Reader : public PNMReader {
public:
Reader(PNMFileType *type, FILE *file, bool owns_file, string magic_number);
virtual ~Reader();
virtual bool supports_read_row() const;
virtual bool read_row(xel *array, xelval *alpha);
private:
unsigned char *yuvbuf;
};
class Writer : public PNMWriter {
public:
Writer(PNMFileType *type, FILE *file, bool owns_file);
virtual ~Writer();
virtual bool supports_write_row() const;
virtual bool write_header();
virtual bool write_row(xel *array, xelval *alpha);
private:
unsigned char *yuvbuf;
};
// The TypedWritable interface follows.
public:
static void register_with_read_factory();
protected:
static TypedWritable *make_PNMFileTypeYUV(const FactoryParams &params);
public:
static TypeHandle get_class_type() {
return _type_handle;
}
static void init_type() {
PNMFileType::init_type();
register_type(_type_handle, "PNMFileTypeYUV",
PNMFileType::get_class_type());
}
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
private:
static TypeHandle _type_handle;
};
#endif

View File

@ -5,6 +5,5 @@
#include "pnmFileTypeBMPWriter.cxx"
#include "pnmFileTypeIMG.cxx"
#include "pnmFileTypeBMP.cxx"
#include "pnmFileTypeRadiance.cxx"

View File

@ -4,4 +4,3 @@
#include "pnmFileTypeSGIWriter.cxx"
#include "pnmFileTypeSoftImage.cxx"
#include "pnmFileTypeTGA.cxx"
#include "pnmFileTypeYUV.cxx"

View File

@ -1,107 +0,0 @@
/* Filename: resolu.c
* Created by:
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* PANDA 3D SOFTWARE
* Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
*
* All use of this software is subject to the terms of the Panda 3d
* Software license. You should have received a copy of this license
* along with this source code; you will also find a current copy of
* the license at http://www.panda3d.org/license.txt .
*
* To contact the maintainers of this program write to
* panda3d@yahoogroups.com .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Read and write image resolutions.
*/
#include <stdio.h>
#include <stdlib.h>
#include "resolu.h"
char resolu_buf[RESOLU_BUFLEN]; /* resolution line buffer */
int str2resolu(register RESOLU *, char *);
char *resolu2str(char *, register RESOLU *);
void
fputresolu(int ord, int sl, int ns, FILE *fp) /* put out picture dimensions */
{
RESOLU rs;
if ((rs.orient = ord) & YMAJOR) {
rs.xr = sl;
rs.yr = ns;
} else {
rs.xr = ns;
rs.yr = sl;
}
fputsresolu(&rs, fp);
}
int
fgetresolu(int *sl, int *ns, FILE *fp) /* get picture dimensions */
{
RESOLU rs;
if (!fgetsresolu(&rs, fp))
return(-1);
if (rs.orient & YMAJOR) {
*sl = rs.xr;
*ns = rs.yr;
} else {
*sl = rs.yr;
*ns = rs.xr;
}
return(rs.orient);
}
char *
resolu2str(char *buf, register RESOLU *rp) /* convert resolution struct to line */
{
if (rp->orient&YMAJOR)
sprintf(buf, "%cY %d %cX %d\n",
rp->orient&YDECR ? '-' : '+', rp->yr,
rp->orient&XDECR ? '-' : '+', rp->xr);
else
sprintf(buf, "%cX %d %cY %d\n",
rp->orient&XDECR ? '-' : '+', rp->xr,
rp->orient&YDECR ? '-' : '+', rp->yr);
return(buf);
}
int str2resolu(register RESOLU *rp, char *buf) /* convert resolution line to struct */
{
register char *xndx, *yndx;
register char *cp;
if (buf == NULL)
return(0);
xndx = yndx = NULL;
for (cp = buf; *cp; cp++)
if (*cp == 'X')
xndx = cp;
else if (*cp == 'Y')
yndx = cp;
if (xndx == NULL || yndx == NULL)
return(0);
rp->orient = 0;
if (xndx > yndx) rp->orient |= YMAJOR;
if (xndx[-1] == '-') rp->orient |= XDECR;
if (yndx[-1] == '-') rp->orient |= YDECR;
if ((rp->xr = atoi(xndx+1)) <= 0)
return(0);
if ((rp->yr = atoi(yndx+1)) <= 0)
return(0);
return(1);
}

View File

@ -1,66 +0,0 @@
/* Filename: resolu.h
* Created by:
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* PANDA 3D SOFTWARE
* Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
*
* All use of this software is subject to the terms of the Panda 3d
* Software license. You should have received a copy of this license
* along with this source code; you will also find a current copy of
* the license at http://www.panda3d.org/license.txt .
*
* To contact the maintainers of this program write to
* panda3d@yahoogroups.com .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* SCCSid "@(#)resolu.h 2.2 6/4/93 LBL" */
/*
* Definitions for resolution line in image file.
*
* True image orientation is defined by an xy coordinate system
* whose origin is at the lower left corner of the image, with
* x increasing to the right and y increasing in the upward direction.
* This true orientation is independent of how the pixels are actually
* ordered in the file, which is indicated by the resolution line.
* This line is of the form "{+-}{XY} xyres {+-}{YX} yxres\n".
* A typical line for a 1024x600 image might be "-Y 600 +X 1024\n",
* indicating that the scanlines are in English text order (PIXSTANDARD).
*/
/* flags for scanline ordering */
#define XDECR 1
#define YDECR 2
#define YMAJOR 4
/* standard scanline ordering */
#define PIXSTANDARD (YMAJOR|YDECR)
#define PIXSTDFMT "-Y %d +X %d\n"
/* structure for image dimensions */
typedef struct {
int orient; /* orientation (from flags above) */
int xr, yr; /* x and y resolution */
} RESOLU;
/* macros to get scanline length and number */
#define scanlen(rs) ((rs)->or & YMAJOR ? (rs)->xr : (rs)->yr)
#define numscans(rs) ((rs)->or & YMAJOR ? (rs)->yr : (rs)->xr)
/* resolution string buffer and its size */
#define RESOLU_BUFLEN 32
extern char resolu_buf[RESOLU_BUFLEN];
/* macros for reading/writing resolution struct */
#define fputsresolu(rs,fp) fputs(resolu2str(resolu_buf,rs),fp)
#define fgetsresolu(rs,fp) str2resolu(rs, \
fgets(resolu_buf,RESOLU_BUFLEN,fp))
/* reading/writing of standard ordering */
#define fprtresolu(sl,ns,fp) fprintf(fp,PIXSTDFMT,ns,sl)
#define fscnresolu(sl,ns,fp) (fscanf(fp,PIXSTDFMT,ns,sl)==2)
extern char *resolu2str();