File: ppm.cpp

package info (click to toggle)
povray 1%3A3.6.1-12
  • links: PTS
  • area: non-free
  • in suites: lenny, squeeze
  • size: 31,084 kB
  • ctags: 20,310
  • sloc: ansic: 110,032; cpp: 86,573; sh: 13,595; pascal: 5,942; asm: 2,994; makefile: 1,753; ada: 1,637
file content (681 lines) | stat: -rw-r--r-- 18,442 bytes parent folder | download | duplicates (2)
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
/****************************************************************************
 *               ppm.cpp
 *
 * This module contains the code to read and write the PPM file format.
 *
 * from Persistence of Vision(tm) Ray Tracer version 3.6.
 * Copyright 1991-2003 Persistence of Vision Team
 * Copyright 2003-2004 Persistence of Vision Raytracer Pty. Ltd.
 *---------------------------------------------------------------------------
 * NOTICE: This source code file is provided so that users may experiment
 * with enhancements to POV-Ray and to port the software to platforms other
 * than those supported by the POV-Ray developers. There are strict rules
 * regarding how you are permitted to use this file. These rules are contained
 * in the distribution and derivative versions licenses which should have been
 * provided with this file.
 *
 * These licences may be found online, linked from the end-user license
 * agreement that is located at http://www.povray.org/povlegal.html
 *---------------------------------------------------------------------------
 * This program is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 *---------------------------------------------------------------------------
 * $File: //depot/povray/3.6-release/source/ppm.cpp $
 * $Revision: #3 $
 * $Change: 3032 $
 * $DateTime: 2004/08/02 18:43:41 $
 * $Author: chrisc $
 * $Log$
 *****************************************************************************/

/****************************************************************************
*
*  PPM format according to NetPBM specs (http://netpbm.sourceforge.net/doc/):
*
*  This module implements read support for PPM image maps and
*  write support for PPM output.
*
*  For reading both ASCII and binary files are supported ('P3' and 'P6').
*
*  For writing we use binary files. OutputQuality > 8 leads to 16 bit files.
*  Special handling of HF_GRAY_16 -> 16 bit PGM files ('P5')
*  All formats supported for writing can now also be used in continued trace.
*
*****************************************************************************/

#include "frame.h"
#include "povray.h"
#include "optout.h"
#include "pgm.h"
#include "ppm.h"
#include "pov_util.h"
#include "povmsend.h"
#include "colour.h"

#include <ctype.h>

BEGIN_POV_NAMESPACE

/*****************************************************************************
* Local preprocessor defines
******************************************************************************/

/*****************************************************************************
* Local typedefs
******************************************************************************/

/*****************************************************************************
* Local variables
******************************************************************************/

/*****************************************************************************
* Static functions
******************************************************************************/

/*****************************************************************************
*
* FUNCTION
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* DESCRIPTION
*
* CHANGES
*
******************************************************************************/

PPM_Image::PPM_Image(char *name, int w, int h, int m, int l)
{
  int  file_type;
  unsigned char header[2];
  char line[1024];
  char *ptr;
  int depth;

  mode = m;
  filename = name;
  in_file = NULL;
  out_file = NULL;
  width = w ;
  height = h ;
  line_number = l;

  // HF_GRAY_16 leads to 16 bit grayscale (PGM) output 
  if (opts.Options & HF_GRAY_16) file_type = POV_File_Image_PGM;
  else file_type = POV_File_Image_PPM;

  switch (mode)
  {
    case READ_MODE:

      // We can't resume from stdout. 
      if (opts.Options & TO_STDOUT || (in_file = New_Checked_IStream(name, file_type)) == NULL)
      {
        return;
      }

      // --- Read Header --- 
      if (!in_file->read((char *)header, 2)) return;

      if(header[0] != 'P') return;

      if((header[1] != '5') && (header[1] != '6')) return;

      do
      {
        in_file->getline (line, 1024);
        line[1023] = '\0';
        if ((ptr = strchr(line, '#')) != NULL) *ptr = '\0';  // remove comment 
      }
      while (line[0]=='\0');  // read until line without comment from beginning 

      // --- First: two numbers: with and height --- 
      if (sscanf(line,"%d %d",&width, &height) != 2) return;

      do
      {
        in_file->getline (line, 1024);
        line[1023] = '\0';
        if ((ptr = strchr(line, '#')) != NULL) *ptr = '\0';  // remove comment 
      }
      while (line[0]=='\0');  // read until line without comment from beginning 

      // --- Second: one number: color depth --- 
      if (sscanf(line,"%d",&depth) != 1) return;

      if ((depth > 65535) || (depth < 1)) return;

      if (w != width || h != height)
        Error ("PPM file dimensions do not match render resolution.");

      Send_Progress("Resuming interrupted trace", PROGRESS_RESUMING_INTERRUPTED_TRACE);

      break;

    case WRITE_MODE:

      if (opts.Options & TO_STDOUT)
      {
        out_file = New_OStream("stdout", file_type, false);
      }
      else if ((out_file = New_Checked_OStream(name, file_type, false)) == NULL)
      {
        return;
      }

      // HF_GRAY_16 leads to 16 bit grayscale (PGM) output 
      if (opts.Options & HF_GRAY_16)
      {
        out_file->printf("P5\n%d %d\n65535\n", w, h);
      }
      else
      {
        out_file->printf("P6\n%d %d\n%d\n", w, h, (1 << opts.OutputQuality) - 1);
      }

      width = w;
      height = h;

      break;

    case APPEND_MODE:

      if (opts.Options & TO_STDOUT)
      {
        out_file = New_OStream("stdout", file_type, true);
      }
      else if ((out_file = New_Checked_OStream(name, file_type, true)) == NULL)
      {
        return;
      }

      break;
  }

  valid = true;
}

