blob: 8b1fe38082b1d159954e803b8cc97acb7357d175 [file] [log] [blame]
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001/*
2 * Copyright (C)2011 D. R. Commander. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/* This program demonstrates how to check for the colorspace extension
30 capabilities of libjpeg-turbo at both compile time and run time. */
31
32#include <stdio.h>
33#include <jpeglib.h>
34#include <jerror.h>
35#include <setjmp.h>
36
37#ifndef JCS_EXTENSIONS
Chris Blumecca8c4d2019-03-01 01:09:50 -080038#define JCS_EXT_RGB 6
Tom Hudson0d47d2d2016-05-04 13:22:56 -040039#endif
40#if !defined(JCS_EXTENSIONS) || !defined(JCS_ALPHA_EXTENSIONS)
Chris Blumecca8c4d2019-03-01 01:09:50 -080041#define JCS_EXT_RGBA 12
Tom Hudson0d47d2d2016-05-04 13:22:56 -040042#endif
43
44static char lasterror[JMSG_LENGTH_MAX] = "No error";
45
46typedef struct _error_mgr {
47 struct jpeg_error_mgr pub;
48 jmp_buf jb;
49} error_mgr;
50
51static void my_error_exit(j_common_ptr cinfo)
52{
53 error_mgr *myerr = (error_mgr *)cinfo->err;
Chris Blumecca8c4d2019-03-01 01:09:50 -080054 (*cinfo->err->output_message) (cinfo);
Tom Hudson0d47d2d2016-05-04 13:22:56 -040055 longjmp(myerr->jb, 1);
56}
57
58static void my_output_message(j_common_ptr cinfo)
59{
Chris Blumecca8c4d2019-03-01 01:09:50 -080060 (*cinfo->err->format_message) (cinfo, lasterror);
Tom Hudson0d47d2d2016-05-04 13:22:56 -040061}
62
63int main(void)
64{
65 int jcs_valid = -1, jcs_alpha_valid = -1;
66 struct jpeg_compress_struct cinfo;
67 error_mgr jerr;
68
69 printf("libjpeg-turbo colorspace extensions:\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -080070#if JCS_EXTENSIONS
Tom Hudson0d47d2d2016-05-04 13:22:56 -040071 printf(" Present at compile time\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -080072#else
Tom Hudson0d47d2d2016-05-04 13:22:56 -040073 printf(" Not present at compile time\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -080074#endif
Tom Hudson0d47d2d2016-05-04 13:22:56 -040075
76 cinfo.err = jpeg_std_error(&jerr.pub);
77 jerr.pub.error_exit = my_error_exit;
78 jerr.pub.output_message = my_output_message;
79
Chris Blumecca8c4d2019-03-01 01:09:50 -080080 if (setjmp(jerr.jb)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -040081 /* this will execute if libjpeg has an error */
82 jcs_valid = 0;
83 goto done;
84 }
85
86 jpeg_create_compress(&cinfo);
87 cinfo.input_components = 3;
88 jpeg_set_defaults(&cinfo);
89 cinfo.in_color_space = JCS_EXT_RGB;
90 jpeg_default_colorspace(&cinfo);
91 jcs_valid = 1;
92
Chris Blumecca8c4d2019-03-01 01:09:50 -080093done:
Tom Hudson0d47d2d2016-05-04 13:22:56 -040094 if (jcs_valid)
95 printf(" Working properly\n");
96 else
97 printf(" Not working properly. Error returned was:\n %s\n",
98 lasterror);
99
100 printf("libjpeg-turbo alpha colorspace extensions:\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -0800101#if JCS_ALPHA_EXTENSIONS
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400102 printf(" Present at compile time\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -0800103#else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400104 printf(" Not present at compile time\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -0800105#endif
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400106
Chris Blumecca8c4d2019-03-01 01:09:50 -0800107 if (setjmp(jerr.jb)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400108 /* this will execute if libjpeg has an error */
109 jcs_alpha_valid = 0;
110 goto done2;
111 }
112
113 cinfo.in_color_space = JCS_EXT_RGBA;
114 jpeg_default_colorspace(&cinfo);
115 jcs_alpha_valid = 1;
116
Chris Blumecca8c4d2019-03-01 01:09:50 -0800117done2:
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400118 if (jcs_alpha_valid)
119 printf(" Working properly\n");
120 else
121 printf(" Not working properly. Error returned was:\n %s\n",
122 lasterror);
123
124 jpeg_destroy_compress(&cinfo);
125 return 0;
126}