1 /*
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
38 #define JCS_EXT_RGB 6
39 #endif
40 #if !defined(JCS_EXTENSIONS) || !defined(JCS_ALPHA_EXTENSIONS)
41 #define JCS_EXT_RGBA 12
42 #endif
43 
44 static char lasterror[JMSG_LENGTH_MAX] = "No error";
45 
46 typedef struct _error_mgr {
47   struct jpeg_error_mgr pub;
48   jmp_buf jb;
49 } error_mgr;
50 
my_error_exit(j_common_ptr cinfo)51 static void my_error_exit(j_common_ptr cinfo)
52 {
53   error_mgr *myerr = (error_mgr *)cinfo->err;
54   (*cinfo->err->output_message)(cinfo);
55   longjmp(myerr->jb, 1);
56 }
57 
my_output_message(j_common_ptr cinfo)58 static void my_output_message(j_common_ptr cinfo)
59 {
60   (*cinfo->err->format_message)(cinfo, lasterror);
61 }
62 
main(void)63 int 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");
70   #if JCS_EXTENSIONS
71   printf("  Present at compile time\n");
72   #else
73   printf("  Not present at compile time\n");
74   #endif
75 
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 
80   if(setjmp(jerr.jb)) {
81     /* 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 
93   done:
94   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");
101   #if JCS_ALPHA_EXTENSIONS
102   printf("  Present at compile time\n");
103   #else
104   printf("  Not present at compile time\n");
105   #endif
106 
107   if(setjmp(jerr.jb)) {
108     /* 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 
117   done2:
118   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 }
127