fork download
  1. #include "Texture.h"
  2.  
  3. void Texture::Load(string filename,bool fixEdge)
  4. {
  5. //load and decode
  6. std::vector<unsigned char> buffer, image;
  7. loadFile(buffer, filename);
  8. unsigned long w, h;
  9. int error = decodePNG(image, w, h, buffer.empty() ? 0 : &buffer[0], (unsigned long)buffer.size());
  10.  
  11. if (!error) {
  12. glEnable(GL_TEXTURE_2D);
  13. width=w;
  14. height=h;
  15. glPixelStorei(GL_UNPACK_ALIGNMENT,4);
  16. glGenTextures(1,&texID);
  17. glBindTexture(GL_TEXTURE_2D,texID);
  18. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  19. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  20. if (fixEdge) {
  21. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
  22. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
  23. } else {
  24. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  25. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  26. }
  27.  
  28. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, &image[0]);
  29. } else {
  30. cout << "Error: " << filename << endl;
  31. }
  32. }
  33.  
  34. /*
  35. void Texture::loadFile(vector<unsigned char>& buffer,const string& filename) // release
  36. {
  37. ifstream file("data.bin",ios::in|ios::binary);
  38. unsigned len;
  39. while(file.read((char*)&len,sizeof(unsigned))) {
  40. string currname(len,'\0');
  41. file.read(&currname[0],len);
  42. streamsize size;
  43. file.read((char*)&size,sizeof(streamsize));
  44. if(currname==filename) {
  45. buffer.resize(size);
  46. file.read((char*)&buffer[0],size);
  47. return;
  48. }
  49. file.seekg(size,ios::cur);
  50. }
  51. }*/
  52.  
  53. void Texture::loadFile(std::vector<unsigned char>& buffer, const std::string& filename) //just debug, designed for loading files from hard disk in an std::vector
  54. {
  55. std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
  56.  
  57. //get filesize
  58. std::streamsize size = 0;
  59. if(file.seekg(0, std::ios::end).good()) size = file.tellg();
  60. if(file.seekg(0, std::ios::beg).good()) size -= file.tellg();
  61.  
  62. //read contents of the file into the vector
  63. if(size > 0)
  64. {
  65. buffer.resize((size_t)size);
  66. file.read((char*)(&buffer[0]), size);
  67. }
  68. else buffer.clear();
  69. }
  70.  
  71. /*
  72. decodePNG: The picoPNG function, decodes a PNG file buffer in memory, into a raw pixel buffer.
  73. out_image: output parameter, this will contain the raw pixels after decoding.
  74.   By default the output is 32-bit RGBA color.
  75.   The std::vector is automatically resized to the correct size.
  76. image_width: output_parameter, this will contain the width of the image in pixels.
  77. image_height: output_parameter, this will contain the height of the image in pixels.
  78. in_png: pointer to the buffer of the PNG file in memory. To get it from a file on
  79.   disk, load it and store it in a memory buffer yourself first.
  80. in_size: size of the input PNG file in bytes.
  81. convert_to_rgba32: optional parameter, true by default.
  82.   Set to true to get the output in RGBA 32-bit (8 bit per channel) color format
  83.   no matter what color type the original PNG image had. This gives predictable,
  84.   useable data from any random input PNG.
  85.   Set to false to do no color conversion at all. The result then has the same data
  86.   type as the PNG image, which can range from 1 bit to 64 bits per pixel.
  87.   Information about the color type or palette colors are not provided. You need
  88.   to know this information yourself to be able to use the data so this only
  89.   works for trusted PNG files. Use LodePNG instead of picoPNG if you need this information.
  90. return: 0 if success, not 0 if some error occured.
  91. */
  92.  
  93. int Texture::decodePNG(std::vector<unsigned char>& out_image, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, size_t in_size, bool convert_to_rgba32)
  94. {
  95. // picoPNG version 20101224
  96. // Copyright (c) 2005-2010 Lode Vandevenne
  97. //
  98. // This software is provided 'as-is', without any express or implied
  99. // warranty. In no event will the authors be held liable for any damages
  100. // arising from the use of this software.
  101. //
  102. // Permission is granted to anyone to use this software for any purpose,
  103. // including commercial applications, and to alter it and redistribute it
  104. // freely, subject to the following restrictions:
  105. //
  106. // 1. The origin of this software must not be misrepresented; you must not
  107. // claim that you wrote the original software. If you use this software
  108. // in a product, an acknowledgment in the product documentation would be
  109. // appreciated but is not required.
  110. // 2. Altered source versions must be plainly marked as such, and must not be
  111. // misrepresented as being the original software.
  112. // 3. This notice may not be removed or altered from any source distribution.
  113.  
  114. // picoPNG is a PNG decoder in one C++ function of around 500 lines. Use picoPNG for
  115. // programs that need only 1 .cpp file. Since it's a single function, it's very limited,
  116. // it can convert a PNG to raw pixel data either converted to 32-bit RGBA color or
  117. // with no color conversion at all. For anything more complex, another tiny library
  118. // is available: LodePNG (lodepng.c(pp)), which is a single source and header file.
  119. // Apologies for the compact code style, it's to make this tiny.
  120.  
  121. static const unsigned long LENBASE[29] = {3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258};
  122. static const unsigned long LENEXTRA[29] = {0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
  123. static const unsigned long DISTBASE[30] = {1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577};
  124. static const unsigned long DISTEXTRA[30] = {0,0,0,0,1,1,2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
  125. static const unsigned long CLCL[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; //code length code lengths
  126. struct Zlib { //nested functions for zlib decompression
  127. static unsigned long readBitFromStream(size_t& bitp, const unsigned char* bits) {
  128. unsigned long result = (bits[bitp >> 3] >> (bitp & 0x7)) & 1;
  129. bitp++;
  130. return result;
  131. }
  132. static unsigned long readBitsFromStream(size_t& bitp, const unsigned char* bits, size_t nbits) {
  133. unsigned long result = 0;
  134. for(size_t i = 0; i < nbits; i++) {
  135. result += (readBitFromStream(bitp, bits)) << i;
  136. }
  137. return result;
  138. }
  139. struct HuffmanTree {
  140. int makeFromLengths(const std::vector<unsigned long>& bitlen, unsigned long maxbitlen) {
  141. //make tree given the lengths
  142. unsigned long numcodes = (unsigned long)(bitlen.size()), treepos = 0, nodefilled = 0;
  143. std::vector<unsigned long> tree1d(numcodes), blcount(maxbitlen + 1, 0), nextcode(maxbitlen + 1, 0);
  144. for(unsigned long bits = 0; bits < numcodes; bits++) {
  145. blcount[bitlen[bits]]++; //count number of instances of each code length
  146. }
  147. for(unsigned long bits = 1; bits <= maxbitlen; bits++) {
  148. nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1;
  149. }
  150. for(unsigned long n = 0; n < numcodes; n++) if(bitlen[n] != 0) {
  151. tree1d[n] = nextcode[bitlen[n]]++; //generate all the codes
  152. }
  153. tree2d.clear();
  154. tree2d.resize(numcodes * 2, 32767); //32767 here means the tree2d isn't filled there yet
  155. for(unsigned long n = 0; n < numcodes; n++) //the codes
  156. for(unsigned long i = 0; i < bitlen[n]; i++) { //the bits for this code
  157. unsigned long bit = (tree1d[n] >> (bitlen[n] - i - 1)) & 1;
  158. if(treepos > numcodes - 2) {
  159. return 55;
  160. }
  161. if(tree2d[2 * treepos + bit] == 32767) { //not yet filled in
  162. if(i + 1 == bitlen[n]) {
  163. tree2d[2 * treepos + bit] = n; //last bit
  164. treepos = 0;
  165. } else {
  166. tree2d[2 * treepos + bit] = ++nodefilled + numcodes; //addresses are encoded as values > numcodes
  167. treepos = nodefilled;
  168. }
  169. } else {
  170. treepos = tree2d[2 * treepos + bit] - numcodes; //subtract numcodes from address to get address value
  171. }
  172. }
  173. return 0;
  174. }
  175. int decode(bool& decoded, unsigned long& result, size_t& treepos, unsigned long bit) const {
  176. //Decodes a symbol from the tree
  177. unsigned long numcodes = (unsigned long)tree2d.size() / 2;
  178. if(treepos >= numcodes) {
  179. return 11; //error: you appeared outside the codetree
  180. }
  181. result = tree2d[2 * treepos + bit];
  182. decoded = (result < numcodes);
  183. treepos = decoded ? 0 : result - numcodes;
  184. return 0;
  185. }
  186. std::vector<unsigned long> tree2d; //2D representation of a huffman tree: The one dimension is "0" or "1", the other contains all nodes and leaves of the tree.
  187. };
  188. struct Inflator {
  189. int error;
  190. void inflate(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, size_t inpos = 0) {
  191. size_t bp = 0, pos = 0; //bit pointer and byte pointer
  192. error = 0;
  193. unsigned long BFINAL = 0;
  194. while(!BFINAL && !error) {
  195. if(bp >> 3 >= in.size()) {
  196. error = 52; //error, bit pointer will jump past memory
  197. return;
  198. }
  199. BFINAL = readBitFromStream(bp, &in[inpos]);
  200. unsigned long BTYPE = readBitFromStream(bp, &in[inpos]);
  201. BTYPE += 2 * readBitFromStream(bp, &in[inpos]);
  202. if(BTYPE == 3) {
  203. error = 20; //error: invalid BTYPE
  204. return;
  205. } else if(BTYPE == 0) {
  206. inflateNoCompression(out, &in[inpos], bp, pos, in.size());
  207. } else {
  208. inflateHuffmanBlock(out, &in[inpos], bp, pos, in.size(), BTYPE);
  209. }
  210. }
  211. if(!error) {
  212. out.resize(pos); //Only now we know the true size of out, resize it to that
  213. }
  214. }
  215. void generateFixedTrees(HuffmanTree& tree, HuffmanTree& treeD) { //get the tree of a deflated block with fixed tree
  216. std::vector<unsigned long> bitlen(288, 8), bitlenD(32, 5);;
  217. for(size_t i = 144; i <= 255; i++) {
  218. bitlen[i] = 9;
  219. }
  220. for(size_t i = 256; i <= 279; i++) {
  221. bitlen[i] = 7;
  222. }
  223. tree.makeFromLengths(bitlen, 15);
  224. treeD.makeFromLengths(bitlenD, 15);
  225. }
  226. HuffmanTree codetree, codetreeD, codelengthcodetree; //the code tree for Huffman codes, dist codes, and code length codes
  227. unsigned long huffmanDecodeSymbol(const unsigned char* in, size_t& bp, const HuffmanTree& codetree, size_t inlength) {
  228. //decode a single symbol from given list of bits with given code tree. return value is the symbol
  229. bool decoded;
  230. unsigned long ct;
  231. for(size_t treepos = 0;;) {
  232. if((bp & 0x07) == 0 && (bp >> 3) > inlength) {
  233. error = 10; //error: end reached without endcode
  234. return 0;
  235. }
  236. error = codetree.decode(decoded, ct, treepos, readBitFromStream(bp, in));
  237. if(error) {
  238. return 0; //stop, an error happened
  239. }
  240. if(decoded) {
  241. return ct;
  242. }
  243. }
  244. }
  245. void getTreeInflateDynamic(HuffmanTree& tree, HuffmanTree& treeD, const unsigned char* in, size_t& bp, size_t inlength) {
  246. //get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree
  247. std::vector<unsigned long> bitlen(288, 0), bitlenD(32, 0);
  248. if(bp >> 3 >= inlength - 2) {
  249. error = 49; //the bit pointer is or will go past the memory
  250. return;
  251. }
  252. size_t HLIT = readBitsFromStream(bp, in, 5) + 257; //number of literal/length codes + 257
  253. size_t HDIST = readBitsFromStream(bp, in, 5) + 1; //number of dist codes + 1
  254. size_t HCLEN = readBitsFromStream(bp, in, 4) + 4; //number of code length codes + 4
  255. std::vector<unsigned long> codelengthcode(19); //lengths of tree to decode the lengths of the dynamic tree
  256. for(size_t i = 0; i < 19; i++) {
  257. codelengthcode[CLCL[i]] = (i < HCLEN) ? readBitsFromStream(bp, in, 3) : 0;
  258. }
  259. error = codelengthcodetree.makeFromLengths(codelengthcode, 7);
  260. if(error) {
  261. return;
  262. }
  263. size_t i = 0, replength;
  264. while(i < HLIT + HDIST) {
  265. unsigned long code = huffmanDecodeSymbol(in, bp, codelengthcodetree, inlength);
  266. if(error) {
  267. return;
  268. }
  269. if(code <= 15) {
  270. if(i < HLIT) {
  271. bitlen[i++] = code; //a length code
  272. } else {
  273. bitlenD[i++ - HLIT] = code;
  274. }
  275. } else if(code == 16) { //repeat previous
  276. if(bp >> 3 >= inlength) {
  277. error = 50; //error, bit pointer jumps past memory
  278. return;
  279. }
  280. replength = 3 + readBitsFromStream(bp, in, 2);
  281. unsigned long value; //set value to the previous code
  282. if((i - 1) < HLIT) {
  283. value = bitlen[i - 1];
  284. } else {
  285. value = bitlenD[i - HLIT - 1];
  286. }
  287. for(size_t n = 0; n < replength; n++) { //repeat this value in the next lengths
  288. if(i >= HLIT + HDIST) {
  289. error = 13; //error: i is larger than the amount of codes
  290. return;
  291. }
  292. if(i < HLIT) {
  293. bitlen[i++] = value;
  294. } else {
  295. bitlenD[i++ - HLIT] = value;
  296. }
  297. }
  298. } else if(code == 17) { //repeat "0" 3-10 times
  299. if(bp >> 3 >= inlength) {
  300. error = 50; //error, bit pointer jumps past memory
  301. return;
  302. }
  303. replength = 3 + readBitsFromStream(bp, in, 3);
  304. for(size_t n = 0; n < replength; n++) { //repeat this value in the next lengths
  305. if(i >= HLIT + HDIST) {
  306. error = 14; //error: i is larger than the amount of codes
  307. return;
  308. }
  309. if(i < HLIT) {
  310. bitlen[i++] = 0;
  311. } else {
  312. bitlenD[i++ - HLIT] = 0;
  313. }
  314. }
  315. } else if(code == 18) { //repeat "0" 11-138 times
  316. if(bp >> 3 >= inlength) {
  317. error = 50; //error, bit pointer jumps past memory
  318. return;
  319. }
  320. replength = 11 + readBitsFromStream(bp, in, 7);
  321. for(size_t n = 0; n < replength; n++) { //repeat this value in the next lengths
  322. if(i >= HLIT + HDIST) {
  323. error = 15; //error: i is larger than the amount of codes
  324. return;
  325. }
  326. if(i < HLIT) {
  327. bitlen[i++] = 0;
  328. } else {
  329. bitlenD[i++ - HLIT] = 0;
  330. }
  331. }
  332. } else {
  333. error = 16; //error: somehow an unexisting code appeared. This can never happen.
  334. return;
  335. }
  336. }
  337. if(bitlen[256] == 0) {
  338. error = 64; //the length of the end code 256 must be larger than 0
  339. return;
  340. }
  341. error = tree.makeFromLengths(bitlen, 15);
  342. if(error) {
  343. return; //now we've finally got HLIT and HDIST, so generate the code trees, and the function is done
  344. }
  345. error = treeD.makeFromLengths(bitlenD, 15);
  346. if(error) {
  347. return;
  348. }
  349. }
  350. void inflateHuffmanBlock(std::vector<unsigned char>& out, const unsigned char* in, size_t& bp, size_t& pos, size_t inlength, unsigned long btype) {
  351. if(btype == 1) {
  352. generateFixedTrees(codetree, codetreeD);
  353. } else if(btype == 2) {
  354. getTreeInflateDynamic(codetree, codetreeD, in, bp, inlength);
  355. if(error) {
  356. return;
  357. }
  358. }
  359. for(;;) {
  360. unsigned long code = huffmanDecodeSymbol(in, bp, codetree, inlength);
  361. if(error) {
  362. return;
  363. }
  364. if(code == 256) {
  365. return; //end code
  366. } else if(code <= 255) { //literal symbol
  367. if(pos >= out.size()) {
  368. out.resize((pos + 1) * 2); //reserve more room
  369. }
  370. out[pos++] = (unsigned char)(code);
  371. } else if(code >= 257 && code <= 285) { //length code
  372. size_t length = LENBASE[code - 257], numextrabits = LENEXTRA[code - 257];
  373. if((bp >> 3) >= inlength) {
  374. error = 51; //error, bit pointer will jump past memory
  375. return;
  376. }
  377. length += readBitsFromStream(bp, in, numextrabits);
  378. unsigned long codeD = huffmanDecodeSymbol(in, bp, codetreeD, inlength);
  379. if(error) {
  380. return;
  381. }
  382. if(codeD > 29) {
  383. error = 18; //error: invalid dist code (30-31 are never used)
  384. return;
  385. }
  386. unsigned long dist = DISTBASE[codeD], numextrabitsD = DISTEXTRA[codeD];
  387. if((bp >> 3) >= inlength) {
  388. error = 51; //error, bit pointer will jump past memory
  389. return;
  390. }
  391. dist += readBitsFromStream(bp, in, numextrabitsD);
  392. size_t start = pos, back = start - dist; //backwards
  393. if(pos + length >= out.size()) {
  394. out.resize((pos + length) * 2); //reserve more room
  395. }
  396. for(size_t i = 0; i < length; i++) {
  397. out[pos++] = out[back++];
  398. if(back >= start) {
  399. back = start - dist;
  400. }
  401. }
  402. }
  403. }
  404. }
  405. void inflateNoCompression(std::vector<unsigned char>& out, const unsigned char* in, size_t& bp, size_t& pos, size_t inlength) {
  406. while((bp & 0x7) != 0) {
  407. bp++; //go to first boundary of byte
  408. }
  409. size_t p = bp / 8;
  410. if(p >= inlength - 4) {
  411. error = 52; //error, bit pointer will jump past memory
  412. return;
  413. }
  414. unsigned long LEN = in[p] + 256 * in[p + 1], NLEN = in[p + 2] + 256 * in[p + 3];
  415. p += 4;
  416. if(LEN + NLEN != 65535) {
  417. error = 21; //error: NLEN is not one's complement of LEN
  418. return;
  419. }
  420. if(pos + LEN >= out.size()) {
  421. out.resize(pos + LEN);
  422. }
  423. if(p + LEN > inlength) {
  424. error = 23; //error: reading outside of in buffer
  425. return;
  426. }
  427. for(unsigned long n = 0; n < LEN; n++) {
  428. out[pos++] = in[p++]; //read LEN bytes of literal data
  429. }
  430. bp = p * 8;
  431. }
  432. };
  433. int decompress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in) { //returns error value
  434. Inflator inflator;
  435. if(in.size() < 2) {
  436. return 53; //error, size of zlib data too small
  437. }
  438. if((in[0] * 256 + in[1]) % 31 != 0) {
  439. return 24; //error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way
  440. }
  441. unsigned long CM = in[0] & 15, CINFO = (in[0] >> 4) & 15, FDICT = (in[1] >> 5) & 1;
  442. if(CM != 8 || CINFO > 7) {
  443. return 25; //error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec
  444. }
  445. if(FDICT != 0) {
  446. return 26; //error: the specification of PNG says about the zlib stream: "The additional flags shall not specify a preset dictionary."
  447. }
  448. inflator.inflate(out, in, 2);
  449. return inflator.error; //note: adler32 checksum was skipped and ignored
  450. }
  451. };
  452. struct PNG { //nested functions for PNG decoding
  453. struct Info {
  454. unsigned long width, height, colorType, bitDepth, compressionMethod, filterMethod, interlaceMethod, key_r, key_g, key_b;
  455. bool key_defined; //is a transparent color key given?
  456. std::vector<unsigned char> palette;
  457. } info;
  458. int error;
  459. void decode(std::vector<unsigned char>& out, const unsigned char* in, size_t size, bool convert_to_rgba32) {
  460. error = 0;
  461. if(size == 0 || in == 0) {
  462. error = 48; //the given data is empty
  463. return;
  464. }
  465. readPngHeader(&in[0], size);
  466. if(error) {
  467. return;
  468. }
  469. size_t pos = 33; //first byte of the first chunk after the header
  470. std::vector<unsigned char> idat; //the data from idat chunks
  471. bool IEND = false;
  472. info.key_defined = false;
  473. while(!IEND) { //loop through the chunks, ignoring unknown chunks and stopping at IEND chunk. IDAT data is put at the start of the in buffer
  474. if(pos + 8 >= size) {
  475. error = 30; //error: size of the in buffer too small to contain next chunk
  476. return;
  477. }
  478. size_t chunkLength = read32bitInt(&in[pos]);
  479. pos += 4;
  480. if(chunkLength > 2147483647) {
  481. error = 63;
  482. return;
  483. }
  484. if(pos + chunkLength >= size) {
  485. error = 35; //error: size of the in buffer too small to contain next chunk
  486. return;
  487. }
  488. if(in[pos + 0] == 'I' && in[pos + 1] == 'D' && in[pos + 2] == 'A' && in[pos + 3] == 'T') { //IDAT chunk, containing compressed image data
  489. idat.insert(idat.end(), &in[pos + 4], &in[pos + 4 + chunkLength]);
  490. pos += (4 + chunkLength);
  491. } else if(in[pos + 0] == 'I' && in[pos + 1] == 'E' && in[pos + 2] == 'N' && in[pos + 3] == 'D') {
  492. pos += 4;
  493. IEND = true;
  494. } else if(in[pos + 0] == 'P' && in[pos + 1] == 'L' && in[pos + 2] == 'T' && in[pos + 3] == 'E') { //palette chunk (PLTE)
  495. pos += 4; //go after the 4 letters
  496. info.palette.resize(4 * (chunkLength / 3));
  497. if(info.palette.size() > (4 * 256)) {
  498. error = 38; //error: palette too big
  499. return;
  500. }
  501. for(size_t i = 0; i < info.palette.size(); i += 4) {
  502. for(size_t j = 0; j < 3; j++) {
  503. info.palette[i + j] = in[pos++]; //RGB
  504. }
  505. info.palette[i + 3] = 255; //alpha
  506. }
  507. } else if(in[pos + 0] == 't' && in[pos + 1] == 'R' && in[pos + 2] == 'N' && in[pos + 3] == 'S') { //palette transparency chunk (tRNS)
  508. pos += 4; //go after the 4 letters
  509. if(info.colorType == 3) {
  510. if(4 * chunkLength > info.palette.size()) {
  511. error = 39; //error: more alpha values given than there are palette entries
  512. return;
  513. }
  514. for(size_t i = 0; i < chunkLength; i++) {
  515. info.palette[4 * i + 3] = in[pos++];
  516. }
  517. } else if(info.colorType == 0) {
  518. if(chunkLength != 2) {
  519. error = 40; //error: this chunk must be 2 bytes for greyscale image
  520. return;
  521. }
  522. info.key_defined = 1;
  523. info.key_r = info.key_g = info.key_b = 256 * in[pos] + in[pos + 1];
  524. pos += 2;
  525. } else if(info.colorType == 2) {
  526. if(chunkLength != 6) {
  527. error = 41; //error: this chunk must be 6 bytes for RGB image
  528. return;
  529. }
  530. info.key_defined = 1;
  531. info.key_r = 256 * in[pos] + in[pos + 1];
  532. pos += 2;
  533. info.key_g = 256 * in[pos] + in[pos + 1];
  534. pos += 2;
  535. info.key_b = 256 * in[pos] + in[pos + 1];
  536. pos += 2;
  537. } else {
  538. error = 42; //error: tRNS chunk not allowed for other color models
  539. return;
  540. }
  541. } else { //it's not an implemented chunk type, so ignore it: skip over the data
  542. if(!(in[pos + 0] & 32)) {
  543. error = 69; //error: unknown critical chunk (5th bit of first byte of chunk type is 0)
  544. return;
  545. }
  546. pos += (chunkLength + 4); //skip 4 letters and uninterpreted data of unimplemented chunk
  547. }
  548. pos += 4; //step over CRC (which is ignored)
  549. }
  550. unsigned long bpp = getBpp(info);
  551. std::vector<unsigned char> scanlines(((info.width * (info.height * bpp + 7)) / 8) + info.height); //now the out buffer will be filled
  552. Zlib zlib; //decompress with the Zlib decompressor
  553. error = zlib.decompress(scanlines, idat);
  554. if(error) {
  555. return; //stop if the zlib decompressor returned an error
  556. }
  557. size_t bytewidth = (bpp + 7) / 8, outlength = (info.height * info.width * bpp + 7) / 8;
  558. out.resize(outlength); //time to fill the out buffer
  559. unsigned char* out_ = outlength ? &out[0] : 0; //use a regular pointer to the std::vector for faster code if compiled without optimization
  560. if(info.interlaceMethod == 0) { //no interlace, just filter
  561. size_t linestart = 0, linelength = (info.width * bpp + 7) / 8; //length in bytes of a scanline, excluding the filtertype byte
  562. if(bpp >= 8) //byte per byte
  563. for(unsigned long y = 0; y < info.height; y++) {
  564. unsigned long filterType = scanlines[linestart];
  565. const unsigned char* prevline = (y == 0) ? 0 : &out_[(y - 1) * info.width * bytewidth];
  566. unFilterScanline(&out_[linestart - y], &scanlines[linestart + 1], prevline, bytewidth, filterType, linelength);
  567. if(error) {
  568. return;
  569. }
  570. linestart += (1 + linelength); //go to start of next scanline
  571. }
  572. else { //less than 8 bits per pixel, so fill it up bit per bit
  573. std::vector<unsigned char> templine((info.width * bpp + 7) >> 3); //only used if bpp < 8
  574. for(size_t y = 0, obp = 0; y < info.height; y++) {
  575. unsigned long filterType = scanlines[linestart];
  576. const unsigned char* prevline = (y == 0) ? 0 : &out_[(y - 1) * info.width * bytewidth];
  577. unFilterScanline(&templine[0], &scanlines[linestart + 1], prevline, bytewidth, filterType, linelength);
  578. if(error) {
  579. return;
  580. }
  581. for(size_t bp = 0; bp < info.width * bpp;) {
  582. setBitOfReversedStream(obp, out_, readBitFromReversedStream(bp, &templine[0]));
  583. }
  584. linestart += (1 + linelength); //go to start of next scanline
  585. }
  586. }
  587. } else { //interlaceMethod is 1 (Adam7)
  588. size_t passw[7] = { (info.width + 7) / 8, (info.width + 3) / 8, (info.width + 3) / 4, (info.width + 1) / 4, (info.width + 1) / 2, (info.width + 0) / 2, (info.width + 0) / 1 };
  589. size_t passh[7] = { (info.height + 7) / 8, (info.height + 7) / 8, (info.height + 3) / 8, (info.height + 3) / 4, (info.height + 1) / 4, (info.height + 1) / 2, (info.height + 0) / 2 };
  590. size_t passstart[7] = {0};
  591. size_t pattern[28] = {0,4,0,2,0,1,0,0,0,4,0,2,0,1,8,8,4,4,2,2,1,8,8,8,4,4,2,2}; //values for the adam7 passes
  592. for(int i = 0; i < 6; i++) {
  593. passstart[i + 1] = passstart[i] + passh[i] * ((passw[i] ? 1 : 0) + (passw[i] * bpp + 7) / 8);
  594. }
  595. std::vector<unsigned char> scanlineo((info.width * bpp + 7) / 8), scanlinen((info.width * bpp + 7) / 8); //"old" and "new" scanline
  596. for(int i = 0; i < 7; i++) {
  597. adam7Pass(&out_[0], &scanlinen[0], &scanlineo[0], &scanlines[passstart[i]], info.width, pattern[i], pattern[i + 7], pattern[i + 14], pattern[i + 21], passw[i], passh[i], bpp);
  598. }
  599. }
  600. if(convert_to_rgba32 && (info.colorType != 6 || info.bitDepth != 8)) { //conversion needed
  601. std::vector<unsigned char> data = out;
  602. error = convert(out, &data[0], info, info.width, info.height);
  603. }
  604. }
  605. void readPngHeader(const unsigned char* in, size_t inlength) { //read the information from the header and store it in the Info
  606. if(inlength < 29) {
  607. error = 27; //error: the data length is smaller than the length of the header
  608. return;
  609. }
  610. if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) {
  611. error = 28; //no PNG signature
  612. return;
  613. }
  614. if(in[12] != 'I' || in[13] != 'H' || in[14] != 'D' || in[15] != 'R') {
  615. error = 29; //error: it doesn't start with a IHDR chunk!
  616. return;
  617. }
  618. info.width = read32bitInt(&in[16]);
  619. info.height = read32bitInt(&in[20]);
  620. info.bitDepth = in[24];
  621. info.colorType = in[25];
  622. info.compressionMethod = in[26];
  623. if(in[26] != 0) {
  624. error = 32; //error: only compression method 0 is allowed in the specification
  625. return;
  626. }
  627. info.filterMethod = in[27];
  628. if(in[27] != 0) {
  629. error = 33; //error: only filter method 0 is allowed in the specification
  630. return;
  631. }
  632. info.interlaceMethod = in[28];
  633. if(in[28] > 1) {
  634. error = 34; //error: only interlace methods 0 and 1 exist in the specification
  635. return;
  636. }
  637. error = checkColorValidity(info.colorType, info.bitDepth);
  638. }
  639. void unFilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon, size_t bytewidth, unsigned long filterType, size_t length) {
  640. switch(filterType) {
  641. case 0:
  642. for(size_t i = 0; i < length; i++) {
  643. recon[i] = scanline[i];
  644. }
  645. break;
  646. case 1:
  647. for(size_t i = 0; i < bytewidth; i++) {
  648. recon[i] = scanline[i];
  649. }
  650. for(size_t i = bytewidth; i < length; i++) {
  651. recon[i] = scanline[i] + recon[i - bytewidth];
  652. }
  653. break;
  654. case 2:
  655. if(precon) for(size_t i = 0; i < length; i++) {
  656. recon[i] = scanline[i] + precon[i];
  657. }
  658. else for(size_t i = 0; i < length; i++) {
  659. recon[i] = scanline[i];
  660. }
  661. break;
  662. case 3:
  663. if(precon) {
  664. for(size_t i = 0; i < bytewidth; i++) {
  665. recon[i] = scanline[i] + precon[i] / 2;
  666. }
  667. for(size_t i = bytewidth; i < length; i++) {
  668. recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2);
  669. }
  670. } else {
  671. for(size_t i = 0; i < bytewidth; i++) {
  672. recon[i] = scanline[i];
  673. }
  674. for(size_t i = bytewidth; i < length; i++) {
  675. recon[i] = scanline[i] + recon[i - bytewidth] / 2;
  676. }
  677. }
  678. break;
  679. case 4:
  680. if(precon) {
  681. for(size_t i = 0; i < bytewidth; i++) {
  682. recon[i] = scanline[i] + paethPredictor(0, precon[i], 0);
  683. }
  684. for(size_t i = bytewidth; i < length; i++) {
  685. recon[i] = scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]);
  686. }
  687. } else {
  688. for(size_t i = 0; i < bytewidth; i++) {
  689. recon[i] = scanline[i];
  690. }
  691. for(size_t i = bytewidth; i < length; i++) {
  692. recon[i] = scanline[i] + paethPredictor(recon[i - bytewidth], 0, 0);
  693. }
  694. }
  695. break;
  696. default:
  697. error = 36;
  698. return; //error: unexisting filter type given
  699. }
  700. }
  701. void adam7Pass(unsigned char* out, unsigned char* linen, unsigned char* lineo, const unsigned char* in, unsigned long w, size_t passleft, size_t passtop, size_t spacex, size_t spacey, size_t passw, size_t passh, unsigned long bpp) {
  702. //filter and reposition the pixels into the output when the image is Adam7 interlaced. This function can only do it after the full image is already decoded. The out buffer must have the correct allocated memory size already.
  703. if(passw == 0) {
  704. return;
  705. }
  706. size_t bytewidth = (bpp + 7) / 8, linelength = 1 + ((bpp * passw + 7) / 8);
  707. for(unsigned long y = 0; y < passh; y++) {
  708. unsigned char filterType = in[y * linelength], *prevline = (y == 0) ? 0 : lineo;
  709. unFilterScanline(linen, &in[y * linelength + 1], prevline, bytewidth, filterType, (w * bpp + 7) / 8);
  710. if(error) {
  711. return;
  712. }
  713. if(bpp >= 8) for(size_t i = 0; i < passw; i++) for(size_t b = 0; b < bytewidth; b++) { //b = current byte of this pixel
  714. out[bytewidth * w * (passtop + spacey * y) + bytewidth * (passleft + spacex * i) + b] = linen[bytewidth * i + b];
  715. }
  716. else for(size_t i = 0; i < passw; i++) {
  717. size_t obp = bpp * w * (passtop + spacey * y) + bpp * (passleft + spacex * i), bp = i * bpp;
  718. for(size_t b = 0; b < bpp; b++) {
  719. setBitOfReversedStream(obp, out, readBitFromReversedStream(bp, &linen[0]));
  720. }
  721. }
  722. unsigned char* temp = linen;
  723. linen = lineo;
  724. lineo = temp; //swap the two buffer pointers "line old" and "line new"
  725. }
  726. }
  727. static unsigned long readBitFromReversedStream(size_t& bitp, const unsigned char* bits) {
  728. unsigned long result = (bits[bitp >> 3] >> (7 - (bitp & 0x7))) & 1;
  729. bitp++;
  730. return result;
  731. }
  732. static unsigned long readBitsFromReversedStream(size_t& bitp, const unsigned char* bits, unsigned long nbits) {
  733. unsigned long result = 0;
  734. for(size_t i = nbits - 1; i < nbits; i--) {
  735. result += ((readBitFromReversedStream(bitp, bits)) << i);
  736. }
  737. return result;
  738. }
  739. void setBitOfReversedStream(size_t& bitp, unsigned char* bits, unsigned long bit) {
  740. bits[bitp >> 3] |= (bit << (7 - (bitp & 0x7)));
  741. bitp++;
  742. }
  743. unsigned long read32bitInt(const unsigned char* buffer) {
  744. return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
  745. }
  746. int checkColorValidity(unsigned long colorType, unsigned long bd) { //return type is a LodePNG error code
  747. if((colorType == 2 || colorType == 4 || colorType == 6)) {
  748. if(!(bd == 8 || bd == 16)) {
  749. return 37;
  750. } else {
  751. return 0;
  752. }
  753. } else if(colorType == 0) {
  754. if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) {
  755. return 37;
  756. } else {
  757. return 0;
  758. }
  759. } else if(colorType == 3) {
  760. if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) {
  761. return 37;
  762. } else {
  763. return 0;
  764. }
  765. } else {
  766. return 31; //unexisting color type
  767. }
  768. }
  769. unsigned long getBpp(const Info& info) {
  770. if(info.colorType == 2) {
  771. return (3 * info.bitDepth);
  772. } else if(info.colorType >= 4) {
  773. return (info.colorType - 2) * info.bitDepth;
  774. } else {
  775. return info.bitDepth;
  776. }
  777. }
  778. int convert(std::vector<unsigned char>& out, const unsigned char* in, Info& infoIn, unsigned long w, unsigned long h) {
  779. //converts from any color type to 32-bit. return value = LodePNG error code
  780. size_t numpixels = w * h, bp = 0;
  781. out.resize(numpixels * 4);
  782. unsigned char* out_ = out.empty() ? 0 : &out[0]; //faster if compiled without optimization
  783. if(infoIn.bitDepth == 8 && infoIn.colorType == 0) //greyscale
  784. for(size_t i = 0; i < numpixels; i++) {
  785. out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = in[i];
  786. out_[4 * i + 3] = (infoIn.key_defined && in[i] == infoIn.key_r) ? 0 : 255;
  787. }
  788. else if(infoIn.bitDepth == 8 && infoIn.colorType == 2) //RGB color
  789. for(size_t i = 0; i < numpixels; i++) {
  790. for(size_t c = 0; c < 3; c++) {
  791. out_[4 * i + c] = in[3 * i + c];
  792. }
  793. out_[4 * i + 3] = (infoIn.key_defined == 1 && in[3 * i + 0] == infoIn.key_r && in[3 * i + 1] == infoIn.key_g && in[3 * i + 2] == infoIn.key_b) ? 0 : 255;
  794. }
  795. else if(infoIn.bitDepth == 8 && infoIn.colorType == 3) //indexed color (palette)
  796. for(size_t i = 0; i < numpixels; i++) {
  797. if(4U * in[i] >= infoIn.palette.size()) {
  798. return 46;
  799. }
  800. for(size_t c = 0; c < 4; c++) {
  801. out_[4 * i + c] = infoIn.palette[4 * in[i] + c]; //get rgb colors from the palette
  802. }
  803. }
  804. else if(infoIn.bitDepth == 8 && infoIn.colorType == 4) //greyscale with alpha
  805. for(size_t i = 0; i < numpixels; i++) {
  806. out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = in[2 * i + 0];
  807. out_[4 * i + 3] = in[2 * i + 1];
  808. }
  809. else if(infoIn.bitDepth == 8 && infoIn.colorType == 6) for(size_t i = 0; i < numpixels; i++) for(size_t c = 0; c < 4; c++) {
  810. out_[4 * i + c] = in[4 * i + c]; //RGB with alpha
  811. }
  812. else if(infoIn.bitDepth == 16 && infoIn.colorType == 0) //greyscale
  813. for(size_t i = 0; i < numpixels; i++) {
  814. out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = in[2 * i];
  815. out_[4 * i + 3] = (infoIn.key_defined && 256U * in[i] + in[i + 1] == infoIn.key_r) ? 0 : 255;
  816. }
  817. else if(infoIn.bitDepth == 16 && infoIn.colorType == 2) //RGB color
  818. for(size_t i = 0; i < numpixels; i++) {
  819. for(size_t c = 0; c < 3; c++) {
  820. out_[4 * i + c] = in[6 * i + 2 * c];
  821. }
  822. out_[4 * i + 3] = (infoIn.key_defined && 256U*in[6*i+0]+in[6*i+1] == infoIn.key_r && 256U*in[6*i+2]+in[6*i+3] == infoIn.key_g && 256U*in[6*i+4]+in[6*i+5] == infoIn.key_b) ? 0 : 255;
  823. }
  824. else if(infoIn.bitDepth == 16 && infoIn.colorType == 4) //greyscale with alpha
  825. for(size_t i = 0; i < numpixels; i++) {
  826. out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = in[4 * i]; //most significant byte
  827. out_[4 * i + 3] = in[4 * i + 2];
  828. }
  829. else if(infoIn.bitDepth == 16 && infoIn.colorType == 6) for(size_t i = 0; i < numpixels; i++) for(size_t c = 0; c < 4; c++) {
  830. out_[4 * i + c] = in[8 * i + 2 * c]; //RGB with alpha
  831. }
  832. else if(infoIn.bitDepth < 8 && infoIn.colorType == 0) //greyscale
  833. for(size_t i = 0; i < numpixels; i++) {
  834. unsigned long value = (readBitsFromReversedStream(bp, in, infoIn.bitDepth) * 255) / ((1 << infoIn.bitDepth) - 1); //scale value from 0 to 255
  835. out_[4 * i + 0] = out_[4 * i + 1] = out_[4 * i + 2] = (unsigned char)(value);
  836. out_[4 * i + 3] = (infoIn.key_defined && value && ((1U << infoIn.bitDepth) - 1U) == infoIn.key_r && ((1U << infoIn.bitDepth) - 1U)) ? 0 : 255;
  837. }
  838. else if(infoIn.bitDepth < 8 && infoIn.colorType == 3) //palette
  839. for(size_t i = 0; i < numpixels; i++) {
  840. unsigned long value = readBitsFromReversedStream(bp, in, infoIn.bitDepth);
  841. if(4 * value >= infoIn.palette.size()) {
  842. return 47;
  843. }
  844. for(size_t c = 0; c < 4; c++) {
  845. out_[4 * i + c] = infoIn.palette[4 * value + c]; //get rgb colors from the palette
  846. }
  847. }
  848. return 0;
  849. }
  850. unsigned char paethPredictor(short a, short b, short c) { //Paeth predicter, used by PNG filter type 4
  851. short p = a + b - c, pa = p > a ? (p - a) : (a - p), pb = p > b ? (p - b) : (b - p), pc = p > c ? (p - c) : (c - p);
  852. return (unsigned char)((pa <= pb && pa <= pc) ? a : pb <= pc ? b : c);
  853. }
  854. };
  855. PNG decoder;
  856. decoder.decode(out_image, in_png, in_size, convert_to_rgba32);
  857. image_width = decoder.info.width;
  858. image_height = decoder.info.height;
  859. return decoder.error;
  860. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:21: fatal error: Texture.h: No such file or directory
compilation terminated.
stdout
Standard output is empty