PGF Console  6.21.2
PNMPlugin.cpp File Reference
#include "CImage.h"
#include "FreeImage.h"
#include "Utilities.h"

Go to the source code of this file.

Functions

static int GetInt (FreeImageIO *io, fi_handle handle)
 
static WORD ReadWord (FreeImageIO *io, fi_handle handle)
 
static void WriteWord (FreeImageIO *io, fi_handle handle, const WORD value)
 
static const char *DLL_CALLCONV Format ()
 
static const char *DLL_CALLCONV Description ()
 
static const char *DLL_CALLCONV Extension ()
 
static const char *DLL_CALLCONV RegExpr ()
 
static const char *DLL_CALLCONV MimeType ()
 
static BOOL DLL_CALLCONV Validate (FreeImageIO *io, fi_handle handle)
 
static BOOL DLL_CALLCONV SupportsExportDepth (int depth)
 
static BOOL DLL_CALLCONV SupportsExportType (FREE_IMAGE_TYPE type)
 
static FIBITMAP *DLL_CALLCONV Load (FreeImageIO *io, fi_handle handle, int, int, void *)
 
static BOOL DLL_CALLCONV Save (FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int, int flags, void *)
 
void DLL_CALLCONV InitPNM (Plugin *plugin, int format_id)
 

Variables

int s_maxval = 0
 
static int s_format_id
 

Function Documentation

◆ Description()

static const char* DLL_CALLCONV Description ( )
static

Definition at line 140 of file PNMPlugin.cpp.

140  {
141  return "Portable Network Media";
142 }

◆ Extension()

static const char* DLL_CALLCONV Extension ( )
static

Definition at line 145 of file PNMPlugin.cpp.

145  {
146  return "pbm,pgm,ppm";
147 }

◆ Format()

static const char* DLL_CALLCONV Format ( )
static

Definition at line 135 of file PNMPlugin.cpp.

135  {
136  return "PNM";
137 }

◆ GetInt()

static int GetInt ( FreeImageIO *  io,
fi_handle  handle 
)
static

Get an integer value from the actual position pointed by handle

Definition at line 39 of file PNMPlugin.cpp.

39  {
40  static const char *PNM_ERROR_PARSING = "Parsing error";
41 
42  char c = 0;
43  BOOL firstchar;
44 
45  // skip forward to start of next number
46 
47  if(!io->read_proc(&c, 1, 1, handle)) throw PNM_ERROR_PARSING;
48 
49  while (1) {
50  // eat comments
51 
52  if (c == '#') {
53  // if we're at a comment, read to end of line
54 
55  firstchar = TRUE;
56 
57  while (1) {
58  if(!io->read_proc(&c, 1, 1, handle)) throw PNM_ERROR_PARSING;
59 
60  if (firstchar && c == ' ') {
61  // loop off 1 sp after #
62 
63  firstchar = FALSE;
64  } else if (c == '\n') {
65  break;
66  }
67  }
68  }
69 
70  if (c >= '0' && c <='9') {
71  // we've found what we were looking for
72 
73  break;
74  }
75 
76  if(!io->read_proc(&c, 1, 1, handle)) throw PNM_ERROR_PARSING;
77  }
78 
79  // we're at the start of a number, continue until we hit a non-number
80 
81  int i = 0;
82 
83  while (1) {
84  i = (i * 10) + (c - '0');
85 
86  if(!io->read_proc(&c, 1, 1, handle)) throw PNM_ERROR_PARSING;
87 
88  if (c < '0' || c > '9')
89  break;
90  }
91 
92  return i;
93 }

◆ InitPNM()

void DLL_CALLCONV InitPNM ( Plugin *  plugin,
int  format_id 
)

Definition at line 797 of file PNMPlugin.cpp.

