jpegyuv.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Written by Josh Aas and Tim Terriberry
  3. * Copyright (c) 2013, Mozilla Corporation
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. Neither the name of the Mozilla Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived from this
  16. * software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /* Input: JPEG YUV 4:2:0 */
  31. /* Output: YUV 4:2:0 */
  32. /* gcc -std=c99 jpegyuv.c -I/opt/local/include/ -L/opt/local/lib/ -ljpeg -o jpegyuv */
  33. #include <errno.h>
  34. #include <stdio.h>
  35. #include <inttypes.h>
  36. #include <sys/stat.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include "jpeglib.h"
  40. int main(int argc, char *argv[]) {
  41. const char *jpg_path;
  42. const char *yuv_path;
  43. struct jpeg_decompress_struct cinfo;
  44. struct jpeg_error_mgr jerr;
  45. FILE *jpg_fd;
  46. int luma_width;
  47. int luma_height;
  48. int chroma_width;
  49. int chroma_height;
  50. int frame_width;
  51. int yuv_size;
  52. JSAMPLE *image_buffer;
  53. JSAMPROW yrow_pointer[16];
  54. JSAMPROW cbrow_pointer[8];
  55. JSAMPROW crrow_pointer[8];
  56. JSAMPROW *plane_pointer[3];
  57. unsigned char *yuv_buffer;
  58. int x;
  59. int y;
  60. FILE *yuv_fd;
  61. if (argc != 3) {
  62. fprintf(stderr, "Required arguments:\n");
  63. fprintf(stderr, "1. Path to JPG input file\n");
  64. fprintf(stderr, "2. Path to YUV output file\n");
  65. return 1;
  66. }
  67. errno = 0;
  68. /* Will check these for validity when opening via 'fopen'. */
  69. jpg_path = argv[1];
  70. yuv_path = argv[2];
  71. cinfo.err = jpeg_std_error(&jerr);
  72. jpeg_create_decompress(&cinfo);
  73. jpg_fd = fopen(jpg_path, "rb");
  74. if (!jpg_fd) {
  75. fprintf(stderr, "Invalid path to JPEG file!\n");
  76. return 1;
  77. }
  78. jpeg_stdio_src(&cinfo, jpg_fd);
  79. jpeg_read_header(&cinfo, TRUE);
  80. cinfo.raw_data_out = TRUE;
  81. cinfo.do_fancy_upsampling = FALSE;
  82. jpeg_start_decompress(&cinfo);
  83. luma_width = cinfo.output_width;
  84. luma_height = cinfo.output_height;
  85. chroma_width = (luma_width + 1) >> 1;
  86. chroma_height = (luma_height + 1) >> 1;
  87. yuv_size = luma_width*luma_height + 2*chroma_width*chroma_height;
  88. yuv_buffer = (unsigned char *)malloc(yuv_size);
  89. frame_width = (cinfo.output_width + (16 - 1)) & ~(16 - 1);
  90. image_buffer = (JSAMPLE *)malloc(frame_width*16 + 2*(frame_width/2)*8);
  91. plane_pointer[0] = yrow_pointer;
  92. plane_pointer[1] = cbrow_pointer;
  93. plane_pointer[2] = crrow_pointer;
  94. for (y = 0; y < 16; y++) {
  95. yrow_pointer[y] = &image_buffer[frame_width*y];
  96. }
  97. for (y = 0; y < 8; y++) {
  98. cbrow_pointer[y] = &image_buffer[frame_width*16 + (frame_width/2)*y];
  99. crrow_pointer[y] = &image_buffer[frame_width*16 + (frame_width/2)*(8 + y)];
  100. }
  101. while (cinfo.output_scanline < cinfo.output_height) {
  102. int luma_scanline;
  103. int chroma_scanline;
  104. luma_scanline = cinfo.output_scanline;
  105. chroma_scanline = (luma_scanline + 1) >> 1;
  106. jpeg_read_raw_data(&cinfo, plane_pointer, 16);
  107. for (y = 0; y < 16 && luma_scanline + y < luma_height; y++) {
  108. for (x = 0; x < luma_width; x++) {
  109. yuv_buffer[luma_width*(luma_scanline + y) + x] = yrow_pointer[y][x];
  110. }
  111. }
  112. for (y = 0; y < 8 && chroma_scanline + y < chroma_height; y++) {
  113. for (x = 0; x < chroma_width; x++) {
  114. yuv_buffer[luma_width*luma_height +
  115. chroma_width*(chroma_scanline + y) + x] = cbrow_pointer[y][x];
  116. yuv_buffer[luma_width*luma_height + chroma_width*chroma_height +
  117. chroma_width*(chroma_scanline + y) + x] = crrow_pointer[y][x];
  118. }
  119. }
  120. }
  121. jpeg_finish_decompress(&cinfo);
  122. jpeg_destroy_decompress(&cinfo);
  123. fclose(jpg_fd);
  124. free(image_buffer);
  125. yuv_fd = fopen(yuv_path, "wb");
  126. if (!yuv_fd) {
  127. fprintf(stderr, "Invalid path to YUV file!");
  128. return 1;
  129. }
  130. if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
  131. fprintf(stderr, "Error writing yuv file\n");
  132. }
  133. fclose(yuv_fd);
  134. free(yuv_buffer);
  135. return 0;
  136. }