39 |
|
return(cm); |
40 |
|
} |
41 |
|
|
42 |
+ |
static void |
43 |
+ |
adjacent_ra_sizes(size_t bounds[2], size_t target) |
44 |
+ |
{ |
45 |
+ |
bounds[0] = 0; bounds[1] = 2048; |
46 |
+ |
while (bounds[1] < target) { |
47 |
+ |
bounds[0] = bounds[1]; |
48 |
+ |
bounds[1] += bounds[1]>>1; |
49 |
+ |
} |
50 |
+ |
} |
51 |
+ |
|
52 |
|
/* Resize color coefficient matrix */ |
53 |
|
CMATRIX * |
54 |
|
cm_resize(CMATRIX *cm, int nrows) |
55 |
|
{ |
56 |
+ |
size_t old_size, new_size, ra_bounds[2]; |
57 |
+ |
|
58 |
|
if (nrows == cm->nrows) |
59 |
|
return(cm); |
60 |
|
if (nrows <= 0) { |
61 |
|
cm_free(cm); |
62 |
|
return(NULL); |
63 |
|
} |
64 |
< |
cm = (CMATRIX *)realloc(cm, sizeof(CMATRIX) + |
65 |
< |
sizeof(COLOR)*(nrows*cm->ncols - 1)); |
66 |
< |
if (cm == NULL) |
67 |
< |
error(SYSTEM, "out of memory in cm_resize()"); |
64 |
> |
old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1); |
65 |
> |
adjacent_ra_sizes(ra_bounds, old_size); |
66 |
> |
new_size = sizeof(CMATRIX) + sizeof(COLOR)*(nrows*cm->ncols - 1); |
67 |
> |
if (nrows < cm->nrows ? new_size <= ra_bounds[0] : |
68 |
> |
new_size > ra_bounds[1]) { |
69 |
> |
adjacent_ra_sizes(ra_bounds, new_size); |
70 |
> |
cm = (CMATRIX *)realloc(cm, ra_bounds[1]); |
71 |
> |
if (cm == NULL) |
72 |
> |
error(SYSTEM, "out of memory in cm_resize()"); |
73 |
> |
} |
74 |
|
cm->nrows = nrows; |
75 |
|
return(cm); |
76 |
|
} |