797  {
798  s_format_id = format_id;
799 
800  plugin->format_proc = Format;
801  plugin->description_proc = Description;
802  plugin->extension_proc = Extension;
803  plugin->regexpr_proc = RegExpr;
804  plugin->open_proc = nullptr;
805  plugin->close_proc = nullptr;
806  plugin->pagecount_proc = nullptr;
807  plugin->pagecapability_proc = nullptr;
808  plugin->load_proc = Load;
809  plugin->save_proc = Save;
810  plugin->validate_proc = Validate;
811  plugin->mime_proc = MimeType;
812  plugin->supports_export_bpp_proc = SupportsExportDepth;
813  plugin->supports_export_type_proc = SupportsExportType;
814  plugin->supports_icc_profiles_proc = nullptr;
815 }
static const char *DLL_CALLCONV RegExpr()
Definition: PNMPlugin.cpp:150
static const char *DLL_CALLCONV MimeType()
Definition: PNMPlugin.cpp:155
static BOOL DLL_CALLCONV SupportsExportType(FREE_IMAGE_TYPE type)
Definition: PNMPlugin.cpp:202
static const char *DLL_CALLCONV Description()
Definition: PNMPlugin.cpp:140
static FIBITMAP *DLL_CALLCONV Load(FreeImageIO *io, fi_handle handle, int, int, void *)
Definition: PNMPlugin.cpp:213
static const char *DLL_CALLCONV Format()
Definition: PNMPlugin.cpp:135
static BOOL DLL_CALLCONV Validate(FreeImageIO *io, fi_handle handle)
Definition: PNMPlugin.cpp:160
static int s_format_id
Definition: PNMPlugin.cpp:128
static BOOL DLL_CALLCONV SupportsExportDepth(int depth)
Definition: PNMPlugin.cpp:193
static const char *DLL_CALLCONV Extension()
Definition: PNMPlugin.cpp:145
static BOOL DLL_CALLCONV Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int, int flags, void *)
Definition: PNMPlugin.cpp:509

◆ Load()

static FIBITMAP* DLL_CALLCONV Load ( FreeImageIO *  io,
fi_handle  handle,
int  ,
int  ,
void *   
)
static

Definition at line 213 of file PNMPlugin.cpp.

