| 1 |
/* |
| 2 |
Copyright (c) 2005-2023 Intel Corporation |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* |
| 18 |
The original source for this example is |
| 19 |
Copyright (c) 1994-2008 John E. Stone |
| 20 |
All rights reserved. |
| 21 |
|
| 22 |
Redistribution and use in source and binary forms, with or without |
| 23 |
modification, are permitted provided that the following conditions |
| 24 |
are met: |
| 25 |
1. Redistributions of source code must retain the above copyright |
| 26 |
notice, this list of conditions and the following disclaimer. |
| 27 |
2. Redistributions in binary form must reproduce the above copyright |
| 28 |
notice, this list of conditions and the following disclaimer in the |
| 29 |
documentation and/or other materials provided with the distribution. |
| 30 |
3. The name of the author may not be used to endorse or promote products |
| 31 |
derived from this software without specific prior written permission. |
| 32 |
|
| 33 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 34 |
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 35 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 36 |
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 37 |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 38 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 39 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 40 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 41 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 42 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 43 |
SUCH DAMAGE. |
| 44 |
*/ |
| 45 |
|
| 46 |
/* |
| 47 |
* jpeg.cpp - This file deals with JPEG format image files (reading/writing) |
| 48 |
*/ |
| 49 |
|
| 50 |
/* |
| 51 |
* This code requires support from the Independent JPEG Group's libjpeg. |
| 52 |
* For our purposes, we're interested only in the 3 byte per pixel 24 bit |
| 53 |
* RGB output. Probably won't implement any decent checking at this point. |
| 54 |
*/ |
| 55 |
|
| 56 |
#include <cstdio> |
| 57 |
|
| 58 |
#include "machine.hpp" |
| 59 |
#include "types.hpp" |
| 60 |
#include "util.hpp" |
| 61 |
#include "imageio.hpp" /* error codes etc */ |
| 62 |
#include "jpeg.hpp" /* the protos for this file */ |
| 63 |
|
| 64 |
#if !defined(USEJPEG) |
| 65 |
|
| 66 |
int readjpeg(char *name, int *xres, int *yres, unsigned char **imgdata) { |
| 67 |
return IMAGEUNSUP; |
| 68 |
} |
| 69 |
|
| 70 |
#else |
| 71 |
|
| 72 |
#include "jpeglib.hpp" /* the IJG jpeg library headers */ |
| 73 |
|
| 74 |
int readjpeg(char *name, int *xres, int *yres, unsigned char **imgdata) { |
| 75 |
FILE *ifp; |
| 76 |
struct jpeg_decompress_struct cinfo; /* JPEG decompression struct */ |
| 77 |
struct jpeg_error_mgr jerr; /* JPEG Error handler */ |
| 78 |
JSAMPROW row_pointer[1]; /* output row buffer */ |
| 79 |
int row_stride; /* physical row width in output buf */ |
| 80 |
|
| 81 |
/* open input file before doing any JPEG decompression setup */ |
| 82 |
if ((ifp = fopen(name, "rb")) == nullptr) |
| 83 |
return IMAGEBADFILE; /* Could not open image, return error */ |
| 84 |
|
| 85 |
/* |
| 86 |
* Note: The Independent JPEG Group's library does not have a way |
| 87 |
* of returning errors without the use of setjmp/longjmp. |
| 88 |
* This is a problem in multi-threaded environment, since setjmp |
| 89 |
* and longjmp are declared thread-unsafe by many vendors currently. |
| 90 |
* For now, JPEG decompression errors will result in the "default" |
| 91 |
* error handling provided by the JPEG library, which is an error |
| 92 |
* message and a fatal call to exit(). I'll have to work around this |
| 93 |
* or find a reasonably thread-safe way of doing setjmp/longjmp.. |
| 94 |
*/ |
| 95 |
|
| 96 |
cinfo.err = jpeg_std_error(&jerr); /* Set JPEG error handler to default */ |
| 97 |
|
| 98 |
jpeg_create_decompress(&cinfo); /* Create decompression context */ |
| 99 |
jpeg_stdio_src(&cinfo, ifp); /* Set input mechanism to stdio type */ |
| 100 |
jpeg_read_header(&cinfo, TRUE); /* Read the JPEG header for info */ |
| 101 |
jpeg_start_decompress(&cinfo); /* Prepare for actual decompression */ |
| 102 |
|
| 103 |
*xres = cinfo.output_width; /* set returned image width */ |
| 104 |
*yres = cinfo.output_height; /* set returned image height */ |
| 105 |
|
| 106 |
/* Calculate the size of a row in the image */ |
| 107 |
row_stride = cinfo.output_width * cinfo.output_components; |
| 108 |
|
| 109 |
/* Allocate the image buffer which will be returned to the ray tracer */ |
| 110 |
*imgdata = (unsigned char *)malloc(row_stride * cinfo.output_height); |
| 111 |
|
| 112 |
/* decompress the JPEG, one scanline at a time into the buffer */ |
| 113 |
while (cinfo.output_scanline < cinfo.output_height) { |
| 114 |
row_pointer[0] = &((*imgdata)[(cinfo.output_scanline) * row_stride]); |
| 115 |
jpeg_read_scanlines(&cinfo, row_pointer, 1); |
| 116 |
} |
| 117 |
|
| 118 |
jpeg_finish_decompress(&cinfo); /* Tell the JPEG library to cleanup */ |
| 119 |
jpeg_destroy_decompress(&cinfo); /* Destroy JPEG decompression context */ |
| 120 |
|
| 121 |
fclose(ifp); /* Close the input file */ |
| 122 |
|
| 123 |
return IMAGENOERR; /* No fatal errors */ |
| 124 |
} |
| 125 |
|
| 126 |
#endif |