]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ti-machine-learning/ti-machine-learning.git/blob - debian/ti-timl/usr/src/timl/src/common/util/timlUtilReadFixedSizeJPEG.c
modified
[ti-machine-learning/ti-machine-learning.git] / debian / ti-timl / usr / src / timl / src / common / util / timlUtilReadFixedSizeJPEG.c
1 /******************************************************************************/
2 /*!
3  * \file timlUtilReadFixedSizeJPEG.c
4  */
5 /* Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  *    Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the
17  *    distribution.
18  *
19  *    Neither the name of Texas Instruments Incorporated nor the names of
20  *    its contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  ******************************************************************************/
38 /*******************************************************************************
39  *
40  * INCLUDES
41  *
42  ******************************************************************************/
44 #include "../api/timl.h"
47 /******************************************************************************/
48 /*!
49  * \ingroup    util
50  * \brief      Read a jpg image with known size information to avoid frequent allocation and deallocation of data
51  * \param[in]  name    Image name
52  * \param[out] data    Data
53  * \param[in]  row     Row
54  * \param[in]  col     Col
55  * \param[in]  channel Channel
56  * \return     Error code
57  */
58 /******************************************************************************/
60 int timlUtilReadFixedSizeJPEG(const char *name, float *data, int row, int col, int channel)
61 {
62    JSAMPARRAY buffer; /* Output row buffer */
63    FILE       *infile;
64    int        row_stride;
65    int        i;
66    int        j;
67    struct     jpeg_decompress_struct cinfo;
68    struct     jpeg_error_mgr jerr;
69    int        imageChannel;
70    int        imageRow;
71    int        imageCol;
73    cinfo.err = jpeg_std_error(&jerr);
74    jpeg_create_decompress(&cinfo);
75    infile = fopen(name, "rb");
76    jpeg_stdio_src(&cinfo, infile);
77    jpeg_read_header(&cinfo, TRUE);
78    jpeg_start_decompress(&cinfo);
80    imageChannel = cinfo.output_components;
81    imageRow     = cinfo.output_height;
82    imageCol     = cinfo.output_width;
84    if (imageRow != row || imageCol != col) {
85       jpeg_finish_decompress(&cinfo);
86       jpeg_destroy_decompress(&cinfo);
87       fclose(infile);
88       return ERROR_UTIL_JPEG_READING;
89    }
91    // buffer for one row (R,G,B), (R,G,B), .....
92    row_stride = cinfo.output_width * cinfo.output_components;
93    buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
95    while (cinfo.output_scanline < cinfo.output_height) {
96       jpeg_read_scanlines(&cinfo, buffer, 1);
97       i = cinfo.output_scanline - 1;
98       if (imageChannel == 1) {
99          for (j = 0; j < col; j++) {
100             data[i*col + j] = (float) buffer[0][j];
101          }
102       }
103       else {
104          for (j = 0; j < col; j++) {
105             data[i*col + j] = (float) buffer[0][j*3]; // R
106             data[i*col + j + col*row] = (float) buffer[0][3*j + 1]; // G
107             data[i*col + j + 2*col*row] = (float) buffer[0][3*j + 2]; // B
108          }
109       }
110    }
111    jpeg_finish_decompress(&cinfo);
112    jpeg_destroy_decompress(&cinfo);
113    fclose(infile);
115    return 0;