summaryrefslogtreecommitdiff
path: root/plugins/AdvaImg/src/FreeImage/PluginPNM.cpp
blob: 3b4d0de5549902c6c5337b15a79613aca1300e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
// ==========================================================
// PNM (PPM, PGM, PBM) Loader and Writer
//
// Design and implementation by
// - Floris van den Berg (flvdberg@wxs.nl)
// - Hervé Drolon (drolon@infonie.fr)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================

#include "FreeImage.h"
#include "Utilities.h"

// ==========================================================
// Internal functions
// ==========================================================

/**
Get an integer value from the actual position pointed by handle
*/
static int
GetInt(FreeImageIO *io, fi_handle handle) {
    char c = 0;
	BOOL bFirstChar;

    // skip forward to start of next number

	if(!io->read_proc(&c, 1, 1, handle)) {
		throw FI_MSG_ERROR_PARSING;
	}

    while (1) {
        // eat comments

        if (c == '#') {
			// if we're at a comment, read to end of line

            bFirstChar = TRUE;

            while (1) {
				if(!io->read_proc(&c, 1, 1, handle)) {
					throw FI_MSG_ERROR_PARSING;
				}

				if (bFirstChar && c == ' ') {
					// loop off 1 sp after #
					bFirstChar = FALSE;
				} else if (c == '\n') {
					break;
				}
			}
		}

        if (c >= '0' && c <='9') {
			// we've found what we were looking for
            break;
		}

		if(!io->read_proc(&c, 1, 1, handle)) {
			throw FI_MSG_ERROR_PARSING;
		}
    }

    // we're at the start of a number, continue until we hit a non-number

    int i = 0;

    while (1) {
        i = (i * 10) + (c - '0');

		if(!io->read_proc(&c, 1, 1, handle)) {
			throw FI_MSG_ERROR_PARSING;
		}

		if (c < '0' || c > '9') {
			break;
		}
    }

    return i;
}

/**
Read a WORD value taking into account the endianess issue
*/
static inline WORD 
ReadWord(FreeImageIO *io, fi_handle handle) {
	WORD level = 0;
	io->read_proc(&level, 2, 1, handle); 
#ifndef FREEIMAGE_BIGENDIAN
	SwapShort(&level);	// PNM uses the big endian convention
#endif
	return level;
}

/**
Write a WORD value taking into account the endianess issue
*/
static inline void 
WriteWord(FreeImageIO *io, fi_handle handle, const WORD value) {
	WORD level = value;
#ifndef FREEIMAGE_BIGENDIAN
	SwapShort(&level);	// PNM uses the big endian convention
#endif
	io->write_proc(&level, 2, 1, handle);
}


// ==========================================================
// Plugin Interface
// ==========================================================

static int s_format_id;

// ==========================================================
// Plugin Implementation
// ==========================================================

static const char * DLL_CALLCONV
Format() {
	return "PNM";
}

static const char * DLL_CALLCONV
Description() {
	return "Portable Network Media";
}

static const char * DLL_CALLCONV
Extension() {
	return "pbm,pgm,ppm";
}

static const char * DLL_CALLCONV
RegExpr() {
	return NULL;
}

static const char * DLL_CALLCONV
MimeType() {
	return "image/freeimage-pnm";
}

static BOOL DLL_CALLCONV
Validate(FreeImageIO *io, fi_handle handle) {
	BYTE pbm_id1[] = { 0x50, 0x31 };
	BYTE pbm_id2[] = { 0x50, 0x34 };
	BYTE pgm_id1[] = { 0x50, 0x32 };
	BYTE pgm_id2[] = { 0x50, 0x35 };
	BYTE ppm_id1[] = { 0x50, 0x33 };
	BYTE ppm_id2[] = { 0x50, 0x36 };
	BYTE signature[2] = { 0, 0 };

	io->read_proc(signature, 1, sizeof(pbm_id1), handle);

	if (memcmp(pbm_id1, signature, sizeof(pbm_id1)) == 0)
		return TRUE;

	if (memcmp(pbm_id2, signature, sizeof(pbm_id2)) == 0)
		return TRUE;

	if (memcmp(pgm_id1, signature, sizeof(pgm_id1)) == 0)
		return TRUE;

	if (memcmp(pgm_id2, signature, sizeof(pgm_id2)) == 0)
		return TRUE;

	if (memcmp(ppm_id1, signature, sizeof(ppm_id1)) == 0)
		return TRUE;

	if (memcmp(ppm_id2, signature, sizeof(ppm_id2)) == 0)
		return TRUE;

	return FALSE;
}

