1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 #ifndef BZF_PNG_IMAGE_FILE_H
14 #define BZF_PNG_IMAGE_FILE_H
15 
16 #include "ImageFile.h"
17 
18 class PNGPalette;
19 class PNGChunk;
20 
21 
22 /** This class represents a PNG image file. It implements the read() function
23     from ImageFile. */
24 class PNGImageFile : public ImageFile
25 {
26 public:
27     PNGImageFile(std::istream*);
28     virtual ~PNGImageFile();
29 
30     /** This function returns the default extension of PNG image files. */
31     static std::string    getExtension();
32 
33     /** Read image data from a PNG file. */
34     virtual bool      read(void* buffer);
35 private:
36     PNGPalette* readPalette(PNGChunk *c);
37     unsigned char *getLineBuffer(bool active=true);
38     void switchLineBuffers();
39     bool filter();
40     bool expand(unsigned char * destination);
41 
42     static unsigned char          PNGHEADER[8];
43     static const unsigned char        MAX_COMPONENTS;
44     static const unsigned char        FILTER_NONE;
45     static const unsigned char        FILTER_SUB;
46     static const unsigned char        FILTER_UP;
47     static const unsigned char        FILTER_AVERAGE;
48     static const unsigned char        FILTER_PAETH;
49 
50     PNGPalette*                       palette;
51     unsigned char                 bitDepth;
52     unsigned char                 colorDepth;
53     unsigned char                 compressionMethod;
54     unsigned char                 filterMethod;
55     unsigned char                 interlaceMethod;
56     unsigned char                 *lineBuffers[2];
57     int                               activeBufferIndex;
58     int                               lineBufferSize;
59     int                               realBufferSize;
60 };
61 
62 /** This class represents a RGB color value used by the PNG reader. */
63 class PNGRGB
64 {
65 public:
66     PNGRGB();
67     PNGRGB(unsigned char r, unsigned char g, unsigned char b);
68     unsigned char red;
69     unsigned char green;
70     unsigned char blue;
71 };
72 
73 /** This class represents a PNG color palette and is used by the PNG reader.
74     It contains an indexed list of PNGRGB objects. */
75 class PNGPalette
76 {
77 public:
78     PNGPalette( int numColors );
79     ~PNGPalette();
80     void add( PNGRGB &color );
81     PNGRGB& get(int index);
82 private:
83     int       curColor;
84     int       numColors;
85     PNGRGB    *colors;
86 };
87 
88 /** This class represents a chunk of PNG data. It is used by the PNG reader. */
89 class PNGChunk
90 {
91 public:
92     static PNGChunk *readChunk(std::istream *stream);
93     ~PNGChunk();
94     int getLength();
95     int getType();
96     unsigned char* getData();
97 
98     static int        IHDR;
99     static int        PLTE;
100     static int        IDAT;
101     static int        IEND;
102 private:
103     PNGChunk();
104     int           length;
105     int           type;
106     unsigned char     *data;
107     int           crc;
108 
109 };
110 
111 #endif
112 
113 // Local Variables: ***
114 // mode: C++ ***
115 // tab-width: 4 ***
116 // c-basic-offset: 4 ***
117 // indent-tabs-mode: nil ***
118 // End: ***
119 // ex: shiftwidth=4 tabstop=4
120