213  {
214  char id_one = 0, id_two = 0;
215  int x, y;
216  FIBITMAP *dib = nullptr;
217  RGBQUAD *pal; // pointer to dib palette
218  int i;
219 
220  if (!handle)
221  return nullptr;
222 
223  try {
224  FREE_IMAGE_TYPE image_type = FIT_BITMAP; // standard image: 1-, 8-, 24-bit
225 
226  // Read the first two bytes of the file to determine the file format
227  // "P1" = ascii bitmap, "P2" = ascii greymap, "P3" = ascii pixmap,
228  // "P4" = raw bitmap, "P5" = raw greymap, "P6" = raw pixmap
229 
230  io->read_proc(&id_one, 1, 1, handle);
231  io->read_proc(&id_two, 1, 1, handle);
232 
233  if ((id_one != 'P') || (id_two < '1') || (id_two > '6')) {
234  // signature error
235  throw "Invalid magic number";
236  }
237 
238  // Read the header information: width, height and the 'max' value if any
239 
240  int width = GetInt(io, handle);
241  int height = GetInt(io, handle);
242 
243  if((id_two == '2') || (id_two == '5') || (id_two == '3') || (id_two == '6')) {
244  s_maxval = GetInt(io, handle);
245  if((s_maxval <= 0) || (s_maxval > 65535)) {
246  FreeImage_OutputMessageProc(s_format_id, "Invalid max value : %d", s_maxval);
247  throw (const char*)nullptr;
248  }
249  } else {
250  s_maxval = 1;
251  }
252 
253  // Create a new DIB
254 
255  switch (id_two) {
256  case '1':
257  case '4':
258  // 1-bit
259  dib = FreeImage_Allocate(width, height, 1);
260  break;
261 
262  case '2':
263  case '5':
264  if(s_maxval > 255) {
265  // 16-bit greyscale
266  image_type = FIT_UINT16;
267  dib = FreeImage_AllocateT(image_type, width, height);
268  } else {
269  // 8-bit greyscale
270  dib = FreeImage_Allocate(width, height, 8);
271  }
272  break;
273 
274  case '3':
275  case '6':
276  if(s_maxval > 255) {
277  // 48-bit RGB
278  image_type = FIT_RGB16;
279  dib = FreeImage_AllocateT(image_type, width, height);
280  } else {
281  // 24-bit RGB
282  dib = FreeImage_Allocate(width, height, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
283  }
284  break;
285  }
286 
287  if (dib == nullptr)
288  throw "DIB allocation failed";
289 
290  // Read the image...
291 
292  switch(id_two) {
293  case '1':
294  case '4':
295  // write the palette data
296 
297  pal = FreeImage_GetPalette(dib);
298  pal[0].rgbRed = pal[0].rgbGreen = pal[0].rgbBlue = 0;
299  pal[1].rgbRed = pal[1].rgbGreen = pal[1].rgbBlue = 255;
300 
301  // write the bitmap data
302 
303  if (id_two == '1') { // ASCII bitmap
304  for (y = 0; y < height; y++) {
305  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
306 
307  for (x = 0; x < width; x++) {
308  if (GetInt(io, handle) == 0)
309  bits[x >> 3] |= (0x80 >> (x & 0x7));
310  else
311  bits[x >> 3] &= (0xFF7F >> (x & 0x7));
312  }
313  }
314  } else { // Raw bitmap
315  int line = CalculateLine(width, 1);
316 
317  for (y = 0; y < height; y++) {
318  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
319 
320  for (x = 0; x < line; x++) {
321  io->read_proc(&bits[x], 1, 1, handle);
322 
323  bits[x] = ~bits[x];
324  }
325  }
326  }
327 
328  return dib;
329 
330  case '2':
331  case '5':
332  if(image_type == FIT_BITMAP) {
333  // Build a greyscale palette
334 
335  pal = FreeImage_GetPalette(dib);
336 
337  for (i = 0; i < 256; i++) {
338  pal[i].rgbRed =
339  pal[i].rgbGreen =
340  pal[i].rgbBlue = (BYTE)i;
341  }
342 
343  // write the bitmap data
344 
345  if(id_two == '2') { // ASCII greymap
346  int level = 0;
347 
348  for (y = 0; y < height; y++) {
349  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
350 
351  for (x = 0; x < width; x++) {
352  level = GetInt(io, handle);
353  bits[x] = (BYTE)((255 * level) / s_maxval);
354  }
355  }
356  } else { // Raw greymap
357  BYTE level = 0;
358 
359  for (y = 0; y < height; y++) {
360  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
361 
362  for (x = 0; x < width; x++) {
363  io->read_proc(&level, 1, 1, handle);
364  bits[x] = (BYTE)((255 * (int)level) / s_maxval);
365  }
366  }
367  }
368  }
369  else if(image_type == FIT_UINT16) {
370  // write the bitmap data
371 
372  if(id_two == '2') { // ASCII greymap
373  int level = 0;
374 
375  for (y = 0; y < height; y++) {
376  WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);
377 
378  for (x = 0; x < width; x++) {
379  level = GetInt(io, handle);
380  bits[x] = (WORD)level;
381  }
382  }
383  } else { // Raw greymap
384  WORD level = 0;
385 
386  for (y = 0; y < height; y++) {
387  WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);
388 
389  for (x = 0; x < width; x++) {
390  level = ReadWord(io, handle);
391  bits[x] = level;
392  }
393  }
394  }
395  }
396 
397  return dib;
398 
399  case '3':
400  case '6':
401  if(image_type == FIT_BITMAP) {
402  // write the bitmap data
403 
404  if (id_two == '3') { // ASCII pixmap
405  int level = 0;
406 
407  for (y = 0; y < height; y++) {
408  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
409 
410  for (x = 0; x < width; x++) {
411  level = GetInt(io, handle);
412  bits[FI_RGBA_RED] = (BYTE)((255 * level) / s_maxval); // R
413  level = GetInt(io, handle);
414  bits[FI_RGBA_GREEN] = (BYTE)((255 * level) / s_maxval); // G
415  level = GetInt(io, handle);
416  bits[FI_RGBA_BLUE] = (BYTE)((255 * level) / s_maxval); // B
417 
418  bits += 3;
419  }
420  }
421  } else { // Raw pixmap
422  BYTE level = 0;
423 
424  for (y = 0; y < height; y++) {
425  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
426 
427  for (x = 0; x < width; x++) {
428  io->read_proc(&level, 1, 1, handle);
429  bits[FI_RGBA_RED] = (BYTE)((255 * (int)level) / s_maxval); // R
430 
431  io->read_proc(&level, 1, 1, handle);
432  bits[FI_RGBA_GREEN] = (BYTE)((255 * (int)level) / s_maxval); // G
433 
434  io->read_proc(&level, 1, 1, handle);
435  bits[FI_RGBA_BLUE] = (BYTE)((255 * (int)level) / s_maxval); // B
436 
437  bits += 3;
438  }
439  }
440  }
441  }
442  else if(image_type == FIT_RGB16) {
443  // write the bitmap data
444 
445  if (id_two == '3') { // ASCII pixmap
446  int level = 0;
447 
448  for (y = 0; y < height; y++) {
449  FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);
450 
451  for (x = 0; x < width; x++) {
452  level = GetInt(io, handle);
453  bits[x].red = (WORD)level; // R
454  level = GetInt(io, handle);
455  bits[x].green = (WORD)level; // G
456  level = GetInt(io, handle);
457  bits[x].blue = (WORD)level; // B
458  }
459  }
460  } else { // Raw pixmap
461  WORD level = 0;
462 
463  for (y = 0; y < height; y++) {
464  FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);
465 
466  for (x = 0; x < width; x++) {
467  level = ReadWord(io, handle);
468  bits[x].red = level; // R
469  level = ReadWord(io, handle);
470  bits[x].green = level; // G
471  level = ReadWord(io, handle);
472  bits[x].blue = level; // B
473  }
474  }
475  }
476  }
477 
478  return dib;
479  }
480 
481  } catch (const char *text) {
482  if(dib) FreeImage_Unload(dib);
483 
484  if(nullptr != text) {
485  switch(id_two) {
486  case '1':
487  case '4':
488  FreeImage_OutputMessageProc(s_format_id, text);
489  break;
490 
491  case '2':
492  case '5':
493  FreeImage_OutputMessageProc(s_format_id, text);
494  break;
495 
496  case '3':
497  case '6':
498  FreeImage_OutputMessageProc(s_format_id, text);
499  break;
500  }
501  }
502  return nullptr;
503  }
504 
505  return nullptr;
506 }
static int GetInt(FreeImageIO *io, fi_handle handle)
Definition: PNMPlugin.cpp:39
int s_maxval
Definition: PNMPlugin.cpp:122
int CalculateLine(int width, int bitdepth)
Definition: Utilities.h:160
static WORD ReadWord(FreeImageIO *io, fi_handle handle)
Definition: PNMPlugin.cpp:99
static int s_format_id
Definition: PNMPlugin.cpp:128