static BOOL DLL_CALLCONV
SupportsExportDepth(int depth) {
	return (
			(depth == 1) ||
			(depth == 8) ||
			(depth == 24)
		);
}

static BOOL DLL_CALLCONV 
SupportsExportType(FREE_IMAGE_TYPE type) {
	return (
		(type == FIT_BITMAP)  ||
		(type == FIT_UINT16)  ||
		(type == FIT_RGB16)
	);
}

static BOOL DLL_CALLCONV
SupportsNoPixels() {
	return TRUE;
}

// ----------------------------------------------------------

static FIBITMAP * DLL_CALLCONV
Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
	char id_one = 0, id_two = 0;
	int x, y;
	FIBITMAP *dib = NULL;
	RGBQUAD *pal;	// pointer to dib palette
	int i;

	if (!handle) {
		return NULL;
	}

	BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;

	try {
		FREE_IMAGE_TYPE image_type = FIT_BITMAP;	// standard image: 1-, 8-, 24-bit

		// Read the first two bytes of the file to determine the file format
		// "P1" = ascii bitmap, "P2" = ascii greymap, "P3" = ascii pixmap,
		// "P4" = raw bitmap, "P5" = raw greymap, "P6" = raw pixmap

		io->read_proc(&id_one, 1, 1, handle);
		io->read_proc(&id_two, 1, 1, handle);

		if ((id_one != 'P') || (id_two < '1') || (id_two > '6')) {			
			// signature error
			throw FI_MSG_ERROR_MAGIC_NUMBER;
		}

		// Read the header information: width, height and the 'max' value if any

		int width  = GetInt(io, handle);
		int height = GetInt(io, handle);
		int maxval = 1;

		if((id_two == '2') || (id_two == '5') || (id_two == '3') || (id_two == '6')) {
			maxval = GetInt(io, handle);
			if((maxval <= 0) || (maxval > 65535)) {
				FreeImage_OutputMessageProc(s_format_id, "Invalid max value : %d", maxval);
				throw (const char*)NULL;
			}
		}

		// Create a new DIB

		switch (id_two) {
			case '1':
			case '4':
				// 1-bit
				dib = FreeImage_AllocateHeader(header_only, width, height, 1);
				break;

			case '2':
			case '5':
				if(maxval > 255) {
					// 16-bit greyscale
					image_type = FIT_UINT16;
					dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height);
				} else {
					// 8-bit greyscale
					dib = FreeImage_AllocateHeader(header_only, width, height, 8);
				}
				break;

			case '3':
			case '6':
				if(maxval > 255) {
					// 48-bit RGB
					image_type = FIT_RGB16;
					dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height);
				} else {
					// 24-bit RGB
					dib = FreeImage_AllocateHeader(header_only, width, height, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
				}
				break;
		}

		if (dib == NULL) {
			throw FI_MSG_ERROR_DIB_MEMORY;
		}

		// Build a greyscale palette if needed

		if(image_type == FIT_BITMAP) {
			switch(id_two)  {
				case '1':
				case '4':
					pal = FreeImage_GetPalette(dib);
					pal[0].rgbRed = pal[0].rgbGreen = pal[0].rgbBlue = 0;
					pal[1].rgbRed = pal[1].rgbGreen = pal[1].rgbBlue = 255;
					break;

				case '2':
				case '5':
					pal = FreeImage_GetPalette(dib);
					for (i = 0; i < 256; i++) {
						pal[i].rgbRed	=
						pal[i].rgbGreen =
						pal[i].rgbBlue	= (BYTE)i;
					}
					break;

				default:
					break;
			}
		}

		if(header_only) {
			// header only mode
			return dib;
		}

		// Read the image...

		switch(id_two)  {
			case '1':
			case '4':
				// write the bitmap data

				if (id_two == '1') {	// ASCII bitmap
					for (y = 0; y < height; y++) {		
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < width; x++) {
							if (GetInt(io, handle) == 0)
								bits[x >> 3] |= (0x80 >> (x & 0x7));
							else
								bits[x >> 3] &= (0xFF7F >> (x & 0x7));
						}
					}
				}  else {		// Raw bitmap
					int line = CalculateLine(width, 1);

					for (y = 0; y < height; y++) {	
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < line; x++) {
							io->read_proc(&bits[x], 1, 1, handle);

							bits[x] = ~bits[x];
						}
					}
				}

				return dib;

			case '2':
			case '5':
				if(image_type == FIT_BITMAP) {
					// write the bitmap data

					if(id_two == '2') {		// ASCII greymap
						int level = 0;

						for (y = 0; y < height; y++) {	
							BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

							for (x = 0; x < width; x++) {
								level = GetInt(io, handle);
								bits[x] = (BYTE)((255 * level) / maxval);
							}
						}
					} else {		// Raw greymap
						BYTE level = 0;

						for (y = 0; y < height; y++) {		
							BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

							for (x = 0; x < width; x++) {
								io->read_proc(&level, 1, 1, handle);
								bits[x] = (BYTE)((255 * (int)level) / maxval);
							}
						}
					}
				}
				else if(image_type == FIT_UINT16) {
					// write the bitmap data

					if(id_two == '2') {		// ASCII greymap
						int level = 0;

						for (y = 0; y < height; y++) {	
							WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);

							for (x = 0; x < width; x++) {
								level = GetInt(io, handle);
								bits[x] = (WORD)((65535 * (double)level) / maxval);
							}
						}
					} else {		// Raw greymap
						WORD level = 0;

						for (y = 0; y < height; y++) {		
							WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);

							for (x = 0; x < width; x++) {
								level = ReadWord(io, handle);
								bits[x] = (WORD)((65535 * (double)level) / maxval);
							}
						}
					}
				}

				return dib;

			case '3':
			case '6':
				if(image_type == FIT_BITMAP) {
					// write the bitmap data

					if (id_two == '3') {		// ASCII pixmap
						int level = 0;

						for (y = 0; y < height; y++) {	
							BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

							for (x = 0; x < width; x++) {
								level = GetInt(io, handle);
								bits[FI_RGBA_RED] = (BYTE)((255 * level) / maxval);		// R
								level = GetInt(io, handle);
								bits[FI_RGBA_GREEN] = (BYTE)((255 * level) / maxval);	// G
								level = GetInt(io, handle);
								bits[FI_RGBA_BLUE] = (BYTE)((255 * level) / maxval);	// B

								bits += 3;
							}
						}
					}  else {			// Raw pixmap
						BYTE level = 0;

						for (y = 0; y < height; y++) {	
							BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

							for (x = 0; x < width; x++) {
								io->read_proc(&level, 1, 1, handle); 
								bits[FI_RGBA_RED] = (BYTE)((255 * (int)level) / maxval);	// R

								io->read_proc(&level, 1, 1, handle);
								bits[FI_RGBA_GREEN] = (BYTE)((255 * (int)level) / maxval);	// G

								io->read_proc(&level, 1, 1, handle);
								bits[FI_RGBA_BLUE] = (BYTE)((255 * (int)level) / maxval);	// B

								bits += 3;
							}
						}
					}
				}
				else if(image_type == FIT_RGB16) {
					// write the bitmap data

					if (id_two == '3') {		// ASCII pixmap
						int level = 0;

						for (y = 0; y < height; y++) {	
							FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);

							for (x = 0; x < width; x++) {
								level = GetInt(io, handle);
								bits[x].red = (WORD)((65535 * (double)level) / maxval);		// R
								level = GetInt(io, handle);
								bits[x].green = (WORD)((65535 * (double)level) / maxval);	// G
								level = GetInt(io, handle);
								bits[x].blue = (WORD)((65535 * (double)level) / maxval);	// B
							}
						}
					}  else {			// Raw pixmap
						WORD level = 0;

						for (y = 0; y < height; y++) {	
							FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);

							for (x = 0; x < width; x++) {
								level = ReadWord(io, handle);
								bits[x].red = (WORD)((65535 * (double)level) / maxval);		// R
								level = ReadWord(io, handle);
								bits[x].green = (WORD)((65535 * (double)level) / maxval);	// G
								level = ReadWord(io, handle);
								bits[x].blue = (WORD)((65535 * (double)level) / maxval);	// B
							}
						}
					}
				}

				return dib;
		}

	} catch (const char *text)  {
		if(dib) FreeImage_Unload(dib);

		if(NULL != text) {
			switch(id_two)  {
				case '1':
				case '4':
					FreeImage_OutputMessageProc(s_format_id, text);
					break;

				case '2':
				case '5':
					FreeImage_OutputMessageProc(s_format_id, text);
					break;

				case '3':
				case '6':
					FreeImage_OutputMessageProc(s_format_id, text);
					break;
			}
		}
	}
		
	return NULL;
}