/*****************************************************************************
*
* FUNCTION
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* DESCRIPTION
*
* CHANGES
*
*    Identical to targa version
*
******************************************************************************/

PPM_Image::~PPM_Image()
{
  // Close the input file (if open) 
  if(in_file != NULL)
    delete in_file;

  // Close the output file (if open) 
  if(out_file != NULL)
  {
    out_file->flush();
    delete out_file;
  }

  in_file = NULL;
  out_file = NULL;
}

/*****************************************************************************
*
* FUNCTION
*
*  PPM_Image::Write_Line
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
*    Christoph Hormann
*
* DESCRIPTION
*
*    Writes an image line to PPM image file
*
* CHANGES
*
*    August 2003 - New implementation based on targa/png code
*
******************************************************************************/

void PPM_Image::Write_Line(COLOUR *line_data)
{
  if(valid == false)
    Error("Cannot access output image file.");

  register int x;
  unsigned int rval, gval, bval, gray;
  unsigned int mask;
  COLC fac;

  for (x = 0; x < width; x++)
  {
    // HF_GRAY_16 leads to 16 bit grayscale (PGM) output 
    if (opts.Options & HF_GRAY_16)
    {
      gray = (int)floor((GREY_SCALE3(line_data[x][pRED],line_data[x][pGREEN],line_data[x][pBLUE])) * 65535.0);

      out_file->Write_Byte((gray >> 8) & 0xFF);
      if (!out_file->Write_Byte(gray & 0xFF))
        Error("Cannot write PPM output data to %s.",filename);
    }
    else
    // otherwise 3*OutputQuality bit pixel coloring written to 8 or 16 bit file
    {
      mask = (1 << opts.OutputQuality) - 1 ;
      fac = (COLC) (mask) ;

      rval = (unsigned int)floor(line_data[x][pRED] * fac) & mask;
      gval = (unsigned int)floor(line_data[x][pGREEN] * fac) & mask;
      bval = (unsigned int)floor(line_data[x][pBLUE] * fac) & mask;

      if (opts.OutputQuality>8)  // 16 bit per value 
      {
        out_file->Write_Byte(rval >> 8) ;
        out_file->Write_Byte(rval & 0xFF) ;
        out_file->Write_Byte(gval >> 8) ;
        out_file->Write_Byte(gval & 0xFF) ;
        out_file->Write_Byte(bval >> 8) ;
        if (!out_file->Write_Byte(bval & 0xFF))
        {
          Error("Error writing PPM output data to %s.",filename);
        }
      }
      else                       // 8 bit per value 
      {
        out_file->Write_Byte(rval & 0xFF) ;
        out_file->Write_Byte(gval & 0xFF) ;
        if (!out_file->Write_Byte(bval & 0xFF))
        {
          Error("Cannot write PPM output data to %s.",filename);
        }
      }
    }
  }

  line_number++;

  out_file->flush();
}

/*****************************************************************************
*
* FUNCTION
*
*  PPM_Image::Read_Line
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
*    Christoph Hormann
*
* DESCRIPTION
*
*    Reads a PPM image line for continued trace
*
* CHANGES
*
*    August 2003 - New implementation based on targa/png code
*
******************************************************************************/

