jas_seq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2002 Michael David Adams.
  5. * All rights reserved.
  6. */
  7. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * Sequence/Matrix Library
  64. *
  65. * $Id: jas_seq.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include <stdlib.h>
  71. #include <assert.h>
  72. #include <math.h>
  73. #include "jasper/jas_seq.h"
  74. #include "jasper/jas_malloc.h"
  75. #include "jasper/jas_math.h"
  76. /******************************************************************************\
  77. * Constructors and destructors.
  78. \******************************************************************************/
  79. jas_matrix_t *jas_seq2d_create(int xstart, int ystart, int xend, int yend)
  80. {
  81. jas_matrix_t *matrix;
  82. assert(xstart <= xend && ystart <= yend);
  83. if (!(matrix = jas_matrix_create(yend - ystart, xend - xstart))) {
  84. return 0;
  85. }
  86. matrix->xstart_ = xstart;
  87. matrix->ystart_ = ystart;
  88. matrix->xend_ = xend;
  89. matrix->yend_ = yend;
  90. return matrix;
  91. }
  92. jas_matrix_t *jas_matrix_create(int numrows, int numcols)
  93. {
  94. jas_matrix_t *matrix;
  95. int i;
  96. if (!(matrix = jas_malloc(sizeof(jas_matrix_t)))) {
  97. return 0;
  98. }
  99. matrix->flags_ = 0;
  100. matrix->numrows_ = numrows;
  101. matrix->numcols_ = numcols;
  102. matrix->rows_ = 0;
  103. matrix->maxrows_ = numrows;
  104. matrix->data_ = 0;
  105. matrix->datasize_ = numrows * numcols;
  106. if (matrix->maxrows_ > 0) {
  107. if (!(matrix->rows_ = jas_alloc2(matrix->maxrows_,
  108. sizeof(jas_seqent_t *)))) {
  109. jas_matrix_destroy(matrix);
  110. return 0;
  111. }
  112. }
  113. if (matrix->datasize_ > 0) {
  114. if (!(matrix->data_ = jas_alloc2(matrix->datasize_,
  115. sizeof(jas_seqent_t)))) {
  116. jas_matrix_destroy(matrix);
  117. return 0;
  118. }
  119. }
  120. for (i = 0; i < numrows; ++i) {
  121. matrix->rows_[i] = &matrix->data_[i * matrix->numcols_];
  122. }
  123. for (i = 0; i < matrix->datasize_; ++i) {
  124. matrix->data_[i] = 0;
  125. }
  126. matrix->xstart_ = 0;
  127. matrix->ystart_ = 0;
  128. matrix->xend_ = matrix->numcols_;
  129. matrix->yend_ = matrix->numrows_;
  130. return matrix;
  131. }
  132. void jas_matrix_destroy(jas_matrix_t *matrix)
  133. {
  134. if (matrix->data_) {
  135. assert(!(matrix->flags_ & JAS_MATRIX_REF));
  136. jas_free(matrix->data_);
  137. matrix->data_ = 0;
  138. }
  139. if (matrix->rows_) {
  140. jas_free(matrix->rows_);
  141. matrix->rows_ = 0;
  142. }
  143. jas_free(matrix);
  144. }
  145. jas_seq2d_t *jas_seq2d_copy(jas_seq2d_t *x)
  146. {
  147. jas_matrix_t *y;
  148. int i;
  149. int j;
  150. y = jas_seq2d_create(jas_seq2d_xstart(x), jas_seq2d_ystart(x), jas_seq2d_xend(x),
  151. jas_seq2d_yend(x));
  152. assert(y);
  153. for (i = 0; i < x->numrows_; ++i) {
  154. for (j = 0; j < x->numcols_; ++j) {
  155. *jas_matrix_getref(y, i, j) = jas_matrix_get(x, i, j);
  156. }
  157. }
  158. return y;
  159. }
  160. jas_matrix_t *jas_matrix_copy(jas_matrix_t *x)
  161. {
  162. jas_matrix_t *y;
  163. int i;
  164. int j;
  165. y = jas_matrix_create(x->numrows_, x->numcols_);
  166. for (i = 0; i < x->numrows_; ++i) {
  167. for (j = 0; j < x->numcols_; ++j) {
  168. *jas_matrix_getref(y, i, j) = jas_matrix_get(x, i, j);
  169. }
  170. }
  171. return y;
  172. }
  173. /******************************************************************************\
  174. * Bind operations.
  175. \******************************************************************************/
  176. void jas_seq2d_bindsub(jas_matrix_t *s, jas_matrix_t *s1, int xstart, int ystart,
  177. int xend, int yend)
  178. {
  179. jas_matrix_bindsub(s, s1, ystart - s1->ystart_, xstart - s1->xstart_,
  180. yend - s1->ystart_ - 1, xend - s1->xstart_ - 1);
  181. }
  182. void jas_matrix_bindsub(jas_matrix_t *mat0, jas_matrix_t *mat1, int r0, int c0,
  183. int r1, int c1)
  184. {
  185. int i;
  186. if (mat0->data_) {
  187. if (!(mat0->flags_ & JAS_MATRIX_REF)) {
  188. jas_free(mat0->data_);
  189. }
  190. mat0->data_ = 0;
  191. mat0->datasize_ = 0;
  192. }
  193. if (mat0->rows_) {
  194. jas_free(mat0->rows_);
  195. mat0->rows_ = 0;
  196. }
  197. mat0->flags_ |= JAS_MATRIX_REF;
  198. mat0->numrows_ = r1 - r0 + 1;
  199. mat0->numcols_ = c1 - c0 + 1;
  200. mat0->maxrows_ = mat0->numrows_;
  201. mat0->rows_ = jas_alloc2(mat0->maxrows_, sizeof(jas_seqent_t *));
  202. for (i = 0; i < mat0->numrows_; ++i) {
  203. mat0->rows_[i] = mat1->rows_[r0 + i] + c0;
  204. }
  205. mat0->xstart_ = mat1->xstart_ + c0;
  206. mat0->ystart_ = mat1->ystart_ + r0;
  207. mat0->xend_ = mat0->xstart_ + mat0->numcols_;
  208. mat0->yend_ = mat0->ystart_ + mat0->numrows_;
  209. }
  210. /******************************************************************************\
  211. * Arithmetic operations.
  212. \******************************************************************************/
  213. int jas_matrix_cmp(jas_matrix_t *mat0, jas_matrix_t *mat1)
  214. {
  215. int i;
  216. int j;
  217. if (mat0->numrows_ != mat1->numrows_ || mat0->numcols_ !=
  218. mat1->numcols_) {
  219. return 1;
  220. }
  221. for (i = 0; i < mat0->numrows_; i++) {
  222. for (j = 0; j < mat0->numcols_; j++) {
  223. if (jas_matrix_get(mat0, i, j) != jas_matrix_get(mat1, i, j)) {
  224. return 1;
  225. }
  226. }
  227. }
  228. return 0;
  229. }
  230. void jas_matrix_divpow2(jas_matrix_t *matrix, int n)
  231. {
  232. int i;
  233. int j;
  234. jas_seqent_t *rowstart;
  235. int rowstep;
  236. jas_seqent_t *data;
  237. rowstep = jas_matrix_rowstep(matrix);
  238. for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
  239. rowstart += rowstep) {
  240. for (j = matrix->numcols_, data = rowstart; j > 0; --j,
  241. ++data) {
  242. *data = (*data >= 0) ? ((*data) >> n) :
  243. (-((-(*data)) >> n));
  244. }
  245. }
  246. }
  247. void jas_matrix_clip(jas_matrix_t *matrix, jas_seqent_t minval, jas_seqent_t maxval)
  248. {
  249. int i;
  250. int j;
  251. jas_seqent_t v;
  252. jas_seqent_t *rowstart;
  253. jas_seqent_t *data;
  254. int rowstep;
  255. rowstep = jas_matrix_rowstep(matrix);
  256. for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
  257. rowstart += rowstep) {
  258. data = rowstart;
  259. for (j = matrix->numcols_, data = rowstart; j > 0; --j,
  260. ++data) {
  261. v = *data;
  262. if (v < minval) {
  263. *data = minval;
  264. } else if (v > maxval) {
  265. *data = maxval;
  266. }
  267. }
  268. }
  269. }
  270. void jas_matrix_asr(jas_matrix_t *matrix, int n)
  271. {
  272. int i;
  273. int j;
  274. jas_seqent_t *rowstart;
  275. int rowstep;
  276. jas_seqent_t *data;
  277. assert(n >= 0);
  278. rowstep = jas_matrix_rowstep(matrix);
  279. for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
  280. rowstart += rowstep) {
  281. for (j = matrix->numcols_, data = rowstart; j > 0; --j,
  282. ++data) {
  283. *data >>= n;
  284. }
  285. }
  286. }
  287. void jas_matrix_asl(jas_matrix_t *matrix, int n)
  288. {
  289. int i;
  290. int j;
  291. jas_seqent_t *rowstart;
  292. int rowstep;
  293. jas_seqent_t *data;
  294. rowstep = jas_matrix_rowstep(matrix);
  295. for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
  296. rowstart += rowstep) {
  297. for (j = matrix->numcols_, data = rowstart; j > 0; --j,
  298. ++data) {
  299. *data <<= n;
  300. }
  301. }
  302. }
  303. /******************************************************************************\
  304. * Code.
  305. \******************************************************************************/
  306. int jas_matrix_resize(jas_matrix_t *matrix, int numrows, int numcols)
  307. {
  308. int size;
  309. int i;
  310. size = numrows * numcols;
  311. if (size > matrix->datasize_ || numrows > matrix->maxrows_) {
  312. return -1;
  313. }
  314. matrix->numrows_ = numrows;
  315. matrix->numcols_ = numcols;
  316. for (i = 0; i < numrows; ++i) {
  317. matrix->rows_[i] = &matrix->data_[numcols * i];
  318. }
  319. return 0;
  320. }
  321. void jas_matrix_setall(jas_matrix_t *matrix, jas_seqent_t val)
  322. {
  323. int i;
  324. int j;
  325. jas_seqent_t *rowstart;
  326. int rowstep;
  327. jas_seqent_t *data;
  328. rowstep = jas_matrix_rowstep(matrix);
  329. for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
  330. rowstart += rowstep) {
  331. for (j = matrix->numcols_, data = rowstart; j > 0; --j,
  332. ++data) {
  333. *data = val;
  334. }
  335. }
  336. }
  337. jas_matrix_t *jas_seq2d_input(FILE *in)
  338. {
  339. jas_matrix_t *matrix;
  340. int i;
  341. int j;
  342. long x;
  343. int numrows;
  344. int numcols;
  345. int xoff;
  346. int yoff;
  347. if (fscanf(in, "%d %d", &xoff, &yoff) != 2)
  348. return 0;
  349. if (fscanf(in, "%d %d", &numcols, &numrows) != 2)
  350. return 0;
  351. if (!(matrix = jas_seq2d_create(xoff, yoff, xoff + numcols, yoff + numrows)))
  352. return 0;
  353. if (jas_matrix_numrows(matrix) != numrows || jas_matrix_numcols(matrix) != numcols) {
  354. abort();
  355. }
  356. /* Get matrix data. */
  357. for (i = 0; i < jas_matrix_numrows(matrix); i++) {
  358. for (j = 0; j < jas_matrix_numcols(matrix); j++) {
  359. if (fscanf(in, "%ld", &x) != 1) {
  360. jas_matrix_destroy(matrix);
  361. return 0;
  362. }
  363. jas_matrix_set(matrix, i, j, JAS_CAST(jas_seqent_t, x));
  364. }
  365. }
  366. return matrix;
  367. }
  368. int jas_seq2d_output(jas_matrix_t *matrix, FILE *out)
  369. {
  370. #define MAXLINELEN 80
  371. int i;
  372. int j;
  373. jas_seqent_t x;
  374. char buf[MAXLINELEN + 1];
  375. char sbuf[MAXLINELEN + 1];
  376. int n;
  377. fprintf(out, "%d %d\n", (int)jas_seq2d_xstart(matrix),
  378. (int)jas_seq2d_ystart(matrix));
  379. fprintf(out, "%d %d\n", (int)jas_matrix_numcols(matrix),
  380. (int)jas_matrix_numrows(matrix));
  381. buf[0] = '\0';
  382. for (i = 0; i < jas_matrix_numrows(matrix); ++i) {
  383. for (j = 0; j < jas_matrix_numcols(matrix); ++j) {
  384. x = jas_matrix_get(matrix, i, j);
  385. sprintf(sbuf, "%s%4ld", (strlen(buf) > 0) ? " " : "",
  386. JAS_CAST(long, x));
  387. n = strlen(buf);
  388. if (n + strlen(sbuf) > MAXLINELEN) {
  389. fputs(buf, out);
  390. fputs("\n", out);
  391. buf[0] = '\0';
  392. }
  393. strcat(buf, sbuf);
  394. if (j == jas_matrix_numcols(matrix) - 1) {
  395. fputs(buf, out);
  396. fputs("\n", out);
  397. buf[0] = '\0';
  398. }
  399. }
  400. }
  401. fputs(buf, out);
  402. return 0;
  403. }