Shadowrun: Awakened 29 September 2011 - Build 871
LoadPNG.hpp
Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009 Christopher A. Taylor.  All rights reserved.
00003 
00004     Redistribution and use in source and binary forms, with or without
00005     modification, are permitted provided that the following conditions are met:
00006 
00007     * Redistributions of source code must retain the above copyright notice,
00008       this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice,
00010       this list of conditions and the following disclaimer in the documentation
00011       and/or other materials provided with the distribution.
00012     * Neither the name of LibCat nor the names of its contributors may be used
00013       to endorse or promote products derived from this software without
00014       specific prior written permission.
00015 
00016     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019     ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00020     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026     POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 
00029 #ifndef LOAD_PNG_HPP
00030 #define LOAD_PNG_HPP
00031 
00032 #include <cat/AllFramework.hpp>
00033 #include <string>
00034 using namespace std;
00035 #include "zlib-1.2.4/zlib.h"
00036 
00037 namespace cat {
00038 
00039 #include "CRC32.hpp"
00040 
00041 #ifdef CAT_PRAGMA_PACK
00042 #pragma pack(push)
00043 #pragma pack(1)
00044 #endif
00045 
00046     typedef struct
00047     {
00048         u32 length;
00049         char type[4];
00050     } CAT_PACKED SectionHeader;
00051 
00052     typedef struct
00053     {
00054         u32 length;
00055         char type[4];
00056         u32 crc;
00057     } CAT_PACKED EmptySection;
00058 
00059     typedef struct
00060     {
00061         u32 width, height;
00062         u8 bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod;
00063     } CAT_PACKED PNG_IHDR;
00064 
00065     typedef struct
00066     {
00067         u8 r, g, b;
00068     } CAT_PACKED PLTE_Entry8;
00069 
00070     typedef struct
00071     {
00072         PLTE_Entry8 table[256];
00073     } CAT_PACKED PNG_PLTE;
00074 
00075 #ifdef CAT_PRAGMA_PACK
00076 #pragma pack(pop)
00077 #endif
00078 
00079 
00080     class PNGSkeletonTokenizer
00081     {
00082     protected:
00083         MMapFile mmf;
00084         CRC32Calculator calculator;
00085         string path;
00086 
00087         // Split this from the ctor, so virtual overloads are in place by the time we start reading
00088         bool read(const u8 signature[8]); // Returns false on failure
00089 
00090     public:
00091         PNGSkeletonTokenizer(const string &path, u32 CRC32polynomial);
00092         virtual ~PNGSkeletonTokenizer() {}
00093 
00094     protected:
00095         // return true only if section is valid (no exceptions please)
00096         virtual bool onSection(char type[4], u8 data[], u32 len) = 0;
00097     };
00098 
00099 
00100     class PNGTokenizer : public PNGSkeletonTokenizer
00101     {
00102     protected:
00103         z_stream zstream;
00104         u8 *obuf;
00105         u32 olen;
00106         int lastZlibResult;
00107 
00108         bool requirePOTS;
00109 
00110         PNG_IHDR header;
00111         u16 bpp;
00112         u32 palette[256];
00113         u8 trans_red, trans_green, trans_blue;
00114 
00115         void rasterizeImage(u8 *image);
00116 
00117     public:
00118         PNGTokenizer(const string &path, bool requirePOTS);
00119         virtual ~PNGTokenizer();
00120 
00121     protected:
00122         bool onSection(char type[4], u8 data[], u32 len);
00123 
00124     protected:
00125         // Rasterized image in R8G8B8A8 format, new dimensions are powers of two
00126         virtual void onImage(u32 *image, u32 newWidth, u32 newHeight);
00127 
00128     protected:
00129         // Important sections
00130         void onIHDR(PNG_IHDR *infohdr);
00131         void onPLTE(PNG_PLTE *c_palette);
00132         void onIDAT(u8 *data, u32 len);
00133         void onIEND();
00134 
00135         // Transparency info
00136         void onTRNS_Color2(u16 red, u16 green, u16 blue);
00137         void onTRNS_Color3(u8 trans[256], u16 len);
00138 
00139         // Color space information (all unimplemented)
00140         void onGAMA();
00141         void onCHRM();
00142         void onSRGB();
00143         void onICCP();
00144 
00145         // Textual information (all unimplemented)
00146         void onTEXT();
00147         void onZTXT();
00148         void onITXT();
00149 
00150         // Other non-essential info (all unimplemented)
00151         void onBKGD();
00152         void onPHYS();
00153         void onSBIT();
00154         void onSPLT();
00155         void onHIST();
00156         void onTIME();
00157     };
00158 
00159 
00160 } // namespace cat
00161 
00162 #endif // LOAD_PNG_HPP

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net