26#define INSANE_SIZE 20000
32static const char *
const extensions_img[] = {
35static const int num_extensions_img =
sizeof(extensions_img) /
sizeof(
const char *);
49string PNMFileTypeIMG::
51 return "Raw binary RGB";
59get_num_extensions()
const {
60 return num_extensions_img;
67string PNMFileTypeIMG::
68get_extension(
int n)
const {
69 nassertr(n >= 0 && n < num_extensions_img,
string());
70 return extensions_img[n];
77string PNMFileTypeIMG::
78get_suggested_extension()
const {
88make_reader(istream *file,
bool owns_file,
const string &magic_number) {
90 return new Reader(
this, file, owns_file, magic_number);
99make_writer(ostream *file,
bool owns_file) {
101 return new Writer(
this, file, owns_file);
106read_ulong(istream *file) {
108 return pm_readbiglong(file, (
long *)&x)==0 ? x : 0;
112read_ushort_IMG(istream *file) {
114 return pm_readbigshort(file, (
short *)&x)==0 ? x : 0;
118read_uchar_IMG(istream *file) {
121 return (x!=EOF) ? (
unsigned char)x : 0;
125write_ulong(ostream *file,
unsigned long x) {
126 pm_writebiglong(file, (
long)x);
130write_ushort_IMG(ostream *file,
unsigned long x) {
131 pm_writebigshort(file, (
short)(
long)x);
135write_uchar_IMG(ostream *file,
unsigned char x) {
142PNMFileTypeIMG::Reader::
143Reader(
PNMFileType *type, istream *file,
bool owns_file,
string magic_number) :
146 if (img_header_type == IHT_long) {
147 if (!read_magic_number(_file, magic_number, 8)) {
150 if (pnmimage_img_cat.is_debug()) {
151 pnmimage_img_cat.debug()
152 <<
"IMG image file appears to be empty.\n";
159 ((
unsigned char)magic_number[0] << 24) |
160 ((
unsigned char)magic_number[1] << 16) |
161 ((
unsigned char)magic_number[2] << 8) |
162 ((
unsigned char)magic_number[3]);
165 ((
unsigned char)magic_number[4] << 24) |
166 ((
unsigned char)magic_number[5] << 16) |
167 ((
unsigned char)magic_number[6] << 8) |
168 ((
unsigned char)magic_number[7]);
170 }
else if (img_header_type == IHT_short) {
171 if (!read_magic_number(_file, magic_number, 4)) {
172 if (pnmimage_img_cat.is_debug()) {
173 pnmimage_img_cat.debug()
174 <<
"IMG image file appears to be empty.\n";
181 ((
unsigned char)magic_number[0] << 8) |
182 ((
unsigned char)magic_number[1]);
185 ((
unsigned char)magic_number[2] << 8) |
186 ((
unsigned char)magic_number[3]);
189 _x_size = img_size[0];
190 _y_size = img_size[1];
193 if (_x_size == 0 || _y_size == 0 ||
194 _x_size > INSANE_SIZE || _y_size > INSANE_SIZE) {
196 if (img_header_type == IHT_none) {
197 pnmimage_img_cat.error()
198 <<
"Must specify img-xsize and img-ysize to load headerless raw files.\n";
200 pnmimage_img_cat.debug()
201 <<
"IMG file does not have a valid xsize,ysize header.\n";
209 if (pnmimage_img_cat.is_debug()) {
210 pnmimage_img_cat.debug()
211 <<
"Reading IMG " << *
this <<
"\n";
221bool PNMFileTypeIMG::Reader::
222supports_read_row()
const {
232bool PNMFileTypeIMG::Reader::
233read_row(
xel *row_data, xelval *,
int x_size,
int) {
235 xelval red, grn, blu;
236 for (x = 0; x < x_size; x++) {
237 red = read_uchar_IMG(_file);
238 grn = read_uchar_IMG(_file);
239 blu = read_uchar_IMG(_file);
241 PPM_ASSIGN(row_data[x], red, grn, blu);
250PNMFileTypeIMG::Writer::
251Writer(
PNMFileType *type, ostream *file,
bool owns_file) :
262bool PNMFileTypeIMG::Writer::
263supports_write_row()
const {
277bool PNMFileTypeIMG::Writer::
279 if (img_header_type == IHT_long) {
280 write_ulong(_file, _x_size);
281 write_ulong(_file, _y_size);
282 }
else if (img_header_type == IHT_short) {
283 write_ushort_IMG(_file, _x_size);
284 write_ushort_IMG(_file, _y_size);
300bool PNMFileTypeIMG::Writer::
301write_row(
xel *row_data, xelval *) {
303 for (x = 0; x < _x_size; x++) {
304 write_uchar_IMG(_file, (
unsigned char)(255*PPM_GETR(row_data[x])/_maxval));
305 write_uchar_IMG(_file, (
unsigned char)(255*PPM_GETG(row_data[x])/_maxval));
306 write_uchar_IMG(_file, (
unsigned char)(255*PPM_GETB(row_data[x])/_maxval));
318register_with_read_factory() {
320 register_factory(get_class_type(), make_PNMFileTypeIMG);
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
PNMFileType * get_type_by_handle(TypeHandle handle) const
Returns the PNMFileType instance stored in the registry for the given TypeHandle, e....
static PNMFileTypeRegistry * get_global_ptr()
Returns a pointer to the global PNMFileTypeRegistry object.
This is the base class of a family of classes that represent particular image file types that PNMImag...
This is an abstract base class that defines the interface for reading image files of various types.
This is an abstract base class that defines the interface for writing image files of various types.
TypeHandle is the identifier used to differentiate C++ class types.
Base class for objects that can be written to and read from Bam files.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.