webpimg.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*===========================================================================*
  2. - Copyright 2010 Google Inc.
  3. -
  4. - This code is licensed under the same terms as WebM:
  5. - Software License Agreement: http://www.webmproject.org/license/software/
  6. - Additional IP Rights Grant: http://www.webmproject.org/license/additional/
  7. *===========================================================================*/
  8. /*
  9. * Encoding/Decoding of WebP still image compression format.
  10. *
  11. * 1. WebPDecode: Takes an array of bytes (string) corresponding to the WebP
  12. * encoded image and generates output in the YUV format with
  13. * the color components U, V subsampled to 1/2 resolution along
  14. * each dimension.
  15. *
  16. * 2. YUV420toRGBA: Converts from YUV (with color subsampling) such as produced
  17. * by the WebPDecode routine into 32 bits per pixel RGBA data
  18. * array. This data array can be directly used by the Leptonica
  19. * Pix in-memory image format.
  20. *
  21. * 3. WebPEncode: Takes a Y, U, V data buffers (with color components U and V
  22. * subsampled to 1/2 resolution) and generates the WebP string
  23. *
  24. * 4. RGBAToYUV420: Generates Y, U, V data (with color subsampling) from 32 bits
  25. * per pixel RGBA data buffer. The resulting YUV data can be
  26. * directly fed into the WebPEncode routine.
  27. *
  28. * 5. AdjustColorspace:
  29. *
  30. * 6. AdjustColorspaceBack:
  31. */
  32. #ifndef THIRD_PARTY_VP8_VP8IMG_H_
  33. #define THIRD_PARTY_VP8_VP8IMG_H_
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif /* __cplusplus */
  37. typedef unsigned char uint8;
  38. typedef unsigned int uint32;
  39. typedef enum WebPResultType {
  40. webp_success = 0,
  41. webp_failure = -1
  42. } WebPResult;
  43. /* Takes an array of bytes (string) corresponding to the WebP
  44. * encoded image and generates output in the YUV format with
  45. * the color components U, V subsampled to 1/2 resolution along
  46. * each dimension.
  47. * Input:
  48. * 1. data: the WebP data stream (array of bytes)
  49. * 2. data_size: count of bytes in the WebP data stream
  50. *
  51. * Output:
  52. * 3. p_Y/p_U/p_V : pointer to the Y/U/V data buffer (this routine will
  53. * allocate memory for the buffer, fill the buffer with
  54. * appropriate data and transfer owner ship of the buffer
  55. * to caller. Caller is responsible for freeing the memory).
  56. * Note that the memory for Y, U, V buffers is alloacted
  57. * in one chunk, hence one should call free(*p_Y) only.
  58. * Do not try to free the U and V buffers.
  59. *
  60. * 6. p_width: this routine returns the width of the decoded image here
  61. * 7. p_height: this routine returns the width of the decoded image here
  62. * Return: success/failure
  63. */
  64. WebPResult WebPDecode(const uint8* data,
  65. int data_size,
  66. uint8** p_Y,
  67. uint8** p_U,
  68. uint8** p_V,
  69. int* p_width,
  70. int* p_height);
  71. /* WebPEncode: Takes a Y, U, V data buffers (with color components U and V
  72. * subsampled to 1/2 resolution) and generates the WebP string.
  73. * Input:
  74. * 1, 2, 3. Y, U, V: The input YUV data buffers
  75. * 4, 5. y_width, y_height: The width and height of the image whose data
  76. * is in Y, U, V. This matches the Y plane. The U
  77. * and V planes typically have 1/2 width and
  78. * height.
  79. * 6. y_stride: The width (in bytes) of one row of Y data. This may not
  80. * match width if there is end of row padding (e.g., for 32
  81. * bit row alignment).
  82. * 7. QP: the quantization parameter. This parameter controls the
  83. * compression vs quality tradeoff. Use smaller numbers for better
  84. * quality (compression will be lesser) and vice versa. 20 is a
  85. * good optimal value.
  86. * Output:
  87. * 8. p_out: the output array of bytes corresponding to the encoded WebP
  88. * image. This routine allocates memory for the buffer, fills it
  89. * with appropriate values and transfers ownership to caller.
  90. * Caller responsible for freeing of memory.
  91. * Return: success/failure
  92. */
  93. WebPResult WebPEncode(const uint8* Y,
  94. const uint8* U,
  95. const uint8* V,
  96. int y_width,
  97. int y_height,
  98. int y_stride,
  99. int uv_width,
  100. int uv_height,
  101. int uv_stride,
  102. int QP,
  103. unsigned char** p_out,
  104. int* p_out_size_bytes,
  105. double* psnr);
  106. /* Converts from YUV (with color subsampling) such as produced by the WebPDecode
  107. * routine into 32 bits per pixel RGBA data array. This data array can be
  108. * directly used by the Leptonica Pix in-memory image format.
  109. * Input:
  110. * 1, 2, 3. Y, U, V: the input data buffers
  111. * 4. pixwpl: the desired words per line corresponding to the supplied
  112. * output pixdata.
  113. * 5. width, height: the dimensions of the image whose data resides in Y,
  114. * U, V.
  115. * Output:
  116. * 6. pixdata: the output data buffer. Caller should allocate
  117. * height * pixwpl bytes of memory before calling this routine.
  118. */
  119. void YUV420toRGBA(uint8* Y,
  120. uint8* U,
  121. uint8* V,
  122. int words_per_line,
  123. int width,
  124. int height,
  125. uint32* pixdata);
  126. /* Generates Y, U, V data (with color subsampling) from 32 bits
  127. * per pixel RGBA data buffer. The resulting YUV data can be directly fed into
  128. * the WebPEncode routine.
  129. * Input:
  130. * 1. pix data input rgba data buffer
  131. * 2. words per line corresponding to pixdata
  132. * 3, 4. image width and height respectively
  133. * Output:
  134. * 5, 6, 7. Output YUV data buffers
  135. */
  136. void RGBAToYUV420(uint32* pixdata,
  137. int words_per_line,
  138. int width,
  139. int height,
  140. uint8* Y,
  141. uint8* U,
  142. uint8* V);
  143. /* This function adjust from YUV420J (jpeg decoding) to YUV420 (webp input)
  144. * Hints: http://en.wikipedia.org/wiki/YCbCr
  145. */
  146. void AdjustColorspace(uint8* Y, uint8* U, uint8* V, int width, int height);
  147. /* Inverse function: convert from YUV420 to YUV420J */
  148. void AdjustColorspaceBack(uint8* Y, uint8* U, uint8* V, int width, int height);
  149. /* Checks WebP image header and outputs height and width information of
  150. * the image
  151. *
  152. * Input:
  153. * 1. data: the WebP data stream (array of bytes)
  154. * 2. data_size: count of bytes in the WebP data stream
  155. *
  156. * Outut:
  157. * width/height: width and height of the image
  158. *
  159. * Return: success/failure
  160. */
  161. WebPResult WebPGetInfo(const uint8* data,
  162. int data_size,
  163. int *width,
  164. int *height);
  165. #ifdef __cplusplus
  166. }
  167. #endif /* __cplusplus */
  168. #endif /* THIRD_PARTY_VP8_VP8IMG_H_ */