shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Copyright (C)2011 D. R. Commander.  All Rights Reserved.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Redistribution and use in source and binary forms, with or without
shun-iwasawa 82a8f5
 * modification, are permitted provided that the following conditions are met:
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * - Redistributions of source code must retain the above copyright notice,
shun-iwasawa 82a8f5
 *   this list of conditions and the following disclaimer.
shun-iwasawa 82a8f5
 * - Redistributions in binary form must reproduce the above copyright notice,
shun-iwasawa 82a8f5
 *   this list of conditions and the following disclaimer in the documentation
shun-iwasawa 82a8f5
 *   and/or other materials provided with the distribution.
shun-iwasawa 82a8f5
 * - Neither the name of the libjpeg-turbo Project nor the names of its
shun-iwasawa 82a8f5
 *   contributors may be used to endorse or promote products derived from this
shun-iwasawa 82a8f5
 *   software without specific prior written permission.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
shun-iwasawa 82a8f5
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
shun-iwasawa 82a8f5
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
shun-iwasawa 82a8f5
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
shun-iwasawa 82a8f5
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
shun-iwasawa 82a8f5
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
shun-iwasawa 82a8f5
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
shun-iwasawa 82a8f5
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
shun-iwasawa 82a8f5
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
shun-iwasawa 82a8f5
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
shun-iwasawa 82a8f5
 * POSSIBILITY OF SUCH DAMAGE.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* This program demonstrates how to check for the colorspace extension
shun-iwasawa 82a8f5
   capabilities of libjpeg-turbo at both compile time and run time. */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include <stdio.h></stdio.h>
shun-iwasawa 82a8f5
#include <jpeglib.h></jpeglib.h>
shun-iwasawa 82a8f5
#include <jerror.h></jerror.h>
shun-iwasawa 82a8f5
#include <setjmp.h></setjmp.h>
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef JCS_EXTENSIONS
shun-iwasawa 82a8f5
#define JCS_EXT_RGB  6
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
#if !defined(JCS_EXTENSIONS) || !defined(JCS_ALPHA_EXTENSIONS)
shun-iwasawa 82a8f5
#define JCS_EXT_RGBA  12
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static char lasterror[JMSG_LENGTH_MAX] = "No error";
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct _error_mgr {
shun-iwasawa 82a8f5
  struct jpeg_error_mgr pub;
shun-iwasawa 82a8f5
  jmp_buf jb;
shun-iwasawa 82a8f5
} error_mgr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void my_error_exit(j_common_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  error_mgr *myerr = (error_mgr *)cinfo->err;
shun-iwasawa 82a8f5
  (*cinfo->err->output_message) (cinfo);
shun-iwasawa 82a8f5
  longjmp(myerr->jb, 1);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void my_output_message(j_common_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  (*cinfo->err->format_message) (cinfo, lasterror);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
int main(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int jcs_valid = -1, jcs_alpha_valid = -1;
shun-iwasawa 82a8f5
  struct jpeg_compress_struct cinfo;
shun-iwasawa 82a8f5
  error_mgr jerr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  printf("libjpeg-turbo colorspace extensions:\n");
shun-iwasawa 82a8f5
#if JCS_EXTENSIONS
shun-iwasawa 82a8f5
  printf("  Present at compile time\n");
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
  printf("  Not present at compile time\n");
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo.err = jpeg_std_error(&jerr.pub);
shun-iwasawa 82a8f5
  jerr.pub.error_exit = my_error_exit;
shun-iwasawa 82a8f5
  jerr.pub.output_message = my_output_message;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(jerr.jb)) {
shun-iwasawa 82a8f5
    /* this will execute if libjpeg has an error */
shun-iwasawa 82a8f5
    jcs_valid = 0;
shun-iwasawa 82a8f5
    goto done;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_create_compress(&cinfo);
shun-iwasawa 82a8f5
  cinfo.input_components = 3;
shun-iwasawa 82a8f5
  jpeg_set_defaults(&cinfo);
shun-iwasawa 82a8f5
  cinfo.in_color_space = JCS_EXT_RGB;
shun-iwasawa 82a8f5
  jpeg_default_colorspace(&cinfo);
shun-iwasawa 82a8f5
  jcs_valid = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
done:
shun-iwasawa 82a8f5
  if (jcs_valid)
shun-iwasawa 82a8f5
    printf("  Working properly\n");
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    printf("  Not working properly.  Error returned was:\n    %s\n",
shun-iwasawa 82a8f5
           lasterror);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  printf("libjpeg-turbo alpha colorspace extensions:\n");
shun-iwasawa 82a8f5
#if JCS_ALPHA_EXTENSIONS
shun-iwasawa 82a8f5
  printf("  Present at compile time\n");
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
  printf("  Not present at compile time\n");
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(jerr.jb)) {
shun-iwasawa 82a8f5
    /* this will execute if libjpeg has an error */
shun-iwasawa 82a8f5
    jcs_alpha_valid = 0;
shun-iwasawa 82a8f5
    goto done2;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo.in_color_space = JCS_EXT_RGBA;
shun-iwasawa 82a8f5
  jpeg_default_colorspace(&cinfo);
shun-iwasawa 82a8f5
  jcs_alpha_valid = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
done2:
shun-iwasawa 82a8f5
  if (jcs_alpha_valid)
shun-iwasawa 82a8f5
    printf("  Working properly\n");
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    printf("  Not working properly.  Error returned was:\n    %s\n",
shun-iwasawa 82a8f5
           lasterror);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_destroy_compress(&cinfo);
shun-iwasawa 82a8f5
  return 0;
shun-iwasawa 82a8f5
}