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