1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <vector>
20#include <iostream>
21#include <fstream>
22#include <sstream>
23#include <memory.h>
24
25/*!
26 * Simple class to manipulate PGM/PPM images. Not suitable for heavy lifting.
27 */
28class PgmImage
29{
30    friend std::ostream& operator<< (std::ostream& o, const PgmImage& im);
31public:
32    enum {PGM_BINARY_GRAYMAP,PGM_BINARY_PIXMAP,PGM_FORMAT_INVALID};
33    /*!
34    * Constructor from a PGM file name.
35    */
36    PgmImage(std::string filename);
37    /*!
38    * Constructor to allocate an image of given size and type.
39    */
40    PgmImage(int w, int h, int format = PGM_BINARY_GRAYMAP);
41    /*!
42    * Constructor to allocate an image of given size and copy the data in.
43    */
44    PgmImage(unsigned char *data, int w, int h);
45    /*!
46    * Constructor to allocate an image of given size and copy the data in.
47    */
48    PgmImage(std::vector<unsigned char> &data, int w, int h);
49
50    PgmImage(const PgmImage &im);
51
52    PgmImage& operator= (const PgmImage &im);
53    ~PgmImage();
54
55    int GetHeight() const { return m_h; }
56    int GetWidth() const { return m_w; }
57
58    //! Copy pixels from data pointer
59    void SetData(const unsigned char * data);
60
61    //! Get a data pointer to unaligned memory area
62    unsigned char * GetDataPointer() { if ( m_data.size() > 0 ) return &m_data[0]; else return NULL; }
63    unsigned char ** GetRowPointers() { if ( m_rows.size() == m_h ) return &m_rows[0]; else return NULL; }
64
65    //! Read a PGM file from disk
66    bool ReadPGM(const std::string filename);
67    //! Write a PGM file to disk
68    bool WritePGM(const std::string filename, const std::string comment="");
69
70    //! Get image format (returns PGM_BINARY_GRAYMAP, PGM_BINARY_PIXMAP or PGM_FORMAT_INVALID)
71    int GetFormat() const { return m_format; }
72
73    //! Set image format (returns PGM_BINARY_GRAYMAP, PGM_BINARY_PIXMAP). Image data becomes invalid.
74    void SetFormat(int format);
75
76    //! If the image is PGM_BINARY_PIXMAP, convert it to PGM_BINARY_GRAYMAP via Y = 0.3*R + 0.59*G + 0.11*B.
77    void ConvertToGray();
78protected:
79    // Generic functions:
80    void DeepCopy(const PgmImage& src, PgmImage& dst);
81    void SetupRowPointers();
82
83    // PGM data
84    int m_w;
85    int m_h;
86    int m_format;
87    int m_colors;
88    int m_over_allocation;
89    std::vector<unsigned char> m_data;
90    std::string m_comment;
91
92    std::vector<unsigned char *> m_rows;
93};
94
95std::ostream& operator<< (std::ostream& o, const PgmImage& im);
96