jpegyuv.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <stdio.h>
  34. #include <stdlib.h>
  35. #include "jpeglib.h"
  36. int main(int argc, char *argv[]) {
  37. const char *jpg_path;
  38. const char *yuv_path;
  39. struct jpeg_decompress_struct cinfo;
  40. struct jpeg_error_mgr jerr;
  41. FILE *jpg_fd;
  42. int luma_width;
  43. int luma_height;
  44. int chroma_width;
  45. int chroma_height;
  46. int frame_width;
  47. int yuv_size;
  48. JSAMPLE *jpg_buffer;
  49. JSAMPROW yrow_pointer[16];
  50. JSAMPROW cbrow_pointer[8];
  51. JSAMPROW crrow_pointer[8];
  52. JSAMPROW *plane_pointer[3];
  53. unsigned char *yuv_buffer;
  54. int x;
  55. int y;
  56. FILE *yuv_fd;
  57. if (argc != 3) {
  58. fprintf(stderr, "Required arguments:\n");
  59. fprintf(stderr, "1. Path to JPG input file\n");
  60. fprintf(stderr, "2. Path to YUV output file\n");
  61. return 1;
  62. }
  63. /* Will check these for validity when opening via 'fopen'. */
  64. jpg_path = argv[1];
  65. yuv_path = argv[2];
  66. cinfo.err = jpeg_std_error(&jerr);
  67. jpeg_create_decompress(&cinfo);
  68. jpg_fd = fopen(jpg_path, "rb");
  69. if (!jpg_fd) {
  70. fprintf(stderr, "Invalid path to JPEG file!\n");
  71. return 1;
  72. }
  73. jpeg_stdio_src(&cinfo, jpg_fd);
  74. jpeg_read_header(&cinfo, TRUE);
  75. cinfo.raw_data_out = TRUE;
  76. cinfo.do_fancy_upsampling = FALSE;
  77. jpeg_start_decompress(&cinfo);
  78. luma_width = cinfo.output_width;
  79. luma_height = cinfo.output_height;
  80. chroma_width = (luma_width + 1) >> 1;
  81. chroma_height = (luma_height + 1) >> 1;
  82. yuv_size = luma_width*luma_height + 2*chroma_width*chroma_height;
  83. yuv_buffer = malloc(yuv_size);
  84. if (!yuv_buffer) {
  85. fclose(jpg_fd);
  86. fprintf(stderr, "Memory allocation failure!\n");
  87. return 1;
  88. }
  89. frame_width = (cinfo.output_width + (16 - 1)) & ~(16 - 1);
  90. jpg_buffer = malloc(frame_width*16 + 2*(frame_width/2)*8);
  91. if (!jpg_buffer) {
  92. fclose(jpg_fd);
  93. free(yuv_buffer);
  94. fprintf(stderr, "Memory allocation failure!\n");
  95. return 1;
  96. }
  97. plane_pointer[0] = yrow_pointer;
  98. plane_pointer[1] = cbrow_pointer;
  99. plane_pointer[2] = crrow_pointer;
  100. for (y = 0; y < 16; y++) {
  101. yrow_pointer[y] = &jpg_buffer[frame_width*y];
  102. }
  103. for (y = 0; y < 8; y++) {
  104. cbrow_pointer[y] = &jpg_buffer[frame_width*16 + (frame_width/2)*y];
  105. crrow_pointer[y] = &jpg_buffer[frame_width*16 + (frame_width/2)*(8 + y)];
  106. }
  107. while (cinfo.output_scanline < cinfo.output_height) {
  108. int luma_scanline;
  109. int chroma_scanline;
  110. luma_scanline = cinfo.output_scanline;
  111. chroma_scanline = (luma_scanline + 1) >> 1;
  112. jpeg_read_raw_data(&cinfo, plane_pointer, 16);
  113. for (y = 0; y < 16 && luma_scanline + y < luma_height; y++) {
  114. for (x = 0; x < luma_width; x++) {
  115. yuv_buffer[luma_width*(luma_scanline + y) + x] = yrow_pointer[y][x];
  116. }
  117. }
  118. for (y = 0; y < 8 && chroma_scanline + y < chroma_height; y++) {
  119. for (x = 0; x < chroma_width; x++) {
  120. yuv_buffer[luma_width*luma_height +
  121. chroma_width*(chroma_scanline + y) + x] = cbrow_pointer[y][x];
  122. yuv_buffer[luma_width*luma_height + chroma_width*chroma_height +
  123. chroma_width*(chroma_scanline + y) + x] = crrow_pointer[y][x];
  124. }
  125. }
  126. }
  127. jpeg_finish_decompress(&cinfo);
  128. jpeg_destroy_decompress(&cinfo);
  129. fclose(jpg_fd);
  130. free(jpg_buffer);
  131. yuv_fd = fopen(yuv_path, "wb");
  132. if (!yuv_fd) {
  133. fprintf(stderr, "Invalid path to YUV file!");
  134. free(yuv_buffer);
  135. return 1;
  136. }
  137. if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
  138. fprintf(stderr, "Error writing yuv file\n");
  139. }
  140. fclose(yuv_fd);
  141. free(yuv_buffer);
  142. return 0;
  143. }