static BOOL DLL_CALLCONV
Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) {
	// ----------------------------------------------------------
	//   PNM Saving
	// ----------------------------------------------------------
	//
	// Output format :
	//
	// Bit depth		flags			file format
	// -------------    --------------  -----------
	// 1-bit / pixel	PNM_SAVE_ASCII	PBM (P1)
	// 1-bit / pixel	PNM_SAVE_RAW	PBM (P4)
	// 8-bit / pixel	PNM_SAVE_ASCII	PGM (P2)
	// 8-bit / pixel	PNM_SAVE_RAW	PGM (P5)
	// 24-bit / pixel	PNM_SAVE_ASCII	PPM (P3)
	// 24-bit / pixel	PNM_SAVE_RAW	PPM (P6)
	// ----------------------------------------------------------

	int x, y;

	char buffer[256];	// temporary buffer whose size should be enough for what we need

	if(!dib || !handle) return FALSE;
	
	FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);

	int bpp		= FreeImage_GetBPP(dib);
	int width	= FreeImage_GetWidth(dib);
	int height	= FreeImage_GetHeight(dib);

	// Find the appropriate magic number for this file type

	int magic = 0;
	int maxval = 255;

	switch(image_type) {
		case FIT_BITMAP:
			switch (bpp) {
				case 1 :
					magic = 1;	// PBM file (B & W)
					break;
				case 8 : 			
					magic = 2;	// PGM file	(Greyscale)
					break;

				case 24 :
					magic = 3;	// PPM file (RGB)
					break;

				default:
					return FALSE;	// Invalid bit depth
			}
			break;
		
		case FIT_UINT16:
			magic = 2;	// PGM file	(Greyscale)
			maxval = 65535;
			break;

		case FIT_RGB16:
			magic = 3;	// PPM file (RGB)
			maxval = 65535;
			break;

		default:
			return FALSE;
	}


	if (flags == PNM_SAVE_RAW)
		magic += 3;

	// Write the header info

	sprintf(buffer, "P%d\n%d %d\n", magic, width, height);
	io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

	if (bpp != 1) {
		sprintf(buffer, "%d\n", maxval);
		io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
	}

	// Write the image data
	///////////////////////

	if(image_type == FIT_BITMAP) {
		switch(bpp)  {
			case 24 :            // 24-bit RGB, 3 bytes per pixel
			{
				if (flags == PNM_SAVE_RAW)  {
					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < width; x++) {
							io->write_proc(&bits[FI_RGBA_RED], 1, 1, handle);	// R
							io->write_proc(&bits[FI_RGBA_GREEN], 1, 1, handle);	// G
							io->write_proc(&bits[FI_RGBA_BLUE], 1, 1, handle);	// B

							bits += 3;
						}
					}
				} else {
					int length = 0;

					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
						
						for (x = 0; x < width; x++) {
							sprintf(buffer, "%3d %3d %3d ", bits[FI_RGBA_RED], bits[FI_RGBA_GREEN], bits[FI_RGBA_BLUE]);

							io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

							length += 12;

							if(length > 58) {
								// No line should be longer than 70 characters
								sprintf(buffer, "\n");
								io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
								length = 0;
							}

							bits += 3;
						}					
					}

				}
			}
			break;

			case 8:		// 8-bit greyscale
			{
				if (flags == PNM_SAVE_RAW)  {
					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < width; x++) {
							io->write_proc(&bits[x], 1, 1, handle);
						}
					}
				} else {
					int length = 0;

					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < width; x++) {
							sprintf(buffer, "%3d ", bits[x]);

							io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

							length += 4;

							if (length > 66) {
								// No line should be longer than 70 characters
								sprintf(buffer, "\n");
								io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
								length = 0;
							}
						}
					}
				}
			}
			break;

			case 1:		// 1-bit B & W
			{
				int color;

				if (flags == PNM_SAVE_RAW)  {
					for(y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for(x = 0; x < (int)FreeImage_GetLine(dib); x++)
							io->write_proc(&bits[x], 1, 1, handle);
					}
				} else  {
					int length = 0;

					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < (int)FreeImage_GetLine(dib) * 8; x++)	{
							color = (bits[x>>3] & (0x80 >> (x & 0x07))) != 0;

							sprintf(buffer, "%c ", color ? '1':'0');

							io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

							length += 2;

							if (length > 68) {
								// No line should be longer than 70 characters
								sprintf(buffer, "\n");
								io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
								length = 0;
							}
						}
					}
				}
			}
			
			break;
		}
	} // if(FIT_BITMAP)

	else if(image_type == FIT_UINT16) {		// 16-bit greyscale
		if (flags == PNM_SAVE_RAW)  {
			for (y = 0; y < height; y++) {
				// write the scanline to disc
				WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);

				for (x = 0; x < width; x++) {
					WriteWord(io, handle, bits[x]);
				}
			}
		} else {
			int length = 0;

			for (y = 0; y < height; y++) {
				// write the scanline to disc
				WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);

				for (x = 0; x < width; x++) {
					sprintf(buffer, "%5d ", bits[x]);

					io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

					length += 6;

					if (length > 64) {
						// No line should be longer than 70 characters
						sprintf(buffer, "\n");
						io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
						length = 0;
					}
				}
			}
		}
	}

	else if(image_type == FIT_RGB16) {		// 48-bit RGB
		if (flags == PNM_SAVE_RAW)  {
			for (y = 0; y < height; y++) {
				// write the scanline to disc
				FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);

				for (x = 0; x < width; x++) {
					WriteWord(io, handle, bits[x].red);		// R
					WriteWord(io, handle, bits[x].green);	// G
					WriteWord(io, handle, bits[x].blue);	// B
				}
			}
		} else {
			int length = 0;

			for (y = 0; y < height; y++) {
				// write the scanline to disc
				FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);
				
				for (x = 0; x < width; x++) {
					sprintf(buffer, "%5d %5d %5d ", bits[x].red, bits[x].green, bits[x].blue);

					io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

					length += 18;

					if(length > 52) {
						// No line should be longer than 70 characters
						sprintf(buffer, "\n");
						io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
						length = 0;
					}
				}					
			}

		}
	}

	return TRUE;
}

// ==========================================================
//   Init
// ==========================================================

void DLL_CALLCONV
InitPNM(Plugin *plugin, int format_id) {
	s_format_id = format_id;

	plugin->format_proc = Format;
	plugin->description_proc = Description;
	plugin->extension_proc = Extension;
	plugin->regexpr_proc = RegExpr;
	plugin->open_proc = NULL;
	plugin->close_proc = NULL;
	plugin->pagecount_proc = NULL;
	plugin->pagecapability_proc = NULL;
	plugin->load_proc = Load;
	plugin->save_proc = Save;
	plugin->validate_proc = Validate;
	plugin->mime_proc = MimeType;
	plugin->supports_export_bpp_proc = SupportsExportDepth;
	plugin->supports_export_type_proc = SupportsExportType;
	plugin->supports_icc_profiles_proc = NULL;
	plugin->supports_no_pixels_proc = SupportsNoPixels;
}