LoadPNG.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (c) 2009 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef LOAD_PNG_HPP
  26. #define LOAD_PNG_HPP
  27. #include <cat/AllFramework.hpp>
  28. #include <string>
  29. using namespace std;
  30. #include "zlib-1.2.4/zlib.h"
  31. namespace cat {
  32. #include "CRC32.hpp"
  33. #ifdef CAT_PRAGMA_PACK
  34. #pragma pack(push)
  35. #pragma pack(1)
  36. #endif
  37. typedef struct
  38. {
  39. u32 length;
  40. char type[4];
  41. } CAT_PACKED SectionHeader;
  42. typedef struct
  43. {
  44. u32 length;
  45. char type[4];
  46. u32 crc;
  47. } CAT_PACKED EmptySection;
  48. typedef struct
  49. {
  50. u32 width, height;
  51. u8 bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod;
  52. } CAT_PACKED PNG_IHDR;
  53. typedef struct
  54. {
  55. u8 r, g, b;
  56. } CAT_PACKED PLTE_Entry8;
  57. typedef struct
  58. {
  59. PLTE_Entry8 table[256];
  60. } CAT_PACKED PNG_PLTE;
  61. #ifdef CAT_PRAGMA_PACK
  62. #pragma pack(pop)
  63. #endif
  64. class PNGSkeletonTokenizer
  65. {
  66. protected:
  67. MMapFile mmf;
  68. CRC32Calculator calculator;
  69. string path;
  70. // Split this from the ctor, so virtual overloads are in place by the time we start reading
  71. bool read(const u8 signature[8]); // Returns false on failure
  72. public:
  73. PNGSkeletonTokenizer(const string &path, u32 CRC32polynomial);
  74. virtual ~PNGSkeletonTokenizer() {}
  75. protected:
  76. // return true only if section is valid (no exceptions please)
  77. virtual bool onSection(char type[4], u8 data[], u32 len) = 0;
  78. };
  79. class PNGTokenizer : public PNGSkeletonTokenizer
  80. {
  81. protected:
  82. z_stream zstream;
  83. u8 *obuf;
  84. u32 olen;
  85. int lastZlibResult;
  86. bool requirePOTS;
  87. PNG_IHDR header;
  88. u16 bpp;
  89. u32 palette[256];
  90. u8 trans_red, trans_green, trans_blue;
  91. void rasterizeImage(u8 *image);
  92. public:
  93. PNGTokenizer(const string &path, bool requirePOTS);
  94. virtual ~PNGTokenizer();
  95. protected:
  96. bool onSection(char type[4], u8 data[], u32 len);
  97. protected:
  98. // Rasterized image in R8G8B8A8 format, new dimensions are powers of two
  99. virtual void onImage(u32 *image, u32 newWidth, u32 newHeight);
  100. protected:
  101. // Important sections
  102. void onIHDR(PNG_IHDR *infohdr);
  103. void onPLTE(PNG_PLTE *c_palette);
  104. void onIDAT(u8 *data, u32 len);
  105. void onIEND();
  106. // Transparency info
  107. void onTRNS_Color2(u16 red, u16 green, u16 blue);
  108. void onTRNS_Color3(u8 trans[256], u16 len);
  109. // Color space information (all unimplemented)
  110. void onGAMA();
  111. void onCHRM();
  112. void onSRGB();
  113. void onICCP();
  114. // Textual information (all unimplemented)
  115. void onTEXT();
  116. void onZTXT();
  117. void onITXT();
  118. // Other non-essential info (all unimplemented)
  119. void onBKGD();
  120. void onPHYS();
  121. void onSBIT();
  122. void onSPLT();
  123. void onHIST();
  124. void onTIME();
  125. };
  126. } // namespace cat
  127. #endif // LOAD_PNG_HPP
粤ICP备19079148号