26 #define INSANE_SIZE 20000
32 static const char *
const extensions_img[] = {
35 static const int num_extensions_img =
sizeof(extensions_img) /
sizeof(
const char *);
49 string PNMFileTypeIMG::
51 return "Raw binary RGB";
59 get_num_extensions()
const {
60 return num_extensions_img;
67 string PNMFileTypeIMG::
68 get_extension(
int n)
const {
69 nassertr(n >= 0 && n < num_extensions_img,
string());
70 return extensions_img[n];
77 string PNMFileTypeIMG::
78 get_suggested_extension()
const {
88 make_reader(istream *file,
bool owns_file,
const string &magic_number) {
90 return new Reader(
this, file, owns_file, magic_number);
99 make_writer(ostream *file,
bool owns_file) {
101 return new Writer(
this, file, owns_file);
106 read_ulong(istream *file) {
108 return pm_readbiglong(file, (
long *)&x)==0 ? x : 0;
111 inline unsigned short
112 read_ushort_IMG(istream *file) {
114 return pm_readbigshort(file, (
short *)&x)==0 ? x : 0;
118 read_uchar_IMG(istream *file) {
121 return (x!=EOF) ? (
unsigned char)x : 0;
125 write_ulong(ostream *file,
unsigned long x) {
126 pm_writebiglong(file, (
long)x);
130 write_ushort_IMG(ostream *file,
unsigned long x) {
131 pm_writebigshort(file, (
short)(
long)x);
135 write_uchar_IMG(ostream *file,
unsigned char x) {
142 PNMFileTypeIMG::Reader::
143 Reader(
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";
221 bool PNMFileTypeIMG::Reader::
222 supports_read_row()
const {
232 bool PNMFileTypeIMG::Reader::
233 read_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);
250 PNMFileTypeIMG::Writer::
251 Writer(
PNMFileType *type, ostream *file,
bool owns_file) :
262 bool PNMFileTypeIMG::Writer::
263 supports_write_row()
const {
277 bool 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);
300 bool PNMFileTypeIMG::Writer::
301 write_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));
317 void PNMFileTypeIMG::
318 register_with_read_factory() {
320 register_factory(get_class_type(), make_PNMFileTypeIMG);