15 #include "pnmFileTypeIMG.h"
19 #include "config_pnmimagetypes.h"
21 #include "pnmFileTypeRegistry.h"
22 #include "bamReader.h"
27 #define INSANE_SIZE 20000
29 static const char *
const extensions_img[] = {
32 static const int num_extensions_img =
sizeof(extensions_img) /
sizeof(
const char *);
50 string PNMFileTypeIMG::
52 return "Raw binary RGB";
62 get_num_extensions()
const {
63 return num_extensions_img;
73 string PNMFileTypeIMG::
74 get_extension(
int n)
const {
75 nassertr(n >= 0 && n < num_extensions_img,
string());
76 return extensions_img[n];
86 string PNMFileTypeIMG::
87 get_suggested_extension()
const {
99 make_reader(istream *file,
bool owns_file,
const string &magic_number) {
101 return new Reader(
this, file, owns_file, magic_number);
112 make_writer(ostream *file,
bool owns_file) {
114 return new Writer(
this, file, owns_file);
119 read_ulong(istream *file) {
121 return pm_readbiglong(file, (
long *)&x)==0 ? x : 0;
124 inline unsigned short
125 read_ushort_IMG(istream *file) {
127 return pm_readbigshort(file, (
short *)&x)==0 ? x : 0;
131 read_uchar_IMG(istream *file) {
134 return (x!=EOF) ? (
unsigned char)x : 0;
138 write_ulong(ostream *file,
unsigned long x) {
139 pm_writebiglong(file, (
long)x);
143 write_ushort_IMG(ostream *file,
unsigned long x) {
144 pm_writebigshort(file, (
short)(
long)x);
148 write_uchar_IMG(ostream *file,
unsigned char x) {
157 PNMFileTypeIMG::Reader::
158 Reader(
PNMFileType *type, istream *file,
bool owns_file,
string magic_number) :
161 if (img_header_type == IHT_long) {
162 if (!read_magic_number(_file, magic_number, 8)) {
166 if (pnmimage_img_cat.is_debug()) {
167 pnmimage_img_cat.debug()
168 <<
"IMG image file appears to be empty.\n";
175 ((
unsigned char)magic_number[0] << 24) |
176 ((
unsigned char)magic_number[1] << 16) |
177 ((
unsigned char)magic_number[2] << 8) |
178 ((
unsigned char)magic_number[3]);
181 ((
unsigned char)magic_number[4] << 24) |
182 ((
unsigned char)magic_number[5] << 16) |
183 ((
unsigned char)magic_number[6] << 8) |
184 ((
unsigned char)magic_number[7]);
186 }
else if (img_header_type == IHT_short) {
187 if (!read_magic_number(_file, magic_number, 4)) {
188 if (pnmimage_img_cat.is_debug()) {
189 pnmimage_img_cat.debug()
190 <<
"IMG image file appears to be empty.\n";
197 ((
unsigned char)magic_number[0] << 8) |
198 ((
unsigned char)magic_number[1]);
201 ((
unsigned char)magic_number[2] << 8) |
202 ((
unsigned char)magic_number[3]);
205 _x_size = img_size[0];
206 _y_size = img_size[1];
209 if (_x_size == 0 || _y_size == 0 ||
210 _x_size > INSANE_SIZE || _y_size > INSANE_SIZE) {
212 if (img_header_type == IHT_none) {
213 pnmimage_img_cat.error()
214 <<
"Must specify img-xsize and img-ysize to load headerless raw files.\n";
216 pnmimage_img_cat.debug()
217 <<
"IMG file does not have a valid xsize,ysize header.\n";
225 if (pnmimage_img_cat.is_debug()) {
226 pnmimage_img_cat.debug()
227 <<
"Reading IMG " << *
this <<
"\n";
241 bool PNMFileTypeIMG::Reader::
242 supports_read_row()
const {
255 bool PNMFileTypeIMG::Reader::
256 read_row(
xel *row_data, xelval *,
int x_size,
int) {
258 xelval red, grn, blu;
259 for (x = 0; x < x_size; x++) {
260 red = read_uchar_IMG(_file);
261 grn = read_uchar_IMG(_file);
262 blu = read_uchar_IMG(_file);
264 PPM_ASSIGN(row_data[x], red, grn, blu);
275 PNMFileTypeIMG::Writer::
276 Writer(
PNMFileType *type, ostream *file,
bool owns_file) :
291 bool PNMFileTypeIMG::Writer::
292 supports_write_row()
const {
310 bool PNMFileTypeIMG::Writer::
312 if (img_header_type == IHT_long) {
313 write_ulong(_file, _x_size);
314 write_ulong(_file, _y_size);
315 }
else if (img_header_type == IHT_short) {
316 write_ushort_IMG(_file, _x_size);
317 write_ushort_IMG(_file, _y_size);
337 bool PNMFileTypeIMG::Writer::
338 write_row(
xel *row_data, xelval *) {
340 for (x = 0; x < _x_size; x++) {
341 write_uchar_IMG(_file, (
unsigned char)(255*PPM_GETR(row_data[x])/_maxval));
342 write_uchar_IMG(_file, (
unsigned char)(255*PPM_GETG(row_data[x])/_maxval));
343 write_uchar_IMG(_file, (
unsigned char)(255*PPM_GETB(row_data[x])/_maxval));
357 void PNMFileTypeIMG::
358 register_with_read_factory() {
360 register_factory(get_class_type(), make_PNMFileTypeIMG);
PNMFileType * get_type_by_handle(TypeHandle handle) const
Returns the PNMFileType instance stored in the registry for the given TypeHandle, e...
Base class for objects that can be written to and read from Bam files.
This is the base class of a family of classes that represent particular image file types that PNMImag...
static PNMFileTypeRegistry * get_global_ptr()
Returns a pointer to the global PNMFileTypeRegistry object.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
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...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
TypeHandle is the identifier used to differentiate C++ class types.