blob: 8b1fe38082b1d159954e803b8cc97acb7357d175 [file] [log] [blame]
DRCb76c8402011-12-19 15:01:55 +00001/*
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
Leon Scroggins III3993b372018-07-16 10:43:45 -040038#define JCS_EXT_RGB 6
DRCb76c8402011-12-19 15:01:55 +000039#endif
40#if !defined(JCS_EXTENSIONS) || !defined(JCS_ALPHA_EXTENSIONS)
Leon Scroggins III3993b372018-07-16 10:43:45 -040041#define JCS_EXT_RGBA 12
DRCb76c8402011-12-19 15:01:55 +000042#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;
Leon Scroggins III3993b372018-07-16 10:43:45 -040054 (*cinfo->err->output_message) (cinfo);
DRCb76c8402011-12-19 15:01:55 +000055 longjmp(myerr->jb, 1);
56}
57
58static void my_output_message(j_common_ptr cinfo)
59{
Leon Scroggins III3993b372018-07-16 10:43:45 -040060 (*cinfo->err->format_message) (cinfo, lasterror);
DRCb76c8402011-12-19 15:01:55 +000061}
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");
Leon Scroggins III3993b372018-07-16 10:43:45 -040070#if JCS_EXTENSIONS
DRCb76c8402011-12-19 15:01:55 +000071 printf(" Present at compile time\n");
Leon Scroggins III3993b372018-07-16 10:43:45 -040072#else
DRCb76c8402011-12-19 15:01:55 +000073 printf(" Not present at compile time\n");
Leon Scroggins III3993b372018-07-16 10:43:45 -040074#endif
DRCb76c8402011-12-19 15:01:55 +000075
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
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -050080 if (setjmp(jerr.jb)) {
DRC006bc582014-02-27 21:22:54 +000081 /* this will execute if libjpeg has an error */
DRCb76c8402011-12-19 15:01:55 +000082 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
Leon Scroggins III3993b372018-07-16 10:43:45 -040093done:
DRCb76c8402011-12-19 15:01:55 +000094 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");
Leon Scroggins III3993b372018-07-16 10:43:45 -0400101#if JCS_ALPHA_EXTENSIONS
DRCb76c8402011-12-19 15:01:55 +0000102 printf(" Present at compile time\n");
Leon Scroggins III3993b372018-07-16 10:43:45 -0400103#else
DRCb76c8402011-12-19 15:01:55 +0000104 printf(" Not present at compile time\n");
Leon Scroggins III3993b372018-07-16 10:43:45 -0400105#endif
DRCb76c8402011-12-19 15:01:55 +0000106
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500107 if (setjmp(jerr.jb)) {
DRC006bc582014-02-27 21:22:54 +0000108 /* this will execute if libjpeg has an error */
DRCb76c8402011-12-19 15:01:55 +0000109 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
Leon Scroggins III3993b372018-07-16 10:43:45 -0400117done2:
DRCb76c8402011-12-19 15:01:55 +0000118 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}