/* * Copyright (c) 1999-2000 Image Power, Inc. and the University of * British Columbia. * Copyright (c) 2001-2003 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /* * Image Library * * $Id$ */ /******************************************************************************\* Includes.\******************************************************************************/ #define JAS_FOR_INTERNAL_USE_ONLY #include "jasper/jas_init.h"#include "jasper/jas_image.h"#include "jasper/jas_math.h"#include "jasper/jas_malloc.h"#include "jasper/jas_string.h"#include "jasper/jas_debug.h" /******************************************************************************\* Types.\******************************************************************************/ #define FLOORDIV(x, y) ((x) / (y)) /******************************************************************************\* Local prototypes.\******************************************************************************/ static jas_image_cmpt_t *jas_image_cmpt_create0(void);static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt);static jas_image_cmpt_t *jas_image_cmpt_create(int_fast32_t tlx, int_fast32_t tly, int_fast32_t hstep, int_fast32_t vstep, int_fast32_t width, int_fast32_t height, uint_fast16_t depth, bool sgnd, bool inmem);static void jas_image_setbbox(jas_image_t *image);static jas_image_cmpt_t *jas_image_cmpt_copy(jas_image_cmpt_t *cmpt);static int jas_image_growcmpts(jas_image_t *image, unsigned maxcmpts);static uint_fast32_t inttobits(jas_seqent_t v, unsigned prec, bool sgnd);static jas_seqent_t bitstoint(uint_fast32_t v, unsigned prec, bool sgnd);static int putint(jas_stream_t *out, bool sgnd, unsigned prec, long val);static int getint(jas_stream_t *in, bool sgnd, unsigned prec, long *val);static long uptomult(long x, long y);static long downtomult(long x, long y);static long convert(long val, bool oldsgnd, unsigned oldprec, bool newsgnd, unsigned newprec);static void jas_image_calcbbox2(const jas_image_t *image, jas_image_coord_t *tlx, jas_image_coord_t *tly, jas_image_coord_t *brx, jas_image_coord_t *bry);static void jas_image_fmtinfo_init(jas_image_fmtinfo_t *fmtinfo);static void jas_image_fmtinfo_cleanup(jas_image_fmtinfo_t *fmtinfo); /******************************************************************************\* Create and destroy operations.\******************************************************************************/ jas_image_t *jas_image_create(unsigned numcmpts, const jas_image_cmptparm_t *cmptparms, jas_clrspc_t clrspc){ jas_image_t *image; size_t rawsize; unsigned cmptno; const jas_image_cmptparm_t *cmptparm; JAS_LOGDEBUGF(100, "jas_image_create(%d, %p, %d)\n", numcmpts, cmptparms, clrspc); if (!(image = jas_image_create0())) { goto error; } image->clrspc_ = clrspc; image->maxcmpts_ = numcmpts;// image->inmem_ = true; /* Allocate memory for the per-component information. */ if (!(image->cmpts_ = jas_alloc2(image->maxcmpts_, sizeof(jas_image_cmpt_t *)))) { goto error; } /* Initialize in case of failure. */ for (cmptno = 0; cmptno < image->maxcmpts_; ++cmptno) { image->cmpts_[cmptno] = 0; } #if 0 /* Compute the approximate raw size of the image. */ rawsize = 0; for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno, ++cmptparm) { rawsize += cmptparm->width * cmptparm->height * (cmptparm->prec + 7) / 8; } /* Decide whether to buffer the image data in memory, based on the raw size of the image. */ inmem = (rawsize < JAS_IMAGE_INMEMTHRESH);#endif /* Create the individual image components. */ for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno, ++cmptparm) { if (!jas_safe_size_mul3(cmptparm->width, cmptparm->height, (cmptparm->prec + 7), &rawsize)) { goto error; } rawsize /= 8; const bool inmem = (rawsize < JAS_IMAGE_INMEMTHRESH); if (!(image->cmpts_[cmptno] = jas_image_cmpt_create(cmptparm->tlx, cmptparm->tly, cmptparm->hstep, cmptparm->vstep, cmptparm->width, cmptparm->height, cmptparm->prec, cmptparm->sgnd, inmem))) { goto error; } ++image->numcmpts_; } /* Determine the bounding box for all of the components on the reference grid (i.e., the image area) */ jas_image_setbbox(image); return image; error: if (image) { jas_image_destroy(image); } return 0;} jas_image_t *jas_image_create0(){ jas_image_t *image; if (!(image = jas_malloc(sizeof(jas_image_t)))) { return 0; } image->tlx_ = 0; image->tly_ = 0; image->brx_ = 0; image->bry_ = 0; image->clrspc_ = JAS_CLRSPC_UNKNOWN; image->numcmpts_ = 0; image->maxcmpts_ = 0; image->cmpts_ = 0;// image->inmem_ = true; image->cmprof_ = 0; return image;} jas_image_t *jas_image_copy(jas_image_t *image){ jas_image_t *newimage; if (!(newimage = jas_image_create0())) { goto error; } if (jas_image_growcmpts(newimage, image->numcmpts_)) { goto error; } for (unsigned cmptno = 0; cmptno < image->numcmpts_; ++cmptno) { if (!(newimage->cmpts_[cmptno] = jas_image_cmpt_copy( image->cmpts_[cmptno]))) { goto error; } ++newimage->numcmpts_; } jas_image_setbbox(newimage); if (image->cmprof_) { if (!(newimage->cmprof_ = jas_cmprof_copy(image->cmprof_))) { goto error; } } return newimage;error: if (newimage) { jas_image_destroy(newimage); } return 0;} static jas_image_cmpt_t *jas_image_cmpt_create0(){ jas_image_cmpt_t *cmpt; if (!(cmpt = jas_malloc(sizeof(jas_image_cmpt_t)))) { return 0; } memset(cmpt, 0, sizeof(jas_image_cmpt_t)); cmpt->type_ = JAS_IMAGE_CT_UNKNOWN; return cmpt;} static jas_image_cmpt_t *jas_image_cmpt_copy(jas_image_cmpt_t *cmpt){ jas_image_cmpt_t *newcmpt; if (!(newcmpt = jas_image_cmpt_create0())) { return 0; } newcmpt->tlx_ = cmpt->tlx_; newcmpt->tly_ = cmpt->tly_; newcmpt->hstep_ = cmpt->hstep_; newcmpt->vstep_ = cmpt->vstep_; newcmpt->width_ = cmpt->width_; newcmpt->height_ = cmpt->height_; newcmpt->prec_ = cmpt->prec_; newcmpt->sgnd_ = cmpt->sgnd_; newcmpt->cps_ = cmpt->cps_; newcmpt->type_ = cmpt->type_; if (!(newcmpt->stream_ = jas_stream_memopen(0, 0))) { goto error; } if (jas_stream_seek(cmpt->stream_, 0, SEEK_SET)) { goto error; } if (jas_stream_copy(newcmpt->stream_, cmpt->stream_, -1)) { goto error; } if (jas_stream_seek(newcmpt->stream_, 0, SEEK_SET)) { goto error; } return newcmpt;error: jas_image_cmpt_destroy(newcmpt); return 0;} void jas_image_destroy(jas_image_t *image){ if (image->cmpts_) { for (unsigned i = 0; i < image->numcmpts_; ++i) { jas_image_cmpt_destroy(image->cmpts_[i]); image->cmpts_[i] = 0; } jas_free(image->cmpts_); } if (image->cmprof_) { jas_cmprof_destroy(image->cmprof_); } jas_free(image);} static jas_image_cmpt_t *jas_image_cmpt_create(int_fast32_t tlx, int_fast32_t tly, int_fast32_t hstep, int_fast32_t vstep, int_fast32_t width, int_fast32_t height, uint_fast16_t depth, bool sgnd, bool inmem){ jas_image_cmpt_t *cmpt; size_t size; JAS_LOGDEBUGF(100, "jas_image_cmpt_create(%ld, %ld, %ld, %ld, %ld, %ld, %d, %d, %d)\n", JAS_CAST(long, tlx), JAS_CAST(long, tly), JAS_CAST(long, hstep), JAS_CAST(long, vstep), JAS_CAST(long, width), JAS_CAST(long, height), JAS_CAST(int, depth), sgnd, inmem ); if (depth < 1U + sgnd) { /* we need at least 1 bit for unsigned samples and 2 bits for signed samples */ return NULL; } if (width < 0 || height < 0 || hstep <= 0 || vstep <= 0) { return NULL; } if (!jas_safe_intfast32_add(tlx, width, 0) || !jas_safe_intfast32_add(tly, height, 0)) { return NULL; } if (!jas_safe_intfast32_mul3(width, height, depth, 0)) { return NULL; } if (!(cmpt = jas_malloc(sizeof(jas_image_cmpt_t)))) { return NULL; } cmpt->type_ = JAS_IMAGE_CT_UNKNOWN; cmpt->tlx_ = tlx; cmpt->tly_ = tly; cmpt->hstep_ = hstep; cmpt->vstep_ = vstep; cmpt->width_ = width; cmpt->height_ = height; cmpt->prec_ = depth; cmpt->sgnd_ = sgnd; cmpt->stream_ = 0; cmpt->cps_ = (depth + 7) / 8; /* * Compute the number of samples in the image component, while protecting * against overflow. * size = cmpt->width_ * cmpt->height_ * cmpt->cps_; */ if (!jas_safe_size_mul3(cmpt->width_, cmpt->height_, cmpt->cps_, &size)) { goto error; } cmpt->stream_ = (inmem) ? jas_stream_memopen(0, size) : jas_stream_tmpfile(); if (!cmpt->stream_) { goto error; } /* Zero the component data. This isn't necessary, but it is convenient for debugging purposes. */ /* Note: conversion of size - 1 to long can overflow */ if (size > 0) { if (size - 1 > LONG_MAX) { goto error; } if (jas_stream_seek(cmpt->stream_, size - 1, SEEK_SET) < 0 || jas_stream_putc(cmpt->stream_, 0) == EOF || jas_stream_seek(cmpt->stream_, 0, SEEK_SET) < 0) { goto error; } } return cmpt; error: jas_image_cmpt_destroy(cmpt); return 0;} static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt){ if (cmpt->stream_) { jas_stream_close(cmpt->stream_); } jas_free(cmpt);} /******************************************************************************\* Load and save operations.\******************************************************************************/ jas_image_t *jas_image_decode(jas_stream_t *in, int fmt, const char *optstr){ const jas_image_fmtinfo_t *fmtinfo; jas_image_t *image; image = 0; /* If possible, try to determine the format of the input data. */ if (fmt < 0) { if ((fmt = jas_image_getfmt(in)) < 0) { jas_logerrorf("jas_image_decode: cannot determine image format\n"); goto error; } } /* Is it possible to decode an image represented in this format? */ if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) { goto error; } if (!fmtinfo->ops.decode) { jas_logerrorf("jas_image_decode: no decode operation available\n"); goto error; } /* Decode the image. */ if (!(image = (*fmtinfo->ops.decode)(in, optstr))) { jas_logerrorf("jas_image_decode: decode operation failed\n"); goto error; } /* Create a color profile if needed. */ if (!jas_clrspc_isunknown(image->clrspc_) && !jas_clrspc_isgeneric(image->clrspc_) && !image->cmprof_) { if (!(image->cmprof_ = jas_cmprof_createfromclrspc(jas_image_clrspc(image)))) { jas_logerrorf("jas_image_decode: cannot create CM profile\n"); goto error; } } return image; error: if (image) { jas_image_destroy(image); } return 0;} int jas_image_encode(jas_image_t *image, jas_stream_t *out, int fmt, const char *optstr){ const jas_image_fmtinfo_t *fmtinfo; if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) { jas_logerrorf("format lookup failed\n"); return -1; } return (fmtinfo->ops.encode) ? (*fmtinfo->ops.encode)(image, out, optstr) : (-1);} /******************************************************************************\* Component read and write operations.\******************************************************************************/ int jas_image_readcmpt(jas_image_t *image, unsigned cmptno, jas_image_coord_t x, jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height, jas_matrix_t *data){ jas_image_cmpt_t *cmpt; jas_image_coord_t i; jas_image_coord_t j; jas_seqent_t v; int c; jas_seqent_t *dr; jas_seqent_t *d; JAS_LOGDEBUGF(100, "jas_image_readcmpt(%p, %d, %ld, %ld, %ld, %ld, %p)\n", image, cmptno, JAS_CAST(long, x), JAS_CAST(long, y), JAS_CAST(long, width), JAS_CAST(long, height), data); if(data == NULL) { return -1; } if (cmptno >= image->numcmpts_) { return -1; }Lines 1–500 of 1746. Load more
/* * Copyright (c) 1999-2000 Image Power, Inc. and the University of * British Columbia. * Copyright (c) 2001-2003 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /* * Image Library * * $Id$ */ /******************************************************************************\ * Includes. \******************************************************************************/ #define JAS_FOR_INTERNAL_USE_ONLY #include "jasper/jas_init.h" #include "jasper/jas_image.h" #include "jasper/jas_math.h" #include "jasper/jas_malloc.h" #include "jasper/jas_string.h" #include "jasper/jas_debug.h" /******************************************************************************\ * Types. \******************************************************************************/ #define FLOORDIV(x, y) ((x) / (y)) /******************************************************************************\ * Local prototypes. \******************************************************************************/ static jas_image_cmpt_t *jas_image_cmpt_create0(void); static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt); static jas_image_cmpt_t *jas_image_cmpt_create(int_fast32_t tlx, int_fast32_t tly, int_fast32_t hstep, int_fast32_t vstep, int_fast32_t width, int_fast32_t height, uint_fast16_t depth, bool sgnd, bool inmem); static void jas_image_setbbox(jas_image_t *image); static jas_image_cmpt_t *jas_image_cmpt_copy(jas_image_cmpt_t *cmpt); static int jas_image_growcmpts(jas_image_t *image, unsigned maxcmpts); static uint_fast32_t inttobits(jas_seqent_t v, unsigned prec, bool sgnd); static jas_seqent_t bitstoint(uint_fast32_t v, unsigned prec, bool sgnd); static int putint(jas_stream_t *out, bool sgnd, unsigned prec, long val); static int getint(jas_stream_t *in, bool sgnd, unsigned prec, long *val); static long uptomult(long x, long y); static long downtomult(long x, long y); static long convert(long val, bool oldsgnd, unsigned oldprec, bool newsgnd, unsigned newprec); static void jas_image_calcbbox2(const jas_image_t *image, jas_image_coord_t *tlx, jas_image_coord_t *tly, jas_image_coord_t *brx, jas_image_coord_t *bry); static void jas_image_fmtinfo_init(jas_image_fmtinfo_t *fmtinfo); static void jas_image_fmtinfo_cleanup(jas_image_fmtinfo_t *fmtinfo); /******************************************************************************\ * Create and destroy operations. \******************************************************************************/ jas_image_t *jas_image_create(unsigned numcmpts, const jas_image_cmptparm_t *cmptparms, jas_clrspc_t clrspc) { jas_image_t *image; size_t rawsize; unsigned cmptno; const jas_image_cmptparm_t *cmptparm; JAS_LOGDEBUGF(100, "jas_image_create(%d, %p, %d)\n", numcmpts, cmptparms, clrspc); if (!(image = jas_image_create0())) { goto error; } image->clrspc_ = clrspc; image->maxcmpts_ = numcmpts; // image->inmem_ = true; /* Allocate memory for the per-component information. */ if (!(image->cmpts_ = jas_alloc2(image->maxcmpts_, sizeof(jas_image_cmpt_t *)))) { goto error; } /* Initialize in case of failure. */ for (cmptno = 0; cmptno < image->maxcmpts_; ++cmptno) { image->cmpts_[cmptno] = 0; } #if 0 /* Compute the approximate raw size of the image. */ rawsize = 0; for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno, ++cmptparm) { rawsize += cmptparm->width * cmptparm->height * (cmptparm->prec + 7) / 8; } /* Decide whether to buffer the image data in memory, based on the raw size of the image. */ inmem = (rawsize < JAS_IMAGE_INMEMTHRESH); #endif /* Create the individual image components. */ for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno, ++cmptparm) { if (!jas_safe_size_mul3(cmptparm->width, cmptparm->height, (cmptparm->prec + 7), &rawsize)) { goto error; } rawsize /= 8; const bool inmem = (rawsize < JAS_IMAGE_INMEMTHRESH); if (!(image->cmpts_[cmptno] = jas_image_cmpt_create(cmptparm->tlx, cmptparm->tly, cmptparm->hstep, cmptparm->vstep, cmptparm->width, cmptparm->height, cmptparm->prec, cmptparm->sgnd, inmem))) { goto error; } ++image->numcmpts_; } /* Determine the bounding box for all of the components on the reference grid (i.e., the image area) */ jas_image_setbbox(image); return image; error: if (image) { jas_image_destroy(image); } return 0; } jas_image_t *jas_image_create0() { jas_image_t *image; if (!(image = jas_malloc(sizeof(jas_image_t)))) { return 0; } image->tlx_ = 0; image->tly_ = 0; image->brx_ = 0; image->bry_ = 0; image->clrspc_ = JAS_CLRSPC_UNKNOWN; image->numcmpts_ = 0; image->maxcmpts_ = 0; image->cmpts_ = 0; // image->inmem_ = true; image->cmprof_ = 0; return image; } jas_image_t *jas_image_copy(jas_image_t *image) { jas_image_t *newimage; if (!(newimage = jas_image_create0())) { goto error; } if (jas_image_growcmpts(newimage, image->numcmpts_)) { goto error; } for (unsigned cmptno = 0; cmptno < image->numcmpts_; ++cmptno) { if (!(newimage->cmpts_[cmptno] = jas_image_cmpt_copy( image->cmpts_[cmptno]))) { goto error; } ++newimage->numcmpts_; } jas_image_setbbox(newimage); if (image->cmprof_) { if (!(newimage->cmprof_ = jas_cmprof_copy(image->cmprof_))) { goto error; } } return newimage; error: if (newimage) { jas_image_destroy(newimage); } return 0; } static jas_image_cmpt_t *jas_image_cmpt_create0() { jas_image_cmpt_t *cmpt; if (!(cmpt = jas_malloc(sizeof(jas_image_cmpt_t)))) { return 0; } memset(cmpt, 0, sizeof(jas_image_cmpt_t)); cmpt->type_ = JAS_IMAGE_CT_UNKNOWN; return cmpt; } static jas_image_cmpt_t *jas_image_cmpt_copy(jas_image_cmpt_t *cmpt) { jas_image_cmpt_t *newcmpt; if (!(newcmpt = jas_image_cmpt_create0())) { return 0; } newcmpt->tlx_ = cmpt->tlx_; newcmpt->tly_ = cmpt->tly_; newcmpt->hstep_ = cmpt->hstep_; newcmpt->vstep_ = cmpt->vstep_; newcmpt->width_ = cmpt->width_; newcmpt->height_ = cmpt->height_; newcmpt->prec_ = cmpt->prec_; newcmpt->sgnd_ = cmpt->sgnd_; newcmpt->cps_ = cmpt->cps_; newcmpt->type_ = cmpt->type_; if (!(newcmpt->stream_ = jas_stream_memopen(0, 0))) { goto error; } if (jas_stream_seek(cmpt->stream_, 0, SEEK_SET)) { goto error; } if (jas_stream_copy(newcmpt->stream_, cmpt->stream_, -1)) { goto error; } if (jas_stream_seek(newcmpt->stream_, 0, SEEK_SET)) { goto error; } return newcmpt; error: jas_image_cmpt_destroy(newcmpt); return 0; } void jas_image_destroy(jas_image_t *image) { if (image->cmpts_) { for (unsigned i = 0; i < image->numcmpts_; ++i) { jas_image_cmpt_destroy(image->cmpts_[i]); image->cmpts_[i] = 0; } jas_free(image->cmpts_); } if (image->cmprof_) { jas_cmprof_destroy(image->cmprof_); } jas_free(image); } static jas_image_cmpt_t *jas_image_cmpt_create(int_fast32_t tlx, int_fast32_t tly, int_fast32_t hstep, int_fast32_t vstep, int_fast32_t width, int_fast32_t height, uint_fast16_t depth, bool sgnd, bool inmem) { jas_image_cmpt_t *cmpt; size_t size; JAS_LOGDEBUGF(100, "jas_image_cmpt_create(%ld, %ld, %ld, %ld, %ld, %ld, %d, %d, %d)\n", JAS_CAST(long, tlx), JAS_CAST(long, tly), JAS_CAST(long, hstep), JAS_CAST(long, vstep), JAS_CAST(long, width), JAS_CAST(long, height), JAS_CAST(int, depth), sgnd, inmem ); if (depth < 1U + sgnd) { /* we need at least 1 bit for unsigned samples and 2 bits for signed samples */ return NULL; } if (width < 0 || height < 0 || hstep <= 0 || vstep <= 0) { return NULL; } if (!jas_safe_intfast32_add(tlx, width, 0) || !jas_safe_intfast32_add(tly, height, 0)) { return NULL; } if (!jas_safe_intfast32_mul3(width, height, depth, 0)) { return NULL; } if (!(cmpt = jas_malloc(sizeof(jas_image_cmpt_t)))) { return NULL; } cmpt->type_ = JAS_IMAGE_CT_UNKNOWN; cmpt->tlx_ = tlx; cmpt->tly_ = tly; cmpt->hstep_ = hstep; cmpt->vstep_ = vstep; cmpt->width_ = width; cmpt->height_ = height; cmpt->prec_ = depth; cmpt->sgnd_ = sgnd; cmpt->stream_ = 0; cmpt->cps_ = (depth + 7) / 8; /* * Compute the number of samples in the image component, while protecting * against overflow. * size = cmpt->width_ * cmpt->height_ * cmpt->cps_; */ if (!jas_safe_size_mul3(cmpt->width_, cmpt->height_, cmpt->cps_, &size)) { goto error; } cmpt->stream_ = (inmem) ? jas_stream_memopen(0, size) : jas_stream_tmpfile(); if (!cmpt->stream_) { goto error; } /* Zero the component data. This isn't necessary, but it is convenient for debugging purposes. */ /* Note: conversion of size - 1 to long can overflow */ if (size > 0) { if (size - 1 > LONG_MAX) { goto error; } if (jas_stream_seek(cmpt->stream_, size - 1, SEEK_SET) < 0 || jas_stream_putc(cmpt->stream_, 0) == EOF || jas_stream_seek(cmpt->stream_, 0, SEEK_SET) < 0) { goto error; } } return cmpt; error: jas_image_cmpt_destroy(cmpt); return 0; } static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt) { if (cmpt->stream_) { jas_stream_close(cmpt->stream_); } jas_free(cmpt); } /******************************************************************************\ * Load and save operations. \******************************************************************************/ jas_image_t *jas_image_decode(jas_stream_t *in, int fmt, const char *optstr) { const jas_image_fmtinfo_t *fmtinfo; jas_image_t *image; image = 0; /* If possible, try to determine the format of the input data. */ if (fmt < 0) { if ((fmt = jas_image_getfmt(in)) < 0) { jas_logerrorf("jas_image_decode: cannot determine image format\n"); goto error; } } /* Is it possible to decode an image represented in this format? */ if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) { goto error; } if (!fmtinfo->ops.decode) { jas_logerrorf("jas_image_decode: no decode operation available\n"); goto error; } /* Decode the image. */ if (!(image = (*fmtinfo->ops.decode)(in, optstr))) { jas_logerrorf("jas_image_decode: decode operation failed\n"); goto error; } /* Create a color profile if needed. */ if (!jas_clrspc_isunknown(image->clrspc_) && !jas_clrspc_isgeneric(image->clrspc_) && !image->cmprof_) { if (!(image->cmprof_ = jas_cmprof_createfromclrspc(jas_image_clrspc(image)))) { jas_logerrorf("jas_image_decode: cannot create CM profile\n"); goto error; } } return image; error: if (image) { jas_image_destroy(image); } return 0; } int jas_image_encode(jas_image_t *image, jas_stream_t *out, int fmt, const char *optstr) { const jas_image_fmtinfo_t *fmtinfo; if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) { jas_logerrorf("format lookup failed\n"); return -1; } return (fmtinfo->ops.encode) ? (*fmtinfo->ops.encode)(image, out, optstr) : (-1); } /******************************************************************************\ * Component read and write operations. \******************************************************************************/ int jas_image_readcmpt(jas_image_t *image, unsigned cmptno, jas_image_coord_t x, jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height, jas_matrix_t *data) { jas_image_cmpt_t *cmpt; jas_image_coord_t i; jas_image_coord_t j; jas_seqent_t v; int c; jas_seqent_t *dr; jas_seqent_t *d; JAS_LOGDEBUGF(100, "jas_image_readcmpt(%p, %d, %ld, %ld, %ld, %ld, %p)\n", image, cmptno, JAS_CAST(long, x), JAS_CAST(long, y), JAS_CAST(long, width), JAS_CAST(long, height), data); if(data == NULL) { return -1; } if (cmptno >= image->numcmpts_) { return -1; }