github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/libwebp/test/test_util_png.cc (about) 1 // Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 #include "test_util.h" 6 7 #include "./png/lodepng.h" 8 #include "./png/lodepng.cpp" 9 10 #include <string> 11 12 bool DecodePng32( 13 std::string* dst, const char* data, int size, 14 int* width, int* height 15 ) { 16 if(dst == NULL || data == NULL || size <= 0) { 17 return false; 18 } 19 if(width == NULL || height == NULL) { 20 return false; 21 } 22 23 unsigned char* img; 24 unsigned w, h; 25 26 auto err = lodepng_decode32(&img, &w, &h, (const unsigned char*)data, size); 27 if(err != 0) return false; 28 29 dst->assign((const char*)img, w*h*4); 30 free(img); 31 32 *width = w; 33 *height = h; 34 return true; 35 } 36 37 bool DecodePng24( 38 std::string* dst, const char* data, int size, 39 int* width, int* height 40 ) { 41 if(dst == NULL || data == NULL || size <= 0) { 42 return false; 43 } 44 if(width == NULL || height == NULL) { 45 return false; 46 } 47 48 unsigned char* img; 49 unsigned w, h; 50 51 auto err = lodepng_decode32(&img, &w, &h, (const unsigned char*)data, size); 52 if(err != 0) return false; 53 54 dst->assign((const char*)img, w*h*3); 55 free(img); 56 57 *width = w; 58 *height = h; 59 return true; 60 } 61 62 bool EncodePng32( 63 std::string* dst, const char* data, int size, 64 int width, int height, int width_step /*=0*/ 65 ) { 66 if(dst == NULL || data == NULL || size <= 0) { 67 return false; 68 } 69 if(width <= 0 || height <= 0) { 70 return false; 71 } 72 73 if(width_step < width*4) { 74 width_step = width*4; 75 } 76 77 std::string tmp; 78 auto pSrcData = data; 79 80 if(width_step > width*4) { 81 tmp.resize(width*height*4); 82 for(int i = 0; i < height; ++i) { 83 auto ppTmp = (char*)tmp.data() + i*width*4; 84 auto ppSrc = (char*)data + i*width_step; 85 memcpy(ppTmp, ppSrc, width*4); 86 } 87 pSrcData = tmp.data(); 88 } 89 90 unsigned char* png; 91 size_t pngsize; 92 93 auto err = lodepng_encode32(&png, &pngsize, (const unsigned char*)pSrcData, width, height); 94 if(err != 0) return false; 95 96 dst->assign((const char*)png, pngsize); 97 free(png); 98 99 return true; 100 } 101 102 bool EncodePng24( 103 std::string* dst, const char* data, int size, 104 int width, int height, int width_step /*=0*/ 105 ) { 106 if(dst == NULL || data == NULL || size <= 0) { 107 return false; 108 } 109 if(width <= 0 || height <= 0) { 110 return false; 111 } 112 113 if(width_step < width*3) { 114 width_step = width*3; 115 } 116 117 std::string tmp; 118 auto pSrcData = data; 119 120 if(width_step > width*3) { 121 tmp.resize(width*height*3); 122 for(int i = 0; i < height; ++i) { 123 auto ppTmp = (char*)tmp.data() + i*width*3; 124 auto ppSrc = (char*)data + i*width_step; 125 memcpy(ppTmp, ppSrc, width*3); 126 } 127 pSrcData = tmp.data(); 128 } 129 130 unsigned char* png; 131 size_t pngsize; 132 133 auto err = lodepng_encode24(&png, &pngsize, (const unsigned char*)pSrcData, width, height); 134 if(err != 0) return false; 135 136 dst->assign((const char*)png, pngsize); 137 free(png); 138 139 return true; 140 }