int PPM_Image::Read_Line(COLOUR *line_data)
{
  if(valid == false)
    Error("Cannot access output image file.");

  int data, x;
  int data_hi, data_lo;


  if (in_file->eof()) return 0;

  if (opts.Options & HF_GRAY_16)
  {
    for (x = 0; x < width; x++)
    {
      if ((data_hi = in_file->Read_Byte ()) == EOF)
      {
        if (x == 0)
        {
          return 0;
        }
        else
        {
          return -1;
        }
      }
      if ((data_lo = in_file->Read_Byte ()) == EOF) return -1;

      line_data[x][pRED]   = ((COLC)(256*data_hi + data_lo))/65535.0;
      line_data[x][pGREEN] = line_data[x][pRED];
      line_data[x][pBLUE]  = line_data[x][pRED];
    }
  }
  /* depending on the output Quality settings we expect
   * an 8 or 16 bit file.  So using continued trace requires
   * correct OutputQuality settings
   */
  else if (opts.OutputQuality>8)
  {
    COLC fac = (COLC)((1 << opts.OutputQuality) - 1);

    for (x = 0; x < width; x++)
    {
      /* Read the first data byte.  If EOF is reached on the first
       * character read, then this line hasn't been rendered yet.
       * Return 0.  If an EOF occurs somewhere within the line, this
       * is an error - return -1.
       */
      if ((data_hi = in_file->Read_Byte ()) == EOF)
      {
        if (x == 0)
        {
          return 0;
        }
        else
        {
          return -1;
        }
      }
      if ((data_lo = in_file->Read_Byte ()) == EOF) return -1;

      line_data[x][pRED] = ((COLC)(256*data_hi + data_lo))/fac;

      if ((data_hi = in_file->Read_Byte ()) == EOF) return -1;
      if ((data_lo = in_file->Read_Byte ()) == EOF) return -1;

      line_data[x][pGREEN] = ((COLC)(256*data_hi + data_lo))/fac;

      if ((data_hi = in_file->Read_Byte ()) == EOF) return -1;
      if ((data_lo = in_file->Read_Byte ()) == EOF) return -1;

      line_data[x][pBLUE] = ((COLC)(256*data_hi + data_lo))/fac;
    }
  }
  else
  {
    for (x = 0; x < width; x++)
    {
      /* Read the first data byte.  If EOF is reached on the first
       * character read, then this line hasn't been rendered yet.
       * Return 0.  If an EOF occurs somewhere within the line, this
       * is an error - return -1.
       */
      if ((data = in_file->Read_Byte ()) == EOF)
      {
        if (x == 0)
        {
          return 0;
        }
        else
        {
          return -1;
        }
      }

      line_data[x][pRED]  = (DBL) data / 255.0;

      // Read the GREEN data byte. 

      if ((data = in_file->Read_Byte ()) == EOF)
      {
        return -1;
      }

      line_data[x][pGREEN] = (DBL) data / 255.0;

      // Read the BLUE data byte. 

      if ((data = in_file->Read_Byte ()) == EOF)
      {
        return -1;
      }

      line_data[x][pBLUE]  = (DBL) data / 255.0;
    }
  }

  line_number++;

  return 1;
}

/*****************************************************************************
*
* FUNCTION
*
*  Read_PPM_Image
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
*    Christoph Hormann
*
* DESCRIPTION
*
*    Reads an PPM image file
*
* CHANGES
*
*    August 2003 - New implementation based on targa/png reading code
*
******************************************************************************/

