add i_gsamp_bg/i_gsampf_bg functions, sample based versions of
[imager.git] / pnm.c
diff --git a/pnm.c b/pnm.c
index 609fbbc..ca35437 100644 (file)
--- a/pnm.c
+++ b/pnm.c
@@ -753,38 +753,23 @@ static
 int
 write_ppm_data_8(i_img *im, io_glue *ig, int want_channels) {
   int write_size = im->xsize * want_channels;
-  i_color *line_buf = mymalloc(sizeof(i_color) * im->xsize);
-  unsigned char *data = mymalloc(write_size);
+  int buf_size = im->xsize * im->channels;
+  unsigned char *data = mymalloc(buf_size);
   int y = 0;
-  int x;
-  int ch;
   int rc = 1;
   i_color bg;
 
   i_get_file_background(im, &bg);
   while (y < im->ysize && rc >= 0) {
-    i_color *linep = line_buf;
-    unsigned char *datap = data;
-
-    i_glin(im, 0, im->xsize, y, line_buf);
-    i_adapt_colors_bg(want_channels, im->channels, line_buf, im->xsize, &bg);
-    for (x = 0; x < im->xsize; ++x) {
-      for (ch = 0; ch < want_channels; ++ch) {
-       *datap++ = linep->channel[ch];
-      }
-      ++linep;
-    }
+    i_gsamp_bg(im, 0, im->xsize, y, data, want_channels, &bg);
     if (i_io_write(ig, data, write_size) != write_size) {
       i_push_error(errno, "could not write ppm data");
-      myfree(data);
-      myfree(line_buf);
       rc = 0;
       break;
     }
     ++y;
   }
   myfree(data);
-  myfree(line_buf);
 
   return rc;
 }
@@ -792,10 +777,14 @@ write_ppm_data_8(i_img *im, io_glue *ig, int want_channels) {
 static
 int
 write_ppm_data_16(i_img *im, io_glue *ig, int want_channels) {
+  int line_size = im->channels * im->xsize * sizeof(i_fsample_t);
   int sample_count = want_channels * im->xsize;
   int write_size = sample_count * 2;
-  i_fcolor *line_buf = mymalloc(sizeof(i_fcolor) * im->xsize);
+  i_fsample_t *line_buf = mymalloc(line_size);
+  i_fsample_t *samplep;
   unsigned char *write_buf = mymalloc(write_size);
+  unsigned char *writep;
+  int sample_num;
   int y = 0;
   int x, ch;
   int rc = 1;
@@ -804,20 +793,14 @@ write_ppm_data_16(i_img *im, io_glue *ig, int want_channels) {
   i_get_file_backgroundf(im, &bg);
 
   while (y < im->ysize) {
-    i_fcolor *linep = line_buf;
-    unsigned char *writep = write_buf;
-
-    i_glinf(im, 0, im->xsize, y, line_buf);
-    i_adapt_fcolors_bg(want_channels, im->channels, line_buf, im->xsize, &bg);
-    for (x = 0; x < im->xsize; ++x) {
-      for (ch = 0; ch < want_channels; ++ch) {
-       unsigned sample16 = SampleFTo16(linep->channel[ch]);
-       *writep++ = sample16 >> 8;
-       *writep++ = sample16 & 0xFF;
-      }
-      ++linep;
+    i_gsampf_bg(im, 0, im->xsize, y, line_buf, want_channels, &bg);
+    samplep = line_buf;
+    writep = write_buf;
+    for (sample_num = 0; sample_num < sample_count; ++sample_num) {
+      unsigned sample16 = SampleFTo16(*samplep++);
+      *writep++ = sample16 >> 8;
+      *writep++ = sample16 & 0xFF;
     }
-    
     if (i_io_write(ig, write_buf, write_size) != write_size) {
       i_push_error(errno, "could not write ppm data");
       rc = 0;