png.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #pragma once
  2. #include <nall/string.hpp>
  3. #include <nall/decode/inflate.hpp>
  4. namespace nall::Decode {
  5. struct PNG {
  6. inline PNG();
  7. inline ~PNG();
  8. inline auto load(const string& filename) -> bool;
  9. inline auto load(const uint8_t* sourceData, uint sourceSize) -> bool;
  10. inline auto readbits(const uint8_t*& data) -> uint;
  11. struct Info {
  12. uint width;
  13. uint height;
  14. uint bitDepth;
  15. //colorType:
  16. //0 = L (luma)
  17. //2 = R,G,B
  18. //3 = P (palette)
  19. //4 = L,A
  20. //6 = R,G,B,A
  21. uint colorType;
  22. uint compressionMethod;
  23. uint filterType;
  24. uint interlaceMethod;
  25. uint bytesPerPixel;
  26. uint pitch;
  27. uint8_t palette[256][3];
  28. } info;
  29. uint8_t* data = nullptr;
  30. uint size = 0;
  31. uint bitpos = 0;
  32. protected:
  33. enum class FourCC : uint {
  34. IHDR = 0x49484452,
  35. PLTE = 0x504c5445,
  36. IDAT = 0x49444154,
  37. IEND = 0x49454e44,
  38. };
  39. inline auto interlace(uint pass, uint index) -> uint;
  40. inline auto inflateSize() -> uint;
  41. inline auto deinterlace(const uint8_t*& inputData, uint pass) -> bool;
  42. inline auto filter(uint8_t* outputData, const uint8_t* inputData, uint width, uint height) -> bool;
  43. inline auto read(const uint8_t* data, uint length) -> uint;
  44. };
  45. PNG::PNG() {
  46. }
  47. PNG::~PNG() {
  48. if(data) delete[] data;
  49. }
  50. auto PNG::load(const string& filename) -> bool {
  51. if(auto memory = file::read(filename)) {
  52. return load(memory.data(), memory.size());
  53. }
  54. return false;
  55. }
  56. auto PNG::load(const uint8_t* sourceData, uint sourceSize) -> bool {
  57. if(sourceSize < 8) return false;
  58. if(read(sourceData + 0, 4) != 0x89504e47) return false;
  59. if(read(sourceData + 4, 4) != 0x0d0a1a0a) return false;
  60. uint8_t* compressedData = nullptr;
  61. uint compressedSize = 0;
  62. uint offset = 8;
  63. while(offset < sourceSize) {
  64. uint length = read(sourceData + offset + 0, 4);
  65. uint fourCC = read(sourceData + offset + 4, 4);
  66. uint checksum = read(sourceData + offset + 8 + length, 4);
  67. if(fourCC == (uint)FourCC::IHDR) {
  68. info.width = read(sourceData + offset + 8, 4);
  69. info.height = read(sourceData + offset + 12, 4);
  70. info.bitDepth = read(sourceData + offset + 16, 1);
  71. info.colorType = read(sourceData + offset + 17, 1);
  72. info.compressionMethod = read(sourceData + offset + 18, 1);
  73. info.filterType = read(sourceData + offset + 19, 1);
  74. info.interlaceMethod = read(sourceData + offset + 20, 1);
  75. if(info.bitDepth == 0 || info.bitDepth > 16) return false;
  76. if(info.bitDepth & (info.bitDepth - 1)) return false; //not a power of two
  77. if(info.compressionMethod != 0) return false;
  78. if(info.filterType != 0) return false;
  79. if(info.interlaceMethod != 0 && info.interlaceMethod != 1) return false;
  80. switch(info.colorType) {
  81. case 0: info.bytesPerPixel = info.bitDepth * 1; break; //L
  82. case 2: info.bytesPerPixel = info.bitDepth * 3; break; //R,G,B
  83. case 3: info.bytesPerPixel = info.bitDepth * 1; break; //P
  84. case 4: info.bytesPerPixel = info.bitDepth * 2; break; //L,A
  85. case 6: info.bytesPerPixel = info.bitDepth * 4; break; //R,G,B,A
  86. default: return false;
  87. }
  88. if(info.colorType == 2 || info.colorType == 4 || info.colorType == 6) {
  89. if(info.bitDepth != 8 && info.bitDepth != 16) return false;
  90. }
  91. if(info.colorType == 3 && info.bitDepth == 16) return false;
  92. info.bytesPerPixel = (info.bytesPerPixel + 7) / 8;
  93. info.pitch = (int)info.width * info.bytesPerPixel;
  94. }
  95. if(fourCC == (uint)FourCC::PLTE) {
  96. if(length % 3) return false;
  97. for(uint n = 0, p = offset + 8; n < length / 3; n++) {
  98. info.palette[n][0] = sourceData[p++];
  99. info.palette[n][1] = sourceData[p++];
  100. info.palette[n][2] = sourceData[p++];
  101. }
  102. }
  103. if(fourCC == (uint)FourCC::IDAT) {
  104. compressedData = (uint8_t*)realloc(compressedData, compressedSize + length);
  105. memcpy(compressedData + compressedSize, sourceData + offset + 8, length);
  106. compressedSize += length;
  107. }
  108. if(fourCC == (uint)FourCC::IEND) {
  109. break;
  110. }
  111. offset += 4 + 4 + length + 4;
  112. }
  113. uint interlacedSize = inflateSize();
  114. auto interlacedData = new uint8_t[interlacedSize];
  115. bool result = inflate(interlacedData, interlacedSize, compressedData + 2, compressedSize - 6);
  116. free(compressedData);
  117. if(result == false) {
  118. delete[] interlacedData;
  119. return false;
  120. }
  121. size = info.width * info.height * info.bytesPerPixel;
  122. data = new uint8_t[size];
  123. if(info.interlaceMethod == 0) {
  124. if(filter(data, interlacedData, info.width, info.height) == false) {
  125. delete[] interlacedData;
  126. delete[] data;
  127. data = nullptr;
  128. return false;
  129. }
  130. } else {
  131. const uint8_t* passData = interlacedData;
  132. for(uint pass = 0; pass < 7; pass++) {
  133. if(deinterlace(passData, pass) == false) {
  134. delete[] interlacedData;
  135. delete[] data;
  136. data = nullptr;
  137. return false;
  138. }
  139. }
  140. }
  141. delete[] interlacedData;
  142. return true;
  143. }
  144. auto PNG::interlace(uint pass, uint index) -> uint {
  145. static const uint data[7][4] = {
  146. //x-distance, y-distance, x-origin, y-origin
  147. {8, 8, 0, 0},
  148. {8, 8, 4, 0},
  149. {4, 8, 0, 4},
  150. {4, 4, 2, 0},
  151. {2, 4, 0, 2},
  152. {2, 2, 1, 0},
  153. {1, 2, 0, 1},
  154. };
  155. return data[pass][index];
  156. }
  157. auto PNG::inflateSize() -> uint {
  158. if(info.interlaceMethod == 0) {
  159. return info.width * info.height * info.bytesPerPixel + info.height;
  160. }
  161. uint size = 0;
  162. for(uint pass = 0; pass < 7; pass++) {
  163. uint xd = interlace(pass, 0), yd = interlace(pass, 1);
  164. uint xo = interlace(pass, 2), yo = interlace(pass, 3);
  165. uint width = (info.width + (xd - xo - 1)) / xd;
  166. uint height = (info.height + (yd - yo - 1)) / yd;
  167. if(width == 0 || height == 0) continue;
  168. size += width * height * info.bytesPerPixel + height;
  169. }
  170. return size;
  171. }
  172. auto PNG::deinterlace(const uint8_t*& inputData, uint pass) -> bool {
  173. uint xd = interlace(pass, 0), yd = interlace(pass, 1);
  174. uint xo = interlace(pass, 2), yo = interlace(pass, 3);
  175. uint width = (info.width + (xd - xo - 1)) / xd;
  176. uint height = (info.height + (yd - yo - 1)) / yd;
  177. if(width == 0 || height == 0) return true;
  178. uint outputSize = width * height * info.bytesPerPixel;
  179. auto outputData = new uint8_t[outputSize];
  180. bool result = filter(outputData, inputData, width, height);
  181. const uint8_t* rd = outputData;
  182. for(uint y = yo; y < info.height; y += yd) {
  183. uint8_t* wr = data + y * info.pitch;
  184. for(uint x = xo; x < info.width; x += xd) {
  185. for(uint b = 0; b < info.bytesPerPixel; b++) {
  186. wr[x * info.bytesPerPixel + b] = *rd++;
  187. }
  188. }
  189. }
  190. inputData += outputSize + height;
  191. delete[] outputData;
  192. return result;
  193. }
  194. auto PNG::filter(uint8_t* outputData, const uint8_t* inputData, uint width, uint height) -> bool {
  195. uint8_t* wr = outputData;
  196. const uint8_t* rd = inputData;
  197. int bpp = info.bytesPerPixel, pitch = width * bpp;
  198. for(int y = 0; y < height; y++) {
  199. uint8_t filter = *rd++;
  200. switch(filter) {
  201. case 0x00: //None
  202. for(int x = 0; x < pitch; x++) {
  203. wr[x] = rd[x];
  204. }
  205. break;
  206. case 0x01: //Subtract
  207. for(int x = 0; x < pitch; x++) {
  208. wr[x] = rd[x] + (x - bpp < 0 ? 0 : wr[x - bpp]);
  209. }
  210. break;
  211. case 0x02: //Above
  212. for(int x = 0; x < pitch; x++) {
  213. wr[x] = rd[x] + (y - 1 < 0 ? 0 : wr[x - pitch]);
  214. }
  215. break;
  216. case 0x03: //Average
  217. for(int x = 0; x < pitch; x++) {
  218. short a = x - bpp < 0 ? 0 : wr[x - bpp];
  219. short b = y - 1 < 0 ? 0 : wr[x - pitch];
  220. wr[x] = rd[x] + (uint8_t)((a + b) / 2);
  221. }
  222. break;
  223. case 0x04: //Paeth
  224. for(int x = 0; x < pitch; x++) {
  225. short a = x - bpp < 0 ? 0 : wr[x - bpp];
  226. short b = y - 1 < 0 ? 0 : wr[x - pitch];
  227. short c = x - bpp < 0 || y - 1 < 0 ? 0 : wr[x - pitch - bpp];
  228. short p = a + b - c;
  229. short pa = p > a ? p - a : a - p;
  230. short pb = p > b ? p - b : b - p;
  231. short pc = p > c ? p - c : c - p;
  232. auto paeth = (uint8_t)((pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c);
  233. wr[x] = rd[x] + paeth;
  234. }
  235. break;
  236. default: //Invalid
  237. return false;
  238. }
  239. rd += pitch;
  240. wr += pitch;
  241. }
  242. return true;
  243. }
  244. auto PNG::read(const uint8_t* data, uint length) -> uint {
  245. uint result = 0;
  246. while(length--) result = (result << 8) | (*data++);
  247. return result;
  248. }
  249. auto PNG::readbits(const uint8_t*& data) -> uint {
  250. uint result = 0;
  251. switch(info.bitDepth) {
  252. case 1:
  253. result = (*data >> bitpos) & 1;
  254. bitpos++;
  255. if(bitpos == 8) { data++; bitpos = 0; }
  256. break;
  257. case 2:
  258. result = (*data >> bitpos) & 3;
  259. bitpos += 2;
  260. if(bitpos == 8) { data++; bitpos = 0; }
  261. break;
  262. case 4:
  263. result = (*data >> bitpos) & 15;
  264. bitpos += 4;
  265. if(bitpos == 8) { data++; bitpos = 0; }
  266. break;
  267. case 8:
  268. result = *data++;
  269. break;
  270. case 16:
  271. result = (data[0] << 8) | (data[1] << 0);
  272. data += 2;
  273. break;
  274. }
  275. return result;
  276. }
  277. }