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 | * iclose and iflush - |
---|
38 | * |
---|
39 | * Paul Haeberli - 1984 |
---|
40 | * |
---|
41 | */ |
---|
42 | #include <stdio.h> |
---|
43 | #include <errno.h> |
---|
44 | #include <stdlib.h> |
---|
45 | #include <unistd.h> |
---|
46 | #include <fcntl.h> |
---|
47 | #include "image.h" |
---|
48 | |
---|
49 | int iclose(IMAGE *image) |
---|
50 | { |
---|
51 | long tablesize; |
---|
52 | |
---|
53 | iflush(image); |
---|
54 | img_optseek(image, 0); |
---|
55 | if (image->flags&_IOWRT) { |
---|
56 | if(image->dorev) |
---|
57 | cvtimage(image); |
---|
58 | if (img_write(image,(char *)image,sizeof(IMAGE)) != sizeof(IMAGE)) { |
---|
59 | i_errhdlr("iflush: error on write of image header\n"); |
---|
60 | return EOF; |
---|
61 | } |
---|
62 | if(image->dorev) |
---|
63 | cvtimage(image); |
---|
64 | if(ISRLE(image->type)) { |
---|
65 | img_optseek(image, 512L); |
---|
66 | tablesize = image->ysize*image->zsize*sizeof(long); |
---|
67 | if(image->dorev) |
---|
68 | cvtlongs(image->rowstart,tablesize); |
---|
69 | if (img_write(image,(char *)image->rowstart,tablesize) != tablesize) { |
---|
70 | i_errhdlr("iflush: error on write of rowstart\n"); |
---|
71 | return EOF; |
---|
72 | } |
---|
73 | if(image->dorev) |
---|
74 | cvtlongs(image->rowsize,tablesize); |
---|
75 | if (img_write(image,(char *)image->rowsize,tablesize) != tablesize) { |
---|
76 | i_errhdlr("iflush: error on write of rowsize\n"); |
---|
77 | return EOF; |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | if(image->base) { |
---|
82 | free(image->base); |
---|
83 | image->base = 0; |
---|
84 | } |
---|
85 | if(image->tmpbuf) { |
---|
86 | free(image->tmpbuf); |
---|
87 | image->tmpbuf = 0; |
---|
88 | } |
---|
89 | if(ISRLE(image->type)) { |
---|
90 | free(image->rowstart); |
---|
91 | image->rowstart = 0; |
---|
92 | free(image->rowsize); |
---|
93 | image->rowsize = 0; |
---|
94 | } |
---|
95 | return(close(image->file)); |
---|
96 | } |
---|
97 | |
---|
98 | int iflush(IMAGE *image) |
---|
99 | { |
---|
100 | IMushort *base; |
---|
101 | |
---|
102 | if ( (image->flags&_IOWRT) |
---|
103 | && (base=image->base)!=NULL && (image->ptr-base)>0) { |
---|
104 | if (putrow(image, base, image->y,image->z)!=image->xsize) { |
---|
105 | image->flags |= _IOERR; |
---|
106 | return(EOF); |
---|
107 | } |
---|
108 | } |
---|
109 | return 0; |
---|
110 | } |
---|