summaryrefslogtreecommitdiff
path: root/hw2/src/imageIO.ppm.cpp
blob: 28dbc31064e28dd866f3a8ca84dcaed51f309303 (plain) (blame)
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
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527     */
/* without consent of either the College of William & Mary or     */
/* Pieter Peers.                                                  */
/******************************************************************/
#include "imageIO.ppm.h"
#include "errorMessage.h"

#include <memory>
#include <cassert>
#include <cstdint>
#include <fstream>
#include <algorithm>

//////////////////////
// Helper functions //
//////////////////////
static void skipToNewline(std::ifstream& ifs)
{
  while(ifs.get() != '\n' && ifs.good()); 
}

static void skipComments(std::ifstream& ifs)
{
  while(!ifs.eof() && ifs.peek() == '#') skipToNewline(ifs); 
}


////////////////
// Import PPM //
////////////////
void importPPM(const std::string& name, image& img)
{
  // open file
  std::ifstream ifs(name.c_str());
  if(!ifs.is_open()) errorMessage("Unable to open file: '%s'.", name.c_str());

  // read header
  std::string magicMark;
  ifs >> magicMark;
  skipToNewline(ifs);
  if(magicMark != "P6") errorMessage("Unsupported PPM format (%s).", magicMark.c_str());

  // read width & height
  image::size_type width, height;
  skipComments(ifs);
  ifs >> width >> height;
  skipToNewline(ifs);

  // allocate
  img = image(width, height);

  // check magic number (again)
  skipComments(ifs);
  ifs >> magicMark;
  skipToNewline(ifs);
  if(magicMark != "255") errorMessage("Unsupported bit-depth in PPM file (%s).", magicMark.c_str());

  // read char buffer
  std::unique_ptr<uint8_t[]> tempBuffer(new uint8_t[img.size() * 3]);
  ifs.read((char *)(tempBuffer.get()), img.size() * 3);

  // convert to image
  std::transform(tempBuffer.get(), tempBuffer.get() + (img.size()*3), img.begin()->begin(), [](uint8_t val) 
  { 
    return (float)(val) / 255.0f; 
  });

  // Done.
}


////////////////
// Export PPM //
////////////////
void exportPPM(const std::string& name, const image& img)
{
  // sanity check
  assert(img.width() != 0 && img.height() != 0);

  // open file
  std::ofstream ofs(name.c_str());
  if(!ofs.is_open()) errorMessage("Unable to open file: '%s'.", name.c_str());

  // write header
  ofs << "P6\n" << img.width() << " " << img.height() << "\n" << "255\n";

  // convert to char buffer
  std::unique_ptr<uint8_t[]> tempBuffer(new uint8_t[img.size() * 3]);
  std::transform(img.begin()->begin(), img.end()->begin(), tempBuffer.get(), [](float val)
  { 
    return (val < 0.0f) ? 0 : 
           (val > 1.0f) ? 255 : 
           uint8_t(val*255); 
   });
    
  // write body
  ofs.write((const char*)(tempBuffer.get()), img.size() * 3);

  // Done.
}