close.c
1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* iclose and iflush -
*
* Paul Haeberli - 1984
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "image.h"
int iclose(IMAGE *image)
{
int tablesize, ret;
iflush(image);
img_optseek(image, 0);
if (image->flags&_IOWRT) {
if(image->dorev)
cvtimage(image);
if (img_write(image,(char *)image,sizeof(IMAGE)) != sizeof(IMAGE)) {
i_errhdlr("iclose: error on write of image header\n");
return EOF;
}
if(image->dorev)
cvtimage(image);
if(ISRLE(image->type)) {
img_optseek(image, 512);
tablesize = image->ysize*image->zsize*sizeof(int);
if(image->dorev)
cvtlongs(image->rowstart,tablesize);
if (img_write(image,(char *)(image->rowstart),tablesize) != tablesize) {
i_errhdlr("iclose: error on write of rowstart\n");
return EOF;
}
if(image->dorev)
cvtlongs(image->rowsize,tablesize);
if (img_write(image,(char *)(image->rowsize),tablesize) != tablesize) {
i_errhdlr("iclose: error on write of rowsize\n");
return EOF;
}
}
}
if(image->base) {
free(image->base);
image->base = 0;
}
if(image->tmpbuf) {
free(image->tmpbuf);
image->tmpbuf = 0;
}
if(ISRLE(image->type)) {
free(image->rowstart);
image->rowstart = 0;
free(image->rowsize);
image->rowsize = 0;
}
ret = close(image->file);
if(ret != 0)
i_errhdlr("iclose: error on close of file\n");
free(image);
return ret;
}
int iflush(IMAGE *image)
{
unsigned short *base;
if ( (image->flags&_IOWRT)
&& (base=image->base)!=NULL && (image->ptr-base)>0) {
if (putrow(image, base, image->y,image->z)!=image->xsize) {
image->flags |= _IOERR;
return(EOF);
}
}
return(0);
}