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