◆ MimeType()

static const char* DLL_CALLCONV MimeType ( )
static

Definition at line 155 of file PNMPlugin.cpp.

155  {
156  return "image/freeimage-pnm";
157 }

◆ ReadWord()

static WORD ReadWord ( FreeImageIO *  io,
fi_handle  handle 
)
inlinestatic

Read a WORD value taking into account the endianess issue

Definition at line 99 of file PNMPlugin.cpp.

99  {
100  WORD level = 0;
101  io->read_proc(&level, 2, 1, handle);
102 #ifndef FREEIMAGE_BIGENDIAN
103  SwapShort(&level); // PNM uses the big endian convention
104 #endif
105  return level;
106 }
void SwapShort(WORD *sp)
Definition: Utilities.h:203

◆ RegExpr()

static const char* DLL_CALLCONV RegExpr ( )
static

Definition at line 150 of file PNMPlugin.cpp.

150  {
151  return nullptr;
152 }

◆ Save()

static BOOL DLL_CALLCONV Save ( FreeImageIO *  io,
FIBITMAP *  dib,
fi_handle  handle,
int  ,
int  flags,
void *   
)
static

Definition at line 509 of file PNMPlugin.cpp.

509  {
510  // ----------------------------------------------------------
511  // PNM Saving
512  // ----------------------------------------------------------
513  //
514  // Output format :
515  //
516  // Bit depth flags file format
517  // ------------- -------------- -----------
518  // 1-bit / pixel PNM_SAVE_ASCII PBM (P1)
519  // 1-bit / pixel PNM_SAVE_RAW PBM (P4)
520  // 8-bit / pixel PNM_SAVE_ASCII PGM (P2)
521  // 8-bit / pixel PNM_SAVE_RAW PGM (P5)
522  // 24-bit / pixel PNM_SAVE_ASCII PPM (P3)
523  // 24-bit / pixel PNM_SAVE_RAW PPM (P6)
524  // ----------------------------------------------------------
525 
526  int x, y;
527 
528  char buffer[256]; // temporary buffer whose size should be enough for what we need
529 
530  if(!dib || !handle) return FALSE;
531 
532  FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
533 
534  int bpp = FreeImage_GetBPP(dib);
535  int width = FreeImage_GetWidth(dib);
536  int height = FreeImage_GetHeight(dib);
537 
538  // Find the appropriate magic number for this file type
539 
540  int magic = 0;
541  if (s_maxval < 255) s_maxval = 255;
542 
543  switch(image_type) {
544  case FIT_BITMAP:
545  switch (bpp) {
546  case 1 :
547  magic = 1; // PBM file (B & W)
548  break;
549  case 8 :
550  magic = 2; // PGM file (Greyscale)
551  break;
552 
553  case 24 :
554  magic = 3; // PPM file (RGB)
555  break;
556 
557  default:
558  return FALSE; // Invalid bit depth
559  }
560  break;
561 
562  case FIT_UINT16:
563  magic = 2; // PGM file (Greyscale)
564  break;
565 
566  case FIT_RGB16:
567  magic = 3; // PPM file (RGB)
568  break;
569 
570  default:
571  return FALSE;
572  }
573 
574 
575  if (flags == PNM_SAVE_RAW)
576  magic += 3;
577 
578  // Write the header info
579 
580  sprintf(buffer, "P%d\n%d %d\n", magic, width, height);
581  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
582 
583  if (bpp != 1) {
584  sprintf(buffer, "%d\n", s_maxval);
585  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
586  }
587 
588  // Write the image data
590 
591  if(image_type == FIT_BITMAP) {
592  switch(bpp) {
593  case 24 : // 24-bit RGB, 3 bytes per pixel
594  {
595  if (flags == PNM_SAVE_RAW) {
596  for (y = 0; y < height; y++) {
597  // write the scanline to disc
598  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
599 
600  for (x = 0; x < width; x++) {
601  io->write_proc(&bits[FI_RGBA_RED], 1, 1, handle); // R
602  io->write_proc(&bits[FI_RGBA_GREEN], 1, 1, handle); // G
603  io->write_proc(&bits[FI_RGBA_BLUE], 1, 1, handle); // B
604 
605  bits += 3;
606  }
607  }
608  } else {
609  int length = 0;
610 
611  for (y = 0; y < height; y++) {
612  // write the scanline to disc
613  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
614 
615  for (x = 0; x < width; x++) {
616  sprintf(buffer, "%3d %3d %3d ", bits[FI_RGBA_RED], bits[FI_RGBA_GREEN], bits[FI_RGBA_BLUE]);
617 
618  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
619 
620  length += 12;
621 
622  if(length > 58) {
623  // No line should be longer than 70 characters
624  sprintf(buffer, "\n");
625  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
626  length = 0;
627  }
628 
629  bits += 3;
630  }
631  }
632 
633  }
634  }
635  break;
636 
637  case 8: // 8-bit greyscale
638  {
639  if (flags == PNM_SAVE_RAW) {
640  for (y = 0; y < height; y++) {
641  // write the scanline to disc
642  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
643 
644  for (x = 0; x < width; x++) {
645  io->write_proc(&bits[x], 1, 1, handle);
646  }
647  }
648  } else {
649  int length = 0;
650 
651  for (y = 0; y < height; y++) {
652  // write the scanline to disc
653  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
654 
655  for (x = 0; x < width; x++) {
656  sprintf(buffer, "%3d ", bits[x]);
657 
658  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
659 
660  length += 4;
661 
662  if (length > 66) {
663  // No line should be longer than 70 characters
664  sprintf(buffer, "\n");
665  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
666  length = 0;
667  }
668  }
669  }
670  }
671  }
672  break;
673 
674  case 1: // 1-bit B & W
675  {
676  int color;
677 
678  if (flags == PNM_SAVE_RAW) {
679  for(y = 0; y < height; y++) {
680  // write the scanline to disc
681  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
682 
683  for(x = 0; x < (int)FreeImage_GetLine(dib); x++)
684  io->write_proc(&bits[x], 1, 1, handle);
685  }
686  } else {
687  int length = 0;
688 
689  for (y = 0; y < height; y++) {
690  // write the scanline to disc
691  BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
692 
693  for (x = 0; x < (int)FreeImage_GetLine(dib) * 8; x++) {
694  color = (bits[x>>3] & (0x80 >> (x & 0x07))) != 0;
695 
696  sprintf(buffer, "%c ", color ? '1':'0');
697 
698  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
699 
700  length += 2;
701 
702  if (length > 68) {
703  // No line should be longer than 70 characters
704  sprintf(buffer, "\n");
705  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
706  length = 0;
707  }
708  }
709  }
710  }
711  }
712 
713  break;
714  }
715  } // if(FIT_BITMAP)
716 
717  else if(image_type == FIT_UINT16) { // 16-bit greyscale
718  if (flags == PNM_SAVE_RAW) {
719  for (y = 0; y < height; y++) {
720  // write the scanline to disc
721  WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);
722 
723  for (x = 0; x < width; x++) {
724  WriteWord(io, handle, bits[x]);
725  }
726  }
727  } else {
728  int length = 0;
729 
730  for (y = 0; y < height; y++) {
731  // write the scanline to disc
732  WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);
733 
734  for (x = 0; x < width; x++) {
735  sprintf(buffer, "%5d ", bits[x]);
736 
737  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
738 
739  length += 6;
740 
741  if (length > 64) {
742  // No line should be longer than 70 characters
743  sprintf(buffer, "\n");
744  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
745  length = 0;
746  }
747  }
748  }
749  }
750  }
751 
752  else if(image_type == FIT_RGB16) { // 48-bit RGB
753  if (flags == PNM_SAVE_RAW) {
754  for (y = 0; y < height; y++) {
755  // write the scanline to disc
756  FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);
757 
758  for (x = 0; x < width; x++) {
759  WriteWord(io, handle, bits[x].red); // R
760  WriteWord(io, handle, bits[x].green); // G
761  WriteWord(io, handle, bits[x].blue); // B
762  }
763  }
764  } else {
765  int length = 0;
766 
767  for (y = 0; y < height; y++) {
768  // write the scanline to disc
769  FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);
770 
771  for (x = 0; x < width; x++) {
772  sprintf(buffer, "%5d %5d %5d ", bits[x].red, bits[x].green, bits[x].blue);
773 
774  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
775 
776  length += 18;
777 
778  if(length > 52) {
779  // No line should be longer than 70 characters
780  sprintf(buffer, "\n");
781  io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
782  length = 0;
783  }
784  }
785  }
786 
787  }
788  }
789 
790  return TRUE;
791 }
int s_maxval
Definition: PNMPlugin.cpp:122
static void WriteWord(FreeImageIO *io, fi_handle handle, const WORD value)
Definition: PNMPlugin.cpp:112

