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