Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 162 lines (133 sloc) 4.336 kb
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
1 #include "image.h"
2 #include <stdio.h>
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
3 #include "iolayer.h"
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
4 #ifndef _MSC_VER
5 #include <unistd.h>
6 #endif
7 #include <string.h>
faa9b3e7 » Tony Cook
2001-08-13 Egads
8 #include <errno.h>
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
9
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
10
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
11
12 /*
13
14 Image loader for raw files.
15
16 This is a barebones raw loader...
17
18 fd: filedescriptor
19 x: xsize
20 y: ysize
21 datachannels: the number of channels the file contains
22 storechannels: the bitmap of channels we will read
23 intrl: interlace flag,
24 0 = sample interleaving
25 1 = line interleaving
26 2 = image interleaving
27
28 */
29
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
30 static
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
31 void
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
32 interleave(unsigned char *inbuffer,unsigned char *outbuffer,int rowsize,int channels) {
33 int ch,ind,i;
34 i=0;
35 if (inbuffer == outbuffer) return; /* Check if data is already in interleaved format */
36 for (ind=0; ind<rowsize; ind++)
37 for (ch=0; ch<channels; ch++)
38 outbuffer[i++] = inbuffer[rowsize*ch+ind];
39 }
40
41 static
42 void
43 expandchannels(unsigned char *inbuffer, unsigned char *outbuffer,
44 int chunks, int datachannels, int storechannels) {
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
45 int ch,i;
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
46 if (inbuffer == outbuffer) return; /* Check if data is already in expanded format */
47 for(ch=0; ch<chunks; ch++)
48 for (i=0; i<storechannels; i++)
49 outbuffer[ch*storechannels+i] = inbuffer[ch*datachannels+i];
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
50 }
51
52 i_img *
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
53 i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl) {
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
54 i_img* im;
55 int rc,k;
56
57 unsigned char *inbuffer;
58 unsigned char *ilbuffer;
59 unsigned char *exbuffer;
60
61 int inbuflen,ilbuflen,exbuflen;
62
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
63 io_glue_commit_types(ig);
64 mm_log((1, "i_readraw(ig %p,x %d,y %d,datachannels %d,storechannels %d,intrl %d)\n",
65 ig, x, y, datachannels, storechannels, intrl));
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
66
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
67 im = i_img_empty_ch(NULL,x,y,storechannels);
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
68
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
69 inbuflen = im->xsize*datachannels;
70 ilbuflen = inbuflen;
71 exbuflen = im->xsize*storechannels;
72 inbuffer = (unsigned char*)mymalloc(inbuflen);
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
73 mm_log((1,"inbuflen: %d, ilbuflen: %d, exbuflen: %d.\n",inbuflen,ilbuflen,exbuflen));
74
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
75 if (intrl==0) ilbuffer = inbuffer;
76 else ilbuffer=mymalloc(inbuflen);
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
77
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
78 if (datachannels==storechannels) exbuffer=ilbuffer;
79 else exbuffer= mymalloc(exbuflen);
80
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
81 k=0;
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
82 while( k<im->ysize ) {
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
83 rc = ig->readcb(ig, inbuffer, inbuflen);
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
84 if (rc != inbuflen) { fprintf(stderr,"Premature end of file.\n"); exit(2); }
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
85 interleave(inbuffer,ilbuffer,im->xsize,datachannels);
86 expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels);
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
87 /* FIXME: Do we ever want to save to a virtual image? */
faa9b3e7 » Tony Cook
2001-08-13 Egads
88 memcpy(&(im->idata[im->xsize*storechannels*k]),exbuffer,exbuflen);
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
89 k++;
90 }
91
92 myfree(inbuffer);
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
93 if (intrl != 0) myfree(ilbuffer);
94 if (datachannels != storechannels) myfree(exbuffer);
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
95 return im;
96 }
97
98
99
100 undef_int
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
101 i_writeraw_wiol(i_img* im, io_glue *ig) {
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
102 int rc;
faa9b3e7 » Tony Cook
2001-08-13 Egads
103
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
104 io_glue_commit_types(ig);
faa9b3e7 » Tony Cook
2001-08-13 Egads
105 i_clear_error();
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
106 mm_log((1,"writeraw(im %p,ig %p)\n", im, ig));
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
107
108 if (im == NULL) { mm_log((1,"Image is empty\n")); return(0); }
faa9b3e7 » Tony Cook
2001-08-13 Egads
109 if (!im->virtual) {
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
110 rc = ig->writecb(ig,im->idata,im->bytes);
111 if (rc != im->bytes) {
faa9b3e7 » Tony Cook
2001-08-13 Egads
112 i_push_error(errno, "Could not write to file");
113 mm_log((1,"i_writeraw: Couldn't write to file\n"));
114 return(0);
115 }
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
116 } else {
faa9b3e7 » Tony Cook
2001-08-13 Egads
117 int y;
118
119 if (im->type == i_direct_type) {
120 /* just save it as 8-bits, maybe support saving higher bit count
121 raw images later */
122 int line_size = im->xsize * im->channels;
123 unsigned char *data = mymalloc(line_size);
a73aeb5f » Arnar Mar Hrafnkelsson
2001-11-01 Fixed most outstanding memory leaks that are revealed in the test cases.
124
125 int y = 0;
126 rc = line_size;
127 while (rc == line_size && y < im->ysize) {
128 i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
129 rc = ig->writecb(ig, data, line_size);
130 ++y;
faa9b3e7 » Tony Cook
2001-08-13 Egads
131 }
132 if (rc != line_size) {
133 i_push_error(errno, "write error");
134 return 0;
135 }
a73aeb5f » Arnar Mar Hrafnkelsson
2001-11-01 Fixed most outstanding memory leaks that are revealed in the test cases.
136 myfree(data);
5473b91d » Arnar Mar Hrafnkelsson
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
137 } else {
faa9b3e7 » Tony Cook
2001-08-13 Egads
138 /* paletted image - assumes the caller puts the palette somewhere
139 else
140 */
141 int line_size = sizeof(i_palidx) * im->xsize;
142 i_palidx *data = mymalloc(sizeof(i_palidx) * im->xsize);
a73aeb5f » Arnar Mar Hrafnkelsson
2001-11-01 Fixed most outstanding memory leaks that are revealed in the test cases.
143
144 int y = 0;
145 rc = line_size;
146 while (rc == line_size && y < im->ysize) {
147 i_gpal(im, 0, im->xsize, y, data);
148 rc = ig->writecb(ig, data, line_size);
149 ++y;
faa9b3e7 » Tony Cook
2001-08-13 Egads
150 }
a73aeb5f » Arnar Mar Hrafnkelsson
2001-11-01 Fixed most outstanding memory leaks that are revealed in the test cases.
151 myfree(data);
faa9b3e7 » Tony Cook
2001-08-13 Egads
152 if (rc != line_size) {
153 i_push_error(errno, "write error");
154 return 0;
155 }
156 }
895dbd34 » Arnar Mar Hrafnkelsson
2001-06-06 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
157 }
10461f9a » Tony Cook
2002-01-04 enhanced iolayer
158
159 ig->closecb(ig);
160
02d1d628 » Arnar Mar Hrafnkelsson
2001-03-25 Initial revision
161 return(1);
162 }
Something went wrong with that request. Please try again.