1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*
* xicc standalone utilities
*
* Author: Graeme W. Gill
* Date: 2/7/00
* Version: 1.00
*
* Copyright 2000 - 2006 Graeme W. Gill
* All rights reserved.
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
* see the License.txt file for licencing details.
*
*/
/*
* This module provides expanded capabilities,
* but is independent of other modules.
*/
#include <sys/types.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#ifdef __sun
#include <unistd.h>
#endif
#if defined(__IBMC__) && defined(_M_IX86)
#include <float.h>
#endif
#include "copyright.h"
#include "aconfig.h"
#include "icc.h"
#include "tiffio.h"
#include "jpeglib.h"
#include "iccjpeg.h"
#include "xutils.h" /* definitions for this library */
#undef DEBUG
#ifdef DEBUG
# define errout stderr
# define debug(xx) fprintf(errout, xx )
# define debug2(xx) fprintf xx
#else
# define debug(xx)
# define debug2(xx)
#endif
#if !defined(O_CREAT) && !defined(_O_CREAT)
# error "Need to #include fcntl.h!"
#endif
/* ------------------------------------------------------ */
/* Common clut table code */
/* Default table of clut resolutions */
/* See discussion in imdi/imdi_gen.c for ideal numbers */
static int lut_resolutions[9][4] = {
/* low, med, high, vhigh */
{ 0, 0, 0, 0 }, /* 0 */
{ 256, 772, 4370, 4370 }, /* 1 */
{ 86, 256, 256, 256 }, /* 2 */
{ 9, 17, 33, 52 }, /* 3 */
{ 6, 9, 18, 33 }, /* 4 */
{ 6, 9, 16, 18 }, /* 5 */
{ 6, 6, 9, 12 }, /* 6 */
{ 6, 7, 7, 9 }, /* 7 */
{ 3, 5, 5, 7 } /* 8 */
};
/* return a lut resolution given the input dimesion and quality */
/* Input dimension [0-8], quality: low 0, medium 1, high 2, very high 3 . */
/* A returned value of 0 indicates illegal. */
int dim_to_clutres(int dim, int quality) {
if (dim < 0)
dim = 0;
else if (dim > 8)
dim = 8;
if (quality < 0)
quality = 0;
if (quality > 3)
quality = 3;
return lut_resolutions[dim][quality];
}
/* ------------------------------------------------------ */
/* JPEG error information */
typedef struct {
jmp_buf env; /* setjmp/longjmp environment */
char message[JMSG_LENGTH_MAX];
} jpegerrorinfo;
/* JPEG error handler */
static void jpeg_error(j_common_ptr cinfo) {
jpegerrorinfo *p = (jpegerrorinfo *)cinfo->client_data;
(*cinfo->err->format_message) (cinfo, p->message);
longjmp(p->env, 1);
}
/* ------------------------------------------------------ */
/* Open an ICC file or a TIFF or JPEG file with an embedded ICC profile for reading. */
/* Return NULL on error */
icc *read_embedded_icc(char *file_name) {
TIFF *rh = NULL;
int size;
void *tag, *buf;
icmAlloc *al;
icmFile *fp;
icc *icco;
TIFFErrorHandler olderrh, oldwarnh;
TIFFErrorHandlerExt olderrhx, oldwarnhx;
int rv;
/* First see if the file can be opened as an ICC profile */
if ((fp = new_icmFileStd_name(file_name,"r")) == NULL) {
debug2((errout,"Can't open file '%s'\n",file_name));
return NULL;
}
if ((icco = new_icc()) == NULL) {
debug("Creation of ICC object failed\n");
fp->del(fp);
return NULL;
}
if ((rv = icco->read_x(icco,fp,0,1)) == 0) {
debug2((errout,"Opened '%s' as an icc profile\n",file_name));
return icco;
}
debug2((errout,"icc read failed with %d, %s\n",rv,icco->err));
icco->del(icco); /* icc wil fp->del() */
/* Not an ICC profile, see if it's a TIFF file */
olderrh = TIFFSetErrorHandler(NULL);
oldwarnh = TIFFSetWarningHandler(NULL);
olderrhx = TIFFSetErrorHandlerExt(NULL);
oldwarnhx = TIFFSetWarningHandlerExt(NULL);
if ((rh = TIFFOpen(file_name, "r")) != NULL) {
TIFFSetErrorHandler(olderrh);
TIFFSetWarningHandler(oldwarnh);
TIFFSetErrorHandlerExt(olderrhx);
TIFFSetWarningHandlerExt(oldwarnhx);
debug("TIFFOpen succeeded\n");
if (TIFFGetField(rh, TIFFTAG_ICCPROFILE, &size, &tag) == 0 || size == 0) {
debug2((errout,"no ICC profile found in '%s'\n",file_name));
TIFFClose(rh);
return NULL;
}
/* Make a copy of the profile to a memory buffer */
if ((al = new_icmAllocStd()) == NULL) {
debug("new_icmAllocStd failed\n");
TIFFClose(rh);
return NULL;
}
if ((buf = al->malloc(al, size)) == NULL) {
debug("malloc of profile buffer failed\n");
al->del(al);
TIFFClose(rh);
return NULL;
}
memmove(buf, tag, size);
TIFFClose(rh);
} else {
jpegerrorinfo jpeg_rerr;
FILE *rf = NULL;
struct jpeg_decompress_struct rj;
struct jpeg_error_mgr jerr;
unsigned char *pdata;
unsigned int plen;
debug2((errout,"TIFFOpen failed for '%s'\n",file_name));
TIFFSetErrorHandler(olderrh);
TIFFSetWarningHandler(oldwarnh);
TIFFSetErrorHandlerExt(olderrhx);
TIFFSetWarningHandlerExt(oldwarnhx);
/* We cope with the horrible ijg jpeg library error handling */
/* by using a setjmp/longjmp. */
jpeg_std_error(&jerr);
jerr.error_exit = jpeg_error;
if (setjmp(jpeg_rerr.env)) {
debug2((errout,"jpeg_read_header failed for '%s'\n",file_name));
jpeg_destroy_decompress(&rj);
fclose(rf);
return NULL;
}
rj.err = &jerr;
rj.client_data = &jpeg_rerr;
jpeg_create_decompress(&rj);
#if defined(O_BINARY) || defined(_O_BINARY)
if ((rf = fopen(file_name,"rb")) == NULL)
#else
if ((rf = fopen(file_name,"r")) == NULL)
#endif
{
debug2((errout,"fopen failed for '%s'\n",file_name));
jpeg_destroy_decompress(&rj);
return NULL;
}
jpeg_stdio_src(&rj, rf);
setup_read_icc_profile(&rj);
/* we'll longjmp on error */
jpeg_read_header(&rj, TRUE);
if (!read_icc_profile(&rj, &pdata, &plen)) {
debug2((errout,"no ICC profile found in '%s'\n",file_name));
jpeg_destroy_decompress(&rj);
fclose(rf);
return NULL;
}
jpeg_destroy_decompress(&rj);
fclose(rf);
/* Make a copy of the profile to a memory buffer */
/* (icmAllocStd may not be the same as malloc ?) */
if ((al = new_icmAllocStd()) == NULL) {
debug("new_icmAllocStd failed\n");
return NULL;
}
if ((buf = al->malloc(al, plen)) == NULL) {
debug("malloc of profile buffer failed\n");
al->del(al);
TIFFClose(rh);
return NULL;
}
memmove(buf, pdata, plen);
size = (int)plen;
free(pdata);
}
/* Memory File fp that will free the buffer when deleted: */
if ((fp = new_icmFileMem_ad(buf, size, al)) == NULL) {
debug("Creating memory file from CMProfileLocation failed");
al->free(al, buf);
al->del(al);
return NULL;
}
if ((icco = new_icc()) == NULL) {
debug("Creation of ICC object failed\n");
fp->del(fp); /* fp will delete al */
return NULL;
}
if ((rv = icco->read_x(icco,fp,0,1)) == 0) {
debug2((errout,"Opened '%s' embedded icc profile\n",file_name));
return icco;
}
debug2((errout,"Failed to read '%s' embedded icc profile\n",file_name));
icco->del(icco); /* icco will delete fp and al */
return NULL;
}
/* ------------------------------------------------------ */
|