jcstest.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /* This program demonstrates how to check for the colorspace extension
  29. capabilities of libjpeg-turbo at both compile time and run time. */
  30. #include <stdio.h>
  31. #include <jpeglib.h>
  32. #include <jerror.h>
  33. #include <setjmp.h>
  34. #ifndef JCS_EXTENSIONS
  35. #define JCS_EXT_RGB 6
  36. #endif
  37. #if !defined(JCS_EXTENSIONS) || !defined(JCS_ALPHA_EXTENSIONS)
  38. #define JCS_EXT_RGBA 12
  39. #endif
  40. static char lasterror[JMSG_LENGTH_MAX] = "No error";
  41. typedef struct _error_mgr {
  42. struct jpeg_error_mgr pub;
  43. jmp_buf jb;
  44. } error_mgr;
  45. static void my_error_exit(j_common_ptr cinfo)
  46. {
  47. error_mgr *myerr = (error_mgr *)cinfo->err;
  48. (*cinfo->err->output_message) (cinfo);
  49. longjmp(myerr->jb, 1);
  50. }
  51. static void my_output_message(j_common_ptr cinfo)
  52. {
  53. (*cinfo->err->format_message) (cinfo, lasterror);
  54. }
  55. int main(void)
  56. {
  57. int jcs_valid = -1, jcs_alpha_valid = -1;
  58. struct jpeg_compress_struct cinfo;
  59. error_mgr jerr;
  60. printf("libjpeg-turbo colorspace extensions:\n");
  61. #if JCS_EXTENSIONS
  62. printf(" Present at compile time\n");
  63. #else
  64. printf(" Not present at compile time\n");
  65. #endif
  66. cinfo.err = jpeg_std_error(&jerr.pub);
  67. jerr.pub.error_exit = my_error_exit;
  68. jerr.pub.output_message = my_output_message;
  69. if (setjmp(jerr.jb)) {
  70. /* this will execute if libjpeg has an error */
  71. jcs_valid = 0;
  72. goto done;
  73. }
  74. jpeg_create_compress(&cinfo);
  75. cinfo.input_components = 3;
  76. jpeg_set_defaults(&cinfo);
  77. cinfo.in_color_space = JCS_EXT_RGB;
  78. jpeg_default_colorspace(&cinfo);
  79. jcs_valid = 1;
  80. done:
  81. if (jcs_valid)
  82. printf(" Working properly\n");
  83. else
  84. printf(" Not working properly. Error returned was:\n %s\n",
  85. lasterror);
  86. printf("libjpeg-turbo alpha colorspace extensions:\n");
  87. #if JCS_ALPHA_EXTENSIONS
  88. printf(" Present at compile time\n");
  89. #else
  90. printf(" Not present at compile time\n");
  91. #endif
  92. if (setjmp(jerr.jb)) {
  93. /* this will execute if libjpeg has an error */
  94. jcs_alpha_valid = 0;
  95. goto done2;
  96. }
  97. cinfo.in_color_space = JCS_EXT_RGBA;
  98. jpeg_default_colorspace(&cinfo);
  99. jcs_alpha_valid = 1;
  100. done2:
  101. if (jcs_alpha_valid)
  102. printf(" Working properly\n");
  103. else
  104. printf(" Not working properly. Error returned was:\n %s\n",
  105. lasterror);
  106. jpeg_destroy_compress(&cinfo);
  107. return 0;
  108. }