github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/jxr/jxrlib/test/jxr_test.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.h" 6 #include "test_util.h" 7 8 #include "jxr.h" 9 10 struct tImgInfo { 11 int width; 12 int height; 13 int channels; 14 int depth; 15 const char* name; 16 }; 17 18 static tImgInfo testCaseJpg[] = { 19 { 512 , 512 , 3, 8, "testdata/lena.jpg" }, 20 { 512 , 512 , 3, 8, "testdata/lena-gray.jpg" }, 21 { 2592, 3904, 3, 8, "testdata/FLOWER.jpg" }, 22 { 3888, 2592, 3, 8, "testdata/SAKURA.jpg" }, 23 { 3888, 2592, 3, 8, "testdata/SMALLTOMATO.jpg" }, 24 }; 25 26 static tImgInfo testCaseJxr[] = { 27 { 512 , 512 , 3, 8, "testdata/lena.wdp" }, 28 { 512 , 512 , 3, 8, "testdata/lena-gray.wdp" }, 29 { 2592, 3904, 3, 8, "testdata/FLOWER.wdp" }, 30 { 3888, 2592, 3, 8, "testdata/SAKURA.wdp" }, 31 { 3888, 2592, 3, 8, "testdata/SMALLTOMATO.wdp" }, 32 }; 33 34 TEST(jxr, JpgHelper) { 35 auto buf = new std::string; 36 auto src = new std::string; 37 auto dst = new std::string; 38 39 for(int i = 0; i < TEST_DIM(testCaseJpg); ++i) { 40 bool rv = loadImageData(testCaseJpg[i].name, buf); 41 ASSERT_TRUE(rv); 42 43 // decode raw file data 44 int width, height, channels; 45 rv = jpegDecode(src, buf->data(), buf->size(), &width, &height, &channels); 46 ASSERT_TRUE(rv); 47 ASSERT_TRUE(width == testCaseJpg[i].width); 48 ASSERT_TRUE(height == testCaseJpg[i].height); 49 ASSERT_TRUE(channels == testCaseJpg[i].channels); 50 51 // encode as jpg 52 buf->clear(); 53 rv = jpegEncode(buf, src->data(), src->size(), width, height, channels, 90, 0); 54 ASSERT_TRUE(rv); 55 56 // decode again 57 rv = jpegDecode(dst, buf->data(), buf->size(), &width, &height, &channels); 58 ASSERT_TRUE(rv); 59 ASSERT_TRUE(width == testCaseJpg[i].width); 60 ASSERT_TRUE(height == testCaseJpg[i].height); 61 ASSERT_TRUE(channels == testCaseJpg[i].channels); 62 63 // compare 64 double diff = diffImageData( 65 (const unsigned char*)src->data(), (const unsigned char*)dst->data(), 66 width, height, channels 67 ); 68 ASSERT_TRUE(diff < 20); 69 } 70 71 delete buf; 72 delete src; 73 delete dst; 74 } 75 76 TEST(jxr, DecodeConfig) { 77 auto buf = new std::string; 78 for(int i = 0; i < TEST_DIM(testCaseJxr); ++i) { 79 bool rv = loadImageData(testCaseJxr[i].name, buf); 80 ASSERT_TRUE(rv); 81 82 // decode jxr data 83 int width, height, channels, depth; 84 jxr_data_type_t type; 85 jxr_bool_t ret = jxr_decode_config(buf->data(), buf->size(), 86 &width, &height, &channels, &depth, &type 87 ); 88 ASSERT_TRUE(ret == jxr_true); 89 ASSERT_TRUE(width == testCaseJxr[i].width); 90 ASSERT_TRUE(height == testCaseJxr[i].height); 91 ASSERT_TRUE(channels == testCaseJxr[i].channels); 92 ASSERT_TRUE(depth == testCaseJxr[i].depth); 93 } 94 delete buf; 95 } 96 97 TEST(jxr, Decode) { 98 auto buf = new std::string; 99 auto src = new std::string; 100 auto dst = new std::string; 101 102 for(int i = 0; i < TEST_DIM(testCaseJxr); ++i) { 103 int width, height, channels, depth; 104 jxr_data_type_t type; 105 106 // decode jxr data 107 bool rv = loadImageData(testCaseJxr[i].name, buf); 108 ASSERT_TRUE(rv); 109 src->resize(testCaseJxr[i].width*testCaseJxr[i].height*testCaseJxr[i].channels); 110 int n = jxr_decode( 111 (char*)src->data(), src->size(), 0, buf->data(), buf->size(), 112 &width, &height, &channels, &depth, &type 113 ); 114 ASSERT_TRUE(n == jxr_true); 115 ASSERT_TRUE(width == testCaseJxr[i].width); 116 ASSERT_TRUE(height == testCaseJxr[i].height); 117 ASSERT_TRUE(channels == testCaseJxr[i].channels); 118 ASSERT_TRUE(depth == testCaseJxr[i].depth); 119 120 // decode jpg data 121 rv = loadImageData(testCaseJpg[i].name, buf); 122 ASSERT_TRUE(rv); 123 rv = jpegDecode(dst, buf->data(), buf->size(), &width, &height, &channels); 124 ASSERT_TRUE(rv); 125 ASSERT_TRUE(width == testCaseJpg[i].width); 126 ASSERT_TRUE(height == testCaseJpg[i].height); 127 ASSERT_TRUE(channels == testCaseJpg[i].channels); 128 129 // compare 130 double diff = diffImageData( 131 (const unsigned char*)src->data(), (const unsigned char*)dst->data(), 132 width, height, channels 133 ); 134 ASSERT_TRUE(diff < 20); 135 } 136 137 delete buf; 138 delete src; 139 delete dst; 140 } 141 142 TEST(jxr, Encode) { 143 // 144 } 145 146 TEST(jxr, DecodeAndEncode) { 147 return; // skip 148 149 auto buf = new std::string; 150 auto src = new std::string; 151 auto dst = new std::string; 152 153 for(int i = 0; i < TEST_DIM(testCaseJxr); ++i) { 154 int width, height, channels, depth; 155 jxr_data_type_t type; 156 int newSize; 157 158 // decode raw file data 159 bool rv = loadImageData(testCaseJxr[i].name, buf); 160 ASSERT_TRUE(rv); 161 src->resize(testCaseJxr[i].width*testCaseJxr[i].height*testCaseJxr[i].channels); 162 int n = jxr_decode( 163 (char*)src->data(), src->size(), 0, buf->data(), buf->size(), 164 &width, &height, &channels, &depth, &type 165 ); 166 ASSERT_TRUE(n == jxr_true); 167 ASSERT_TRUE(width == testCaseJxr[i].width); 168 ASSERT_TRUE(height == testCaseJxr[i].height); 169 ASSERT_TRUE(channels == testCaseJxr[i].channels); 170 ASSERT_TRUE(depth == testCaseJxr[i].depth); 171 172 // encode as jxr 173 buf->clear(); 174 buf->resize(src->size()); 175 n = jxr_encode( 176 (char*)buf->data(), buf->size(), src->data(), src->size(), 0, 177 width, height, channels, depth, 178 90, jxr_unsigned, 179 &newSize 180 ); 181 ASSERT_TRUE(n == jxr_true); 182 ASSERT_TRUE(newSize > 0); 183 184 // decode again 185 dst->resize(testCaseJxr[i].width*testCaseJxr[i].height*testCaseJxr[i].channels); 186 n = jxr_decode( 187 (char*)dst->data(), dst->size(), 0, buf->data(), newSize, 188 &width, &height, &channels, &depth, &type 189 ); 190 ASSERT_TRUE(n == jxr_true); 191 ASSERT_TRUE(width == testCaseJxr[i].width); 192 ASSERT_TRUE(height == testCaseJxr[i].height); 193 ASSERT_TRUE(channels == testCaseJxr[i].channels); 194 ASSERT_TRUE(depth == testCaseJxr[i].depth); 195 196 // compare 197 if(depth == 8) { 198 double diff = diffImageData( 199 (const unsigned char*)src->data(), (const unsigned char*)dst->data(), 200 width, height, channels 201 ); 202 ASSERT_TRUE(diff < 20); 203 } else if(depth == 16) { 204 double diff = diffImageData( 205 (const unsigned short*)src->data(), (const unsigned short*)dst->data(), 206 width, height, channels 207 ); 208 ASSERT_TRUE(diff < 20); 209 } else { 210 ASSERT_TRUE(false); 211 } 212 } 213 214 delete buf; 215 delete src; 216 delete dst; 217 } 218 219 TEST(jxr, CompareJxrJpg) { 220 // diff(jxr, jpg) < 20 221 } 222 223 // benchmark