[4] | 1 | /****************************************************************************** |
---|
| 2 | * SAGE - Scalable Adaptive Graphics Environment |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
| 5 | * University of Illinois at Chicago |
---|
| 6 | * |
---|
| 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright |
---|
| 13 | * notice, this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above |
---|
| 15 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 16 | * in the documentation and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 18 | * the names of its contributors may be used to endorse or promote |
---|
| 19 | * products derived from this software without specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | * |
---|
| 33 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ |
---|
| 34 | * |
---|
| 35 | *****************************************************************************/ |
---|
| 36 | |
---|
| 37 | /* |
---|
| 38 | * putrow, getrow - |
---|
| 39 | * |
---|
| 40 | * Paul Haeberli - 1984 |
---|
| 41 | * |
---|
| 42 | */ |
---|
| 43 | #include <stdio.h> |
---|
| 44 | #include <errno.h> |
---|
| 45 | #include "image.h" |
---|
| 46 | |
---|
| 47 | int putrow( IMAGE *image, IMushort *buffer, int y, int z ) |
---|
| 48 | { |
---|
| 49 | register IMushort *sptr; |
---|
| 50 | register unsigned char *cptr; |
---|
| 51 | register unsigned int x; |
---|
| 52 | register IMulong min, max; |
---|
| 53 | register int cnt; |
---|
| 54 | |
---|
| 55 | if( !(image->flags & (_IORW|_IOWRT)) ) |
---|
| 56 | return -1; |
---|
| 57 | if(image->dim<3) |
---|
| 58 | z = 0; |
---|
| 59 | if(image->dim<2) |
---|
| 60 | y = 0; |
---|
| 61 | if(ISVERBATIM(image->type)) { |
---|
| 62 | switch(BPP(image->type)) { |
---|
| 63 | case 1: |
---|
| 64 | min = image->min; |
---|
| 65 | max = image->max; |
---|
| 66 | cptr = (unsigned char *)image->tmpbuf; |
---|
| 67 | sptr = buffer; |
---|
| 68 | for(x=image->xsize; x--;) { |
---|
| 69 | *cptr = *sptr++; |
---|
| 70 | if (*cptr > max) max = *cptr; |
---|
| 71 | if (*cptr < min) min = *cptr; |
---|
| 72 | cptr++; |
---|
| 73 | } |
---|
| 74 | image->min = min; |
---|
| 75 | image->max = max; |
---|
| 76 | img_seek(image,y,z); |
---|
| 77 | cnt = image->xsize; |
---|
| 78 | if (img_write(image,(char *)image->tmpbuf,cnt) != cnt) |
---|
| 79 | return -1; |
---|
| 80 | else |
---|
| 81 | return cnt; |
---|
| 82 | |
---|
| 83 | case 2: |
---|
| 84 | min = image->min; |
---|
| 85 | max = image->max; |
---|
| 86 | sptr = buffer; |
---|
| 87 | for(x=image->xsize; x--;) { |
---|
| 88 | if (*sptr > max) max = *sptr; |
---|
| 89 | if (*sptr < min) min = *sptr; |
---|
| 90 | sptr++; |
---|
| 91 | } |
---|
| 92 | image->min = min; |
---|
| 93 | image->max = max; |
---|
| 94 | img_seek(image,y,z); |
---|
| 95 | cnt = image->xsize<<1; |
---|
| 96 | if(image->dorev) |
---|
| 97 | cvtshorts(buffer,cnt); |
---|
| 98 | if (img_write(image,(char *)buffer,cnt) != cnt) { |
---|
| 99 | if(image->dorev) |
---|
| 100 | cvtshorts(buffer,cnt); |
---|
| 101 | return -1; |
---|
| 102 | } else { |
---|
| 103 | if(image->dorev) |
---|
| 104 | cvtshorts(buffer,cnt); |
---|
| 105 | return image->xsize; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | default: |
---|
| 109 | i_errhdlr("putrow: wierd bpp\n"); |
---|
| 110 | } |
---|
| 111 | } else if(ISRLE(image->type)) { |
---|
| 112 | switch(BPP(image->type)) { |
---|
| 113 | case 1: |
---|
| 114 | min = image->min; |
---|
| 115 | max = image->max; |
---|
| 116 | sptr = buffer; |
---|
| 117 | for(x=image->xsize; x--;) { |
---|
| 118 | if (*sptr > max) max = *sptr; |
---|
| 119 | if (*sptr < min) min = *sptr; |
---|
| 120 | sptr++; |
---|
| 121 | } |
---|
| 122 | image->min = min; |
---|
| 123 | image->max = max; |
---|
| 124 | cnt = img_rle_compact(buffer,2,image->tmpbuf,1,image->xsize); |
---|
| 125 | img_setrowsize(image,cnt,y,z); |
---|
| 126 | img_seek(image,y,z); |
---|
| 127 | if (img_write(image,(char *)image->tmpbuf,cnt) != cnt) |
---|
| 128 | return -1; |
---|
| 129 | else |
---|
| 130 | return image->xsize; |
---|
| 131 | break; |
---|
| 132 | |
---|
| 133 | case 2: |
---|
| 134 | min = image->min; |
---|
| 135 | max = image->max; |
---|
| 136 | sptr = buffer; |
---|
| 137 | for(x=image->xsize; x--;) { |
---|
| 138 | if (*sptr > max) max = *sptr; |
---|
| 139 | if (*sptr < min) min = *sptr; |
---|
| 140 | sptr++; |
---|
| 141 | } |
---|
| 142 | image->min = min; |
---|
| 143 | image->max = max; |
---|
| 144 | cnt = img_rle_compact(buffer,2,image->tmpbuf,2,image->xsize); |
---|
| 145 | cnt <<= 1; |
---|
| 146 | img_setrowsize(image,cnt,y,z); |
---|
| 147 | img_seek(image,y,z); |
---|
| 148 | if(image->dorev) |
---|
| 149 | cvtshorts(image->tmpbuf,cnt); |
---|
| 150 | if (img_write(image,(char *)image->tmpbuf,cnt) != cnt) { |
---|
| 151 | if(image->dorev) |
---|
| 152 | cvtshorts(image->tmpbuf,cnt); |
---|
| 153 | return -1; |
---|
| 154 | } else { |
---|
| 155 | if(image->dorev) |
---|
| 156 | cvtshorts(image->tmpbuf,cnt); |
---|
| 157 | return image->xsize; |
---|
| 158 | } |
---|
| 159 | break; |
---|
| 160 | |
---|
| 161 | default: |
---|
| 162 | i_errhdlr("putrow: wierd bpp\n"); |
---|
| 163 | } |
---|
| 164 | } else |
---|
| 165 | i_errhdlr("putrow: wierd image type\n"); |
---|
| 166 | return 0; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | int getrow( IMAGE *image, IMushort *buffer, int y, int z ) |
---|
| 170 | { |
---|
| 171 | register int i; |
---|
| 172 | register unsigned char *cptr; |
---|
| 173 | register IMushort *sptr; |
---|
| 174 | register int cnt; |
---|
| 175 | |
---|
| 176 | if( !(image->flags & (_IORW|_IOREAD)) ) |
---|
| 177 | return -1; |
---|
| 178 | if(image->dim<3) |
---|
| 179 | z = 0; |
---|
| 180 | if(image->dim<2) |
---|
| 181 | y = 0; |
---|
| 182 | img_seek(image, y, z); |
---|
| 183 | if(ISVERBATIM(image->type)) { |
---|
| 184 | switch(BPP(image->type)) { |
---|
| 185 | case 1: |
---|
| 186 | if (img_read(image,(char *)image->tmpbuf,image->xsize) |
---|
| 187 | != image->xsize) |
---|
| 188 | return -1; |
---|
| 189 | else { |
---|
| 190 | cptr = (unsigned char *)image->tmpbuf; |
---|
| 191 | sptr = buffer; |
---|
| 192 | for(i=image->xsize; i--;) |
---|
| 193 | *sptr++ = *cptr++; |
---|
| 194 | } |
---|
| 195 | return image->xsize; |
---|
| 196 | case 2: |
---|
| 197 | cnt = image->xsize<<1; |
---|
| 198 | if (img_read(image,(char *)buffer,cnt) != cnt) |
---|
| 199 | return -1; |
---|
| 200 | else { |
---|
| 201 | if(image->dorev) |
---|
| 202 | cvtshorts(buffer,cnt); |
---|
| 203 | return image->xsize; |
---|
| 204 | } |
---|
| 205 | default: |
---|
| 206 | i_errhdlr("getrow: wierd bpp\n"); |
---|
| 207 | break; |
---|
| 208 | } |
---|
| 209 | } else if(ISRLE(image->type)) { |
---|
| 210 | switch(BPP(image->type)) { |
---|
| 211 | case 1: |
---|
| 212 | if( (cnt = img_getrowsize(image)) == -1 ) |
---|
| 213 | return -1; |
---|
| 214 | if( img_read(image,(char *)image->tmpbuf,cnt) != cnt ) |
---|
| 215 | return -1; |
---|
| 216 | else { |
---|
| 217 | img_rle_expand(image->tmpbuf,1,buffer,2); |
---|
| 218 | return image->xsize; |
---|
| 219 | } |
---|
| 220 | case 2: |
---|
| 221 | if( (cnt = img_getrowsize(image)) == -1 ) |
---|
| 222 | return -1; |
---|
| 223 | if( cnt != img_read(image,(char *)image->tmpbuf,cnt) ) |
---|
| 224 | return -1; |
---|
| 225 | else { |
---|
| 226 | if(image->dorev) |
---|
| 227 | cvtshorts(image->tmpbuf,cnt); |
---|
| 228 | img_rle_expand(image->tmpbuf,2,buffer,2); |
---|
| 229 | return image->xsize; |
---|
| 230 | } |
---|
| 231 | default: |
---|
| 232 | i_errhdlr("getrow: wierd bpp\n"); |
---|
| 233 | break; |
---|
| 234 | } |
---|
| 235 | } else |
---|
| 236 | i_errhdlr("getrow: wierd image type\n"); |
---|
| 237 | return 0; |
---|
| 238 | } |
---|