Initial revision
[imager.git] / raw.c
CommitLineData
02d1d628
AMH
1#include "image.h"
2#include <stdio.h>
3#ifndef _MSC_VER
4#include <unistd.h>
5#endif
6#include <string.h>
7
8#define TRUE 1
9#define FALSE 0
10
11#define BSIZ 100*BUFSIZ
12
13/*
14
15 Image loader for raw files.
16
17 This is a barebones raw loader...
18
19 fd: filedescriptor
20 x: xsize
21 y: ysize
22 datachannels: the number of channels the file contains
23 storechannels: the bitmap of channels we will read
24 intrl: interlace flag,
25 0 = sample interleaving
26 1 = line interleaving
27 2 = image interleaving
28
29*/
30
31void
32expandchannels(unsigned char *inbuffer,unsigned char *outbuffer,int chunks,int datachannels,int storechannels) {
33 int ch,i;
34 if (inbuffer==outbuffer) return; /* Check if data is already in expanded format */
35 for(ch=0;ch<chunks;ch++) for (i=0;i<storechannels;i++) outbuffer[ch*storechannels+i]=inbuffer[ch*datachannels+i];
36}
37
38i_img *
39i_readraw(int fd,int x,int y,int datachannels,int storechannels,int intrl) {
40 i_img* im;
41 int rc,k;
42
43 unsigned char *inbuffer;
44 unsigned char *ilbuffer;
45 unsigned char *exbuffer;
46
47 int inbuflen,ilbuflen,exbuflen;
48
49 mm_log((1,"readraw(fd %d,x %d,y %d,datachannels %d,storechannels %d,intrl %d)\n",fd,x,y,datachannels,storechannels,intrl));
50
51 im=i_img_empty_ch(NULL,x,y,storechannels);
52
53 inbuflen=im->xsize*datachannels;
54 ilbuflen=inbuflen;
55 exbuflen=im->xsize*storechannels;
56 inbuffer=(unsigned char*)mymalloc(inbuflen);
57 mm_log((1,"inbuflen: %d, ilbuflen: %d, exbuflen: %d.\n",inbuflen,ilbuflen,exbuflen));
58
59 if (intrl==0) ilbuffer=inbuffer; else ilbuffer=(unsigned char*)mymalloc(inbuflen);
60 if (datachannels==storechannels) exbuffer=ilbuffer; else exbuffer=(unsigned char*)mymalloc(exbuflen);
61
62 k=0;
63 while(k<im->ysize) {
64 rc=myread(fd,inbuffer,inbuflen);
65 if (rc!=inbuflen) { fprintf(stderr,"Premature end of file.\n"); exit(2); }
66 interleave(inbuffer,ilbuffer,im->xsize,datachannels);
67 expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels);
68 memcpy(&(im->data[im->xsize*storechannels*k]),exbuffer,exbuflen);
69 k++;
70 }
71
72 myfree(inbuffer);
73 if (intrl!=0) myfree(ilbuffer);
74 if (datachannels!=storechannels) myfree(exbuffer);
75 return im;
76}
77
78
79
80undef_int
81i_writeraw(i_img* im,int fd) {
82 int rc;
83 mm_log((1,"writeraw(im 0x%x,fd %d)\n",im,fd));
84
85 if (im == NULL) { mm_log((1,"Image is empty\n")); return(0); }
86 rc=mywrite(fd,im->data,im->bytes);
87 if (rc!=im->bytes) { mm_log((1,"i_writeraw: Couldn't write to file\n")); return(0); }
88 return(1);
89}
90
91
92
93
94
95
96
97
98