image.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "opj_includes.h"
  32. opj_image_t* opj_image_create0(void)
  33. {
  34. opj_image_t *image = (opj_image_t*)opj_calloc(1, sizeof(opj_image_t));
  35. return image;
  36. }
  37. opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts,
  38. opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc)
  39. {
  40. OPJ_UINT32 compno;
  41. opj_image_t *image = NULL;
  42. image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t));
  43. if (image) {
  44. image->color_space = clrspc;
  45. image->numcomps = numcmpts;
  46. /* allocate memory for the per-component information */
  47. image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps,
  48. sizeof(opj_image_comp_t));
  49. if (!image->comps) {
  50. /* TODO replace with event manager, breaks API */
  51. /* fprintf(stderr,"Unable to allocate memory for image.\n"); */
  52. opj_image_destroy(image);
  53. return NULL;
  54. }
  55. /* create the individual image components */
  56. for (compno = 0; compno < numcmpts; compno++) {
  57. opj_image_comp_t *comp = &image->comps[compno];
  58. comp->dx = cmptparms[compno].dx;
  59. comp->dy = cmptparms[compno].dy;
  60. comp->w = cmptparms[compno].w;
  61. comp->h = cmptparms[compno].h;
  62. comp->x0 = cmptparms[compno].x0;
  63. comp->y0 = cmptparms[compno].y0;
  64. comp->prec = cmptparms[compno].prec;
  65. comp->sgnd = cmptparms[compno].sgnd;
  66. if (comp->h != 0 &&
  67. (OPJ_SIZE_T)comp->w > SIZE_MAX / comp->h / sizeof(OPJ_INT32)) {
  68. /* TODO event manager */
  69. opj_image_destroy(image);
  70. return NULL;
  71. }
  72. comp->data = (OPJ_INT32*) opj_image_data_alloc(
  73. (size_t)comp->w * comp->h * sizeof(OPJ_INT32));
  74. if (!comp->data) {
  75. /* TODO replace with event manager, breaks API */
  76. /* fprintf(stderr,"Unable to allocate memory for image.\n"); */
  77. opj_image_destroy(image);
  78. return NULL;
  79. }
  80. memset(comp->data, 0, (size_t)comp->w * comp->h * sizeof(OPJ_INT32));
  81. }
  82. }
  83. return image;
  84. }
  85. void OPJ_CALLCONV opj_image_destroy(opj_image_t *image)
  86. {
  87. if (image) {
  88. if (image->comps) {
  89. OPJ_UINT32 compno;
  90. /* image components */
  91. for (compno = 0; compno < image->numcomps; compno++) {
  92. opj_image_comp_t *image_comp = &(image->comps[compno]);
  93. if (image_comp->data) {
  94. opj_image_data_free(image_comp->data);
  95. }
  96. }
  97. opj_free(image->comps);
  98. }
  99. if (image->icc_profile_buf) {
  100. opj_free(image->icc_profile_buf);
  101. }
  102. opj_free(image);
  103. }
  104. }
  105. /**
  106. * Updates the components characteristics of the image from the coding parameters.
  107. *
  108. * @param p_image_header the image header to update.
  109. * @param p_cp the coding parameters from which to update the image.
  110. */
  111. void opj_image_comp_header_update(opj_image_t * p_image_header,
  112. const struct opj_cp * p_cp)
  113. {
  114. OPJ_UINT32 i, l_width, l_height;
  115. OPJ_UINT32 l_x0, l_y0, l_x1, l_y1;
  116. OPJ_UINT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
  117. opj_image_comp_t* l_img_comp = NULL;
  118. l_x0 = opj_uint_max(p_cp->tx0, p_image_header->x0);
  119. l_y0 = opj_uint_max(p_cp->ty0, p_image_header->y0);
  120. l_x1 = p_cp->tx0 + (p_cp->tw - 1U) *
  121. p_cp->tdx; /* validity of p_cp members used here checked in opj_j2k_read_siz. Can't overflow. */
  122. l_y1 = p_cp->ty0 + (p_cp->th - 1U) * p_cp->tdy; /* can't overflow */
  123. l_x1 = opj_uint_min(opj_uint_adds(l_x1, p_cp->tdx),
  124. p_image_header->x1); /* use add saturated to prevent overflow */
  125. l_y1 = opj_uint_min(opj_uint_adds(l_y1, p_cp->tdy),
  126. p_image_header->y1); /* use add saturated to prevent overflow */
  127. l_img_comp = p_image_header->comps;
  128. for (i = 0; i < p_image_header->numcomps; ++i) {
  129. l_comp_x0 = opj_uint_ceildiv(l_x0, l_img_comp->dx);
  130. l_comp_y0 = opj_uint_ceildiv(l_y0, l_img_comp->dy);
  131. l_comp_x1 = opj_uint_ceildiv(l_x1, l_img_comp->dx);
  132. l_comp_y1 = opj_uint_ceildiv(l_y1, l_img_comp->dy);
  133. l_width = opj_uint_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor);
  134. l_height = opj_uint_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor);
  135. l_img_comp->w = l_width;
  136. l_img_comp->h = l_height;
  137. l_img_comp->x0 = l_comp_x0;
  138. l_img_comp->y0 = l_comp_y0;
  139. ++l_img_comp;
  140. }
  141. }
  142. /**
  143. * Copy only header of image and its component header (no data are copied)
  144. * if dest image have data, they will be freed
  145. *
  146. * @param p_image_src the src image
  147. * @param p_image_dest the dest image
  148. *
  149. */
  150. void opj_copy_image_header(const opj_image_t* p_image_src,
  151. opj_image_t* p_image_dest)
  152. {
  153. OPJ_UINT32 compno;
  154. /* preconditions */
  155. assert(p_image_src != 00);
  156. assert(p_image_dest != 00);
  157. p_image_dest->x0 = p_image_src->x0;
  158. p_image_dest->y0 = p_image_src->y0;
  159. p_image_dest->x1 = p_image_src->x1;
  160. p_image_dest->y1 = p_image_src->y1;
  161. if (p_image_dest->comps) {
  162. for (compno = 0; compno < p_image_dest->numcomps; compno++) {
  163. opj_image_comp_t *image_comp = &(p_image_dest->comps[compno]);
  164. if (image_comp->data) {
  165. opj_image_data_free(image_comp->data);
  166. }
  167. }
  168. opj_free(p_image_dest->comps);
  169. p_image_dest->comps = NULL;
  170. }
  171. p_image_dest->numcomps = p_image_src->numcomps;
  172. p_image_dest->comps = (opj_image_comp_t*) opj_malloc(p_image_dest->numcomps *
  173. sizeof(opj_image_comp_t));
  174. if (!p_image_dest->comps) {
  175. p_image_dest->comps = NULL;
  176. p_image_dest->numcomps = 0;
  177. return;
  178. }
  179. for (compno = 0; compno < p_image_dest->numcomps; compno++) {
  180. memcpy(&(p_image_dest->comps[compno]),
  181. &(p_image_src->comps[compno]),
  182. sizeof(opj_image_comp_t));
  183. p_image_dest->comps[compno].data = NULL;
  184. }
  185. p_image_dest->color_space = p_image_src->color_space;
  186. p_image_dest->icc_profile_len = p_image_src->icc_profile_len;
  187. if (p_image_dest->icc_profile_len) {
  188. p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_malloc(
  189. p_image_dest->icc_profile_len);
  190. if (!p_image_dest->icc_profile_buf) {
  191. p_image_dest->icc_profile_buf = NULL;
  192. p_image_dest->icc_profile_len = 0;
  193. return;
  194. }
  195. memcpy(p_image_dest->icc_profile_buf,
  196. p_image_src->icc_profile_buf,
  197. p_image_src->icc_profile_len);
  198. } else {
  199. p_image_dest->icc_profile_buf = NULL;
  200. }
  201. return;
  202. }
  203. opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts,
  204. opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc)
  205. {
  206. OPJ_UINT32 compno;
  207. opj_image_t *image = 00;
  208. image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t));
  209. if (image) {
  210. image->color_space = clrspc;
  211. image->numcomps = numcmpts;
  212. /* allocate memory for the per-component information */
  213. image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps,
  214. sizeof(opj_image_comp_t));
  215. if (!image->comps) {
  216. opj_image_destroy(image);
  217. return 00;
  218. }
  219. /* create the individual image components */
  220. for (compno = 0; compno < numcmpts; compno++) {
  221. opj_image_comp_t *comp = &image->comps[compno];
  222. comp->dx = cmptparms[compno].dx;
  223. comp->dy = cmptparms[compno].dy;
  224. comp->w = cmptparms[compno].w;
  225. comp->h = cmptparms[compno].h;
  226. comp->x0 = cmptparms[compno].x0;
  227. comp->y0 = cmptparms[compno].y0;
  228. comp->prec = cmptparms[compno].prec;
  229. comp->sgnd = cmptparms[compno].sgnd;
  230. comp->data = 0;
  231. }
  232. }
  233. return image;
  234. }