void Read_PPM_Image(IMAGE *Image, char *name)
{
  IStream *filep;
  unsigned char header[2];
  char line[1024];
  char *ptr;
  int nbr;

  int width, height;
  unsigned int depth;

  IMAGE8_LINE *line_data;
  IMAGE16_LINE *line_16_data;
  int data_hi, data_lo;
  int x, i;

  // --- Start by trying to open the file --- 
  if((filep = Locate_File(name,POV_File_Image_PPM,NULL,true)) == NULL)
    Error ("Cannot open PPM image %s.", name);

  // --- Read Header --- 
  if (!filep->read((char *)header, 2))
    Error ("Cannot read header of PPM image %s.", name);

  if(header[0] != 'P') Error ("File is not in PPM format.");

  if((header[1] != '3') && (header[1] != '6'))
    Error ("File is not in PPM format (type %d).", header[1]);

  do
  {
    filep->getline (line, 1024);
    line[1023] = '\0';
    if ((ptr = strchr(line, '#')) != NULL) *ptr = '\0';  // remove comment 
  }
  while (line[0]=='\0');  // read until line without comment from beginning 

  // --- First: two numbers: with and height --- 
  if (sscanf(line,"%d %d",&width, &height) != 2)
    Error ("Cannot read width and height from PPM image.");

  if (width <= 0 || height <= 0)
    Error ("Invalid width or height read from PPM image.");

  do
  {
    filep->getline (line, 1024) ;
    line[1023] = '\0';
    if ((ptr = strchr(line, '#')) != NULL) *ptr = '\0';  // remove comment 
  }
  while (line[0]=='\0');  // read until line without comment from beginning 

  // --- Second: one number: color depth --- 
  if (sscanf(line,"%d",&depth) != 1)
    Error ("Cannot read color depth from PPM image.");

  if ((depth > 65535) || (depth < 1))
    Error ("Unsupported number of colors (%d) in PPM image.", depth);

  Image->iwidth = width;
  Image->iheight = height;
  Image->width = (DBL)width;
  Image->height = (DBL)height;
  Image->Colour_Map = NULL;
  Image->Colour_Map_Size = 0;

  if (depth < 256)
  {
    Image->data.rgb8_lines = (IMAGE8_LINE *)POV_MALLOC(height * sizeof(IMAGE8_LINE), "PPM image");

    for (i = 0; i < height; i++)
    {
      line_data = &Image->data.rgb8_lines[i];

      line_data->red    = (unsigned char *)POV_MALLOC(width, "PPM image line");
      line_data->green  = (unsigned char *)POV_MALLOC(width, "PPM image line");
      line_data->blue   = (unsigned char *)POV_MALLOC(width, "PPM image line");
      line_data->transm = (unsigned char *)NULL;

      if (header[1] == '3') // --- ASCII PPM file (type 3) --- 
      {
        for (x = 0; x < width; x++)
        {
          nbr = Read_ASCII_File_Number(filep);
          if (nbr >= 0) line_data->red[x] = (nbr*255)/depth;
          else Error ("Cannot read image data from PPM image.");
          nbr = Read_ASCII_File_Number(filep);
          if (nbr >= 0) line_data->green[x] = (nbr*255)/depth;
          else Error ("Cannot read image data from PPM image.");
          nbr = Read_ASCII_File_Number(filep);
          if (nbr >= 0) line_data->blue[x] = (nbr*255)/depth;
          else Error ("Cannot read image data from PPM image.");
        }
      }
      else                  // --- binary PPM file (type 6) --- 
      {
        for (x = 0; x < width; x++)
        {
          if ((nbr = filep->Read_Byte ()) == EOF)
            Error("Cannot read data from PPM image.");

          line_data->red[x] = (nbr*255)/depth;

          if ((nbr = filep->Read_Byte ()) == EOF)
            Error("Cannot read data from PPM image.");

          line_data->green[x] = (nbr*255)/depth;

          if ((nbr = filep->Read_Byte ()) == EOF)
            Error("Cannot read data from PPM image.");

          line_data->blue[x] = (nbr*255)/depth;
        }
      }
    }
  }
  else // --- 16 bit PPM (binary or ASCII) --- 
  {
    Image->Image_Type |= IS16BITIMAGE;

    Image->data.rgb16_lines = (IMAGE16_LINE *)POV_MALLOC(height * sizeof(IMAGE16_LINE), "PPM image");

    for (i = 0; i < height; i++)
    {
      line_16_data = &Image->data.rgb16_lines[i];

      line_16_data->red    = (unsigned short *)POV_MALLOC(width * sizeof(unsigned short), "PPM image line");
      line_16_data->green  = (unsigned short *)POV_MALLOC(width * sizeof(unsigned short), "PPM image line");
      line_16_data->blue   = (unsigned short *)POV_MALLOC(width * sizeof(unsigned short), "PPM image line");
      line_16_data->transm = (unsigned short *)NULL;

      if (header[1] == '3') // --- ASCII PPM file (type 3) --- 
      {
        for (x = 0; x < width; x++)
        {
          nbr = Read_ASCII_File_Number(filep);
          if (nbr >= 0) line_16_data->red[x] = (nbr*65535)/depth;
          else Error ("Cannot read image data from PPM image.");

          nbr = Read_ASCII_File_Number(filep);
          if (nbr >= 0) line_16_data->green[x] = (nbr*65535)/depth;
          else Error ("Cannot read image data from PPM image.");

          nbr = Read_ASCII_File_Number(filep);
          if (nbr >= 0) line_16_data->blue[x] = (nbr*65535)/depth;
          else Error ("Cannot read image data from PPM image.");
        }
      }
      else                  // --- binary PPM file (type 6) --- 
      {
        for (x = 0; x < width; x++)
        {
          if ((data_hi = filep->Read_Byte ()) == EOF)
            Error ("Cannot read data from PPM image.");
          if ((data_lo = filep->Read_Byte ()) == EOF)
            Error ("Cannot read data from PPM image.");
          line_16_data->red[x] = (256*data_hi + data_lo)*65535/depth;

          if ((data_hi = filep->Read_Byte ()) == EOF)
            Error ("Cannot read data from PPM image.");
          if ((data_lo = filep->Read_Byte ()) == EOF)
            Error ("Cannot read data from PPM image.");
          line_16_data->green[x] = (256*data_hi + data_lo)*65535/depth;

          if ((data_hi = filep->Read_Byte ()) == EOF)
            Error ("Cannot read data from PPM image.");
          if ((data_lo = filep->Read_Byte ()) == EOF)
            Error ("Cannot read data from PPM image.");
          line_16_data->blue[x] = (256*data_hi + data_lo)*65535/depth;
        }
      }
    }
  }

  // Close the image file 

  delete filep;
}

END_POV_NAMESPACE