◆ SupportsExportDepth()

static BOOL DLL_CALLCONV SupportsExportDepth ( int  depth)
static

Definition at line 193 of file PNMPlugin.cpp.

193  {
194  return (
195  (depth == 1) ||
196  (depth == 8) ||
197  (depth == 24)
198  );
199 }

◆ SupportsExportType()

static BOOL DLL_CALLCONV SupportsExportType ( FREE_IMAGE_TYPE  type)
static

Definition at line 202 of file PNMPlugin.cpp.

202  {
203  return (
204  (type == FIT_BITMAP) ||
205  (type == FIT_UINT16) ||
206  (type == FIT_RGB16)
207  );
208 }

◆ Validate()

static BOOL DLL_CALLCONV Validate ( FreeImageIO *  io,
fi_handle  handle 
)
static

Definition at line 160 of file PNMPlugin.cpp.

160  {
161  BYTE pbm_id1[] = { 0x50, 0x31 };
162  BYTE pbm_id2[] = { 0x50, 0x34 };
163  BYTE pgm_id1[] = { 0x50, 0x32 };
164  BYTE pgm_id2[] = { 0x50, 0x35 };
165  BYTE ppm_id1[] = { 0x50, 0x33 };
166  BYTE ppm_id2[] = { 0x50, 0x36 };
167  BYTE signature[2] = { 0, 0 };
168 
169  io->read_proc(signature, 1, sizeof(pbm_id1), handle);
170 
171  if (memcmp(pbm_id1, signature, sizeof(pbm_id1)) == 0)
172  return TRUE;
173 
174  if (memcmp(pbm_id2, signature, sizeof(pbm_id2)) == 0)
175  return TRUE;
176 
177  if (memcmp(pgm_id1, signature, sizeof(pgm_id1)) == 0)
178  return TRUE;
179 
180  if (memcmp(pgm_id2, signature, sizeof(pgm_id2)) == 0)
181  return TRUE;
182 
183  if (memcmp(ppm_id1, signature, sizeof(ppm_id1)) == 0)
184  return TRUE;
185 
186  if (memcmp(ppm_id2, signature, sizeof(ppm_id2)) == 0)
187  return TRUE;
188 
189  return FALSE;
190 }

◆ WriteWord()

static void WriteWord ( FreeImageIO *  io,
fi_handle  handle,
const WORD  value 
)
inlinestatic

Write a WORD value taking into account the endianess issue

Definition at line 112 of file PNMPlugin.cpp.

112  {
113  WORD level = value;
114 #ifndef FREEIMAGE_BIGENDIAN
115  SwapShort(&level); // PNM uses the big endian convention
116 #endif
117  io->write_proc(&level, 2, 1, handle);
118 }
void SwapShort(WORD *sp)
Definition: Utilities.h:203

Variable Documentation

◆ s_format_id

int s_format_id
static

Definition at line 128 of file PNMPlugin.cpp.

◆ s_maxval

int s_maxval = 0

Definition at line 122 of file PNMPlugin.cpp.