]> git.imager.perl.org - imager.git/blobdiff - raw.c
finally found which file contained the POD errors that
[imager.git] / raw.c
diff --git a/raw.c b/raw.c
index 6b1766948eb5d6913c9e744a8af879af3ae1fa2a..efdf44ae6117f2692475559cbac16dbeb671e359 100644 (file)
--- a/raw.c
+++ b/raw.c
@@ -1,4 +1,4 @@
-#include "image.h"
+#include "imager.h"
 #include <stdio.h>
 #include "iolayer.h"
 #ifndef _MSC_VER
@@ -8,10 +8,6 @@
 #include <errno.h>
 
 
-#define TRUE 1
-#define FALSE 0
-
-#define BSIZ 100*BUFSIZ
 
 /*
 
 
 */
 
+static
+void
+interleave(unsigned char *inbuffer,unsigned char *outbuffer,int rowsize,int channels) {
+  int ch,ind,i;
+  i=0;
+  if (inbuffer == outbuffer) return; /* Check if data is already in interleaved format */
+  for (ind=0; ind<rowsize; ind++) 
+    for (ch=0; ch<channels; ch++) 
+      outbuffer[i++] = inbuffer[rowsize*ch+ind]; 
+}
+
+static
 void
-expandchannels(unsigned char *inbuffer,unsigned char *outbuffer,int chunks,int datachannels,int storechannels) {
+expandchannels(unsigned char *inbuffer, unsigned char *outbuffer, 
+              int chunks, int datachannels, int storechannels) {
   int ch,i;
-  if (inbuffer==outbuffer) return; /* Check if data is already in expanded format */
-  for(ch=0;ch<chunks;ch++) for (i=0;i<storechannels;i++) outbuffer[ch*storechannels+i]=inbuffer[ch*datachannels+i];
+  if (inbuffer == outbuffer) return; /* Check if data is already in expanded format */
+  for(ch=0; ch<chunks; ch++) 
+    for (i=0; i<storechannels; i++) 
+      outbuffer[ch*storechannels+i] = inbuffer[ch*datachannels+i];
 }
 
 i_img *
 i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl) {
   i_img* im;
   int rc,k;
-  
+
   unsigned char *inbuffer;
   unsigned char *ilbuffer;
   unsigned char *exbuffer;
   
   int inbuflen,ilbuflen,exbuflen;
 
+  i_clear_error();
+  
   io_glue_commit_types(ig);
   mm_log((1, "i_readraw(ig %p,x %d,y %d,datachannels %d,storechannels %d,intrl %d)\n",
          ig, x, y, datachannels, storechannels, intrl));
   
   im = i_img_empty_ch(NULL,x,y,storechannels);
+  if (!im)
+    return NULL;
   
   inbuflen = im->xsize*datachannels;
   ilbuflen = inbuflen;
@@ -61,23 +76,39 @@ i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, i
   inbuffer = (unsigned char*)mymalloc(inbuflen);
   mm_log((1,"inbuflen: %d, ilbuflen: %d, exbuflen: %d.\n",inbuflen,ilbuflen,exbuflen));
 
-  if (intrl==0) ilbuffer=inbuffer; else ilbuffer=(unsigned char*)mymalloc(inbuflen);
-  if (datachannels==storechannels) exbuffer=ilbuffer; else exbuffer=(unsigned char*)mymalloc(exbuflen);
+  if (intrl==0) ilbuffer = inbuffer; 
+  else ilbuffer=mymalloc(inbuflen);
 
+  if (datachannels==storechannels) exbuffer=ilbuffer; 
+  else exbuffer= mymalloc(exbuflen);
+  
   k=0;
-  while(k<im->ysize) {
+  while( k<im->ysize ) {
     rc = ig->readcb(ig, inbuffer, inbuflen);
-    if (rc!=inbuflen) { fprintf(stderr,"Premature end of file.\n"); exit(2); }
+    if (rc != inbuflen) { 
+      if (rc < 0)
+       i_push_error(0, "error reading file");
+      else
+       i_push_error(0, "premature end of file");
+      i_img_destroy(im);
+      myfree(inbuffer);
+      if (intrl != 0) myfree(ilbuffer);
+      if (datachannels != storechannels) myfree(exbuffer);
+      return NULL;
+    }
     interleave(inbuffer,ilbuffer,im->xsize,datachannels);
     expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels);
-    /* FIXME? Do we ever want to save to a virtual image? */
+    /* FIXME: Do we ever want to save to a virtual image? */
     memcpy(&(im->idata[im->xsize*storechannels*k]),exbuffer,exbuflen);
     k++;
   }
 
   myfree(inbuffer);
-  if (intrl!=0) myfree(ilbuffer);
-  if (datachannels!=storechannels) myfree(exbuffer);
+  if (intrl != 0) myfree(ilbuffer);
+  if (datachannels != storechannels) myfree(exbuffer);
+
+  i_tags_add(&im->tags, "i_format", 0, "raw", -1, 0);
+
   return im;
 }
 
@@ -93,59 +124,46 @@ i_writeraw_wiol(i_img* im, io_glue *ig) {
   
   if (im == NULL) { mm_log((1,"Image is empty\n")); return(0); }
   if (!im->virtual) {
-    rc=ig->writecb(ig,im->idata,im->bytes);
-    if (rc!=im->bytes) { 
+    rc = ig->writecb(ig,im->idata,im->bytes);
+    if (rc != im->bytes) { 
       i_push_error(errno, "Could not write to file");
       mm_log((1,"i_writeraw: Couldn't write to file\n")); 
       return(0);
     }
-  }
-  else {
-    int y;
-    
+  } else {
     if (im->type == i_direct_type) {
       /* just save it as 8-bits, maybe support saving higher bit count
          raw images later */
       int line_size = im->xsize * im->channels;
       unsigned char *data = mymalloc(line_size);
-      if (data) {
-        int y = 0;
-        rc = line_size;
-        while (rc == line_size && y < im->ysize) {
-          i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
-          rc = ig->writecb(ig, data, line_size);
-          ++y;
-        }
-      }
-      else {
-        i_push_error(0, "Out of memory");
-        return 0;
+
+      int y = 0;
+      rc = line_size;
+      while (rc == line_size && y < im->ysize) {
+       i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
+       rc = ig->writecb(ig, data, line_size);
+       ++y;
       }
       if (rc != line_size) {
         i_push_error(errno, "write error");
         return 0;
       }
-    }
-    else {
+      myfree(data);
+    else {
       /* paletted image - assumes the caller puts the palette somewhere 
          else
       */
       int line_size = sizeof(i_palidx) * im->xsize;
       i_palidx *data = mymalloc(sizeof(i_palidx) * im->xsize);
-      if (data) {
-        int y = 0;
-        rc = line_size;
-        while (rc == line_size && y < im->ysize) {
-         i_gpal(im, 0, im->xsize, y, data);
-          rc = ig->writecb(ig, data, line_size);
-          ++y;
-        }
-        myfree(data);
-      }
-      else {
-        i_push_error(0, "Out of memory");
-        return 0;
+
+      int y = 0;
+      rc = line_size;
+      while (rc == line_size && y < im->ysize) {
+       i_gpal(im, 0, im->xsize, y, data);
+       rc = ig->writecb(ig, data, line_size);
+       ++y;
       }
+      myfree(data);
       if (rc != line_size) {
         i_push_error(errno, "write error");
         return 0;
@@ -153,5 +171,7 @@ i_writeraw_wiol(i_img* im, io_glue *ig) {
     }
   }
 
+  ig->closecb(ig);
+
   return(1);
 }