Ejemplo n.º 1
0
        /*
        decodePNG: The picoPNG function, decodes a PNG file buffer in memory, into a raw pixel buffer.
        out_image: output parameter, this will contain the raw pixels after decoding.
        By default the output is 32-bit RGBA color.
        The std::vector is automatically resized to the correct size.
        image_width: output_parameter, this will contain the width of the image in pixels.
        image_height: output_parameter, this will contain the height of the image in pixels.
        in_png: pointer to the buffer of the PNG file in memory. To get it from a file on
        disk, load it and store it in a memory buffer yourself first.
        in_size: size of the input PNG file in bytes.
        convert_to_rgba32: optional parameter, true by default.
        Set to true to get the output in RGBA 32-bit (8 bit per channel) color format
        no matter what color type the original PNG image had. This gives predictable,
        useable data from any random input PNG.
        Set to false to do no color conversion at all. The result then has the same data
        type as the PNG image, which can range from 1 bit to 64 bits per pixel.
        Information about the color type or palette colors are not provided. You need
        to know this information yourself to be able to use the data so this only
        works for trusted PNG files. Use LodePNG instead of picoPNG if you need this information.
        return: 0 if success, not 0 if some error occured.
        */
        int 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)
        {
            
            // picoPNG version 20101224
            // Copyright (c) 2005-2010 Lode Vandevenne
            //
            // This software is provided 'as-is', without any express or implied
            // warranty. In no event will the authors be held liable for any damages
            // arising from the use of this software.
            //
            // Permission is granted to anyone to use this software for any purpose,
            // including commercial applications, and to alter it and redistribute it
            // freely, subject to the following restrictions:
            //
            //     1. The origin of this software must not be misrepresented; you must not
            //     claim that you wrote the original software. If you use this software
            //     in a product, an acknowledgment in the product documentation would be
            //     appreciated but is not required.
            //     2. Altered source versions must be plainly marked as such, and must not be
            //     misrepresented as being the original software.
            //     3. This notice may not be removed or altered from any source distribution.

            // picoPNG is a PNG decoder in one C++ function of around 500 lines. Use picoPNG for
            // programs that need only 1 .cpp file. Since it's a single function, it's very limited,
            // it can convert a PNG to raw pixel data either converted to 32-bit RGBA color or
            // with no color conversion at all. For anything more complex, another tiny library
            // is available: LodePNG (lodepng.c(pp)), which is a single source and header file.
            // Apologies for the compact code style, it's to make this tiny.

            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 };
            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 };
            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 };
            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 };
            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
            struct Zlib //nested functions for zlib decompression
            {
                static unsigned long readBitFromStream(size_t& bitp, const unsigned char* bits)
                {
                    unsigned long result = (bits[bitp >> 3] >> (bitp & 0x7)) & 1;
                    bitp++;
                    return result;
                }

                static unsigned long readBitsFromStream(size_t& bitp, const unsigned char* bits, size_t nbits)
                {
                    unsigned long result = 0;
                    for (size_t i = 0; i < nbits; i++) result += (readBitFromStream(bitp, bits)) << i;
                    return result;
                }
Ejemplo n.º 2
0
int 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 = true)
{
  // picoPNG version 20101224
  // Copyright (c) 2005-2010 Lode Vandevenne
  //
  // This software is provided 'as-is', without any express or implied
  // warranty. In no event will the authors be held liable for any damages
  // arising from the use of this software.
  //
  // Permission is granted to anyone to use this software for any purpose,
  // including commercial applications, and to alter it and redistribute it
  // freely, subject to the following restrictions:
  //
  //     1. The origin of this software must not be misrepresented; you must not
  //     claim that you wrote the original software. If you use this software
  //     in a product, an acknowledgment in the product documentation would be
  //     appreciated but is not required.
  //     2. Altered source versions must be plainly marked as such, and must not be
  //     misrepresented as being the original software.
  //     3. This notice may not be removed or altered from any source distribution.

	/* Modified 16.05.2012 by sic: errors checking changed for loading broken images,
	   extracting as much valid information as possible and cropping any other.
	   Code lines for doing that marked with //sic! comments.
	   */
  
  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};
  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};
  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};
  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};
  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
  struct Zlib //nested functions for zlib decompression
  {
    static unsigned long readBitFromStream(size_t& bitp, const unsigned char* bits) { unsigned long result = (bits[bitp >> 3] >> (bitp & 0x7)) & 1; bitp++; return result;}
    static unsigned long readBitsFromStream(size_t& bitp, const unsigned char* bits, size_t nbits)
    {
      unsigned long result = 0;
      for(size_t i = 0; i < nbits; i++) result += (readBitFromStream(bitp, bits)) << i;
      return result;
    }
Ejemplo n.º 3
0
//--------------------------------------------------
int Texture::decodePNG(std::vector<unsigned char>& out_image_32bit, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, unsigned long in_size)
{
	// picoPNG version 20080503
	// Copyright (c) 2005-2008 Lode Vandevenne
	//
	// This software is provided 'as-is', without any express or implied
	// warranty. In no event will the authors be held liable for any damages
	// arising from the use of this software.
	//
	// Permission is granted to anyone to use this software for any purpose,
	// including commercial applications, and to alter it and redistribute it
	// freely, subject to the following restrictions:
	//
	//     1. The origin of this software must not be misrepresented; you must not
	//     claim that you wrote the original software. If you use this software
	//     in a product, an acknowledgment in the product documentation would be
	//     appreciated but is not required.
	//     2. Altered source versions must be plainly marked as such, and must not be
	//     misrepresented as being the original software.
	//     3. This notice may not be removed or altered from any source distribution.

	// picoPNG is a PNG decoder in one C++ function. Use picoPNG for programs that need
	// only 1 .cpp file. Apologies for the compact code style, it's to make it tiny.


	struct Zlib //nested functions for zlib decompression
	{
		static unsigned long readBitFromStream(size_t& bitp, const unsigned char* bits)
		{
			unsigned long result = (bits[bitp >> 3] >> (bitp & 0x7)) & 1;
			bitp++;
			return result;
		}
		static unsigned long readBitsFromStream(size_t& bitp, const unsigned char* bits, size_t nbits)
		{
			unsigned long result = 0;
			for (size_t i = 0; i < nbits; i++) result += (readBitFromStream(bitp, bits)) << i;
			return result;
		}
Ejemplo n.º 4
0
//int decodePNG(std::vector<unsigned char>& out_image_32bit, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, unsigned long in_size)
int decodePNG(std::vector<unsigned char>& out_image, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, unsigned long in_size)
{
  // picoPNG version 20101224
  // Copyright (c) 2005-2010 Lode Vandevenne
  //
  // This software is provided 'as-is', without any express or implied
  // warranty. In no event will the authors be held liable for any damages
  // arising from the use of this software.
  //
  // Permission is granted to anyone to use this software for any purpose,
  // including commercial applications, and to alter it and redistribute it
  // freely, subject to the following restrictions:
  //
  //     1. The origin of this software must not be misrepresented; you must not
  //     claim that you wrote the original software. If you use this software
  //     in a product, an acknowledgment in the product documentation would be
  //     appreciated but is not required.
  //     2. Altered source versions must be plainly marked as such, and must not be
  //     misrepresented as being the original software.
  //     3. This notice may not be removed or altered from any source distribution.
  
  // picoPNG is a PNG decoder in one C++ function of around 500 lines. Use picoPNG for
  // programs that need only 1 .cpp file. Since it's a single function, it's very limited,
  // it can convert a PNG to raw pixel data either converted to 32-bit RGBA color or
  // with no color conversion at all. For anything more complex, another tiny library
  // is available: LodePNG (lodepng.c(pp)), which is a single source and header file.
  // Apologies for the compact code style, it's to make this tiny.
  
  struct Zlib //nested functions for zlib decompression
  {
    static unsigned long readBitFromStream(size_t& bitp, const unsigned char* bits) { unsigned long result = (bits[bitp >> 3] >> (bitp & 0x7)) & 1; bitp++; return result;}
    static unsigned long readBitsFromStream(size_t& bitp, const unsigned char* bits, size_t nbits)
    {
      unsigned long result = 0;
      for(size_t i = 0; i < nbits; i++) result += (readBitFromStream(bitp, bits)) << i;
      return result;
    }