github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/libwebp/demo/demo.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  // https://docs.google.com/document/d/1KzP6nWeVsobU1AtTHbchdrmA-_UoNLJGEPgZikHAzVg/pub
     6  
     7  #include <stdio.h>
     8  #include <stdlib.h>
     9  #include <string.h>
    10  #include <webp/types.h>
    11  #include <webp/encode.h>
    12  #include <webp/decode.h>
    13  
    14  uint8_t interpolate(float v0, float v1, float x) {
    15  	return (uint8_t)(v0 + x * (v1 - v0));
    16  }
    17  
    18  int webpWriter(const uint8_t* data, size_t data_size, const WebPPicture* const pic) {
    19  	FILE* const out = (FILE*)pic->custom_ptr;
    20  	return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
    21  }
    22  
    23  int WebPImportGray(const uint8_t* gray_data, WebPPicture* pic) {
    24  	int y, width, uv_width;
    25  	if (pic == NULL || gray_data == NULL) return 0;
    26  #if defined(WEBP_EXPERIMENTAL_FEATURES)
    27  	pic->colorspace = WEBP_YUV400;
    28  #else
    29  	pic->colorspace = WEBP_YUV420;
    30  #endif
    31  	if (!WebPPictureAlloc(pic)) {
    32  		return 0;
    33  	}
    34  	width = pic->width;
    35  	uv_width = (width + 1) >> 1;
    36  	for(y = 0; y < pic->height; ++y) {
    37  		memcpy(pic->y + y * pic->y_stride, gray_data, width);
    38  		gray_data += width;
    39  #if !defined(WEBP_EXPERIMENTAL_FEATURES)
    40  		// WEBP_YUV420
    41  		if((y & 1) == 0) {
    42  			memset(pic->u + (y >> 1) * pic->uv_stride, 128, uv_width);
    43  			memset(pic->v + (y >> 1) * pic->uv_stride, 128, uv_width);
    44  		}
    45  #endif
    46  	}
    47  	return 1;
    48  }
    49  
    50  int main() {
    51  	int ret = 0;
    52  	unsigned int width = 640;
    53  	unsigned int height = 480;
    54  	unsigned int data_size = width*height;
    55  	uint8_t *gray_data = (uint8_t*)malloc(data_size*sizeof(uint8_t));
    56  	int i = 0;
    57  	int h = 0;
    58  	int w = 0;
    59  
    60  	for(h = 0; h < height; h++) {
    61  		for(w = 0; w < width; w++) {
    62  			gray_data[i] = interpolate(0, 255, ((float)(w))/((float)(width)));
    63  			i++;
    64  		}
    65  	}
    66  
    67  	WebPPicture pic;
    68  	WebPPictureInit(&pic);
    69  	pic.writer = webpWriter;
    70  	pic.width = width;
    71  	pic.height = height;
    72  
    73  	pic.custom_ptr = fopen("output.webp", "wb");
    74  	if(!WebPImportGray(gray_data, &pic)) {
    75  		printf("ERR: WebPImportGray failed!\n");
    76  		return -1;
    77  	}
    78  	free(gray_data);
    79  
    80  	WebPConfig config;
    81  	WebPConfigInit(&config);
    82  	WebPEncode(&config, &pic);
    83  
    84  	WebPPictureFree(&pic);
    85  
    86  	uint8_t* rgb = (uint8_t*)malloc(width*height*3);
    87  	uint8_t* output;
    88  	size_t output_size;
    89  	output_size = WebPEncodeLosslessRGB(rgb, width, height, width*3, &output);
    90  
    91  	int new_w, new_h;
    92  	if(!WebPGetInfo(output, output_size, &new_w, &new_h)) {
    93  		printf("ERR: WebPImportGray failed!\n");
    94  		return -1;
    95  	}
    96  	printf("WebPGetInfo: width = %d, height = %d\n", new_w, new_h);
    97  
    98  	return 0;
    99  }