Index: /branches/eam_branches/ipp-20220316/Ohana/src/opihi/cmd.data/rdjpg.c
===================================================================
--- /branches/eam_branches/ipp-20220316/Ohana/src/opihi/cmd.data/rdjpg.c	(revision 42350)
+++ /branches/eam_branches/ipp-20220316/Ohana/src/opihi/cmd.data/rdjpg.c	(revision 42351)
@@ -1,5 +1,5 @@
 # include "data.h"
-#include <setjmp.h>
 # include "jpeglib.h"
+# include <setjmp.h>
 
 struct my_error_mgr {
@@ -27,6 +27,9 @@
 int rdjpg (int argc, char **argv) {
   
-  int i, N, blank;
+  int N;
   Buffer *buf;
+
+  struct jpeg_decompress_struct cinfo;
+  struct my_error_mgr jerr;
 
   int VERBOSE = FALSE;
@@ -41,13 +44,5 @@
   }
 
-  struct jpeg_decompress_struct cinfo;
-  struct my_error_mgr jerr;
-
-  JSAMPARRAY buffer;		/* Output row buffer */
-  int row_stride;		/* physical row width in output buffer */
-
-  // struct my_error_mgr jerr; : needed for error handling (deferred)
-
-  /* test if file exists */
+  // open input file for read
   FILE *f = fopen (argv[2], "r");
   if (f == (FILE *) NULL) {
@@ -56,5 +51,5 @@
   }
 
-  /* find matrix, free old data */
+  // find matrix, free old data
   if ((buf = SelectBuffer (argv[1], ANYBUFFER, TRUE)) == NULL) {
     fclose (f);
@@ -64,14 +59,15 @@
   gfits_free_header (&buf[0].header);
 
-  /* save file name */
+  // save file name in buffer info
   char *filename = filebasename (argv[2]);
   strcpy (buf[0].file, filename);
   free (filename);
 
-  int status = FALSE;
-
+  // the setup for the error handling seems a bit circular, but this
+  // example from the libjpeg example.c seems to work?
   cinfo.err = jpeg_std_error(&jerr.pub);
   jerr.pub.error_exit = my_error_exit;
-  /* Establish the setjmp return context for my_error_exit to use. */
+
+  // an error in the jpeg code will jump (goto) here:
   if (setjmp(jerr.setjmp_buffer)) {
     /* If we get here, the JPEG code has signaled an error.
@@ -89,33 +85,26 @@
   jpeg_start_decompress(&cinfo);
 
-  /* We may need to do some setup of our own at this point before reading
-   * the data.  After jpeg_start_decompress() we have the correct scaled
-   * output image dimensions available, as well as the output colormap
-   * if we asked for color quantization.
-   * In this example, we need to make an output work buffer of the right size.
-   */ 
+  /* JSAMPLEs per row in output buffer */
+  int Nx = cinfo.output_width;
+  int Ny = cinfo.output_height;
+  int Npix_row = Nx * cinfo.output_components;
 
-  /* JSAMPLEs per row in output buffer */
-  row_stride = cinfo.output_width * cinfo.output_components;
-  // Nx = cinfo.output_width or row_stride
-  // Ny = cinfo.output_height
-
-  CreateBuffer (buf, cinfo.output_width, cinfo.output_height, -32, 0.0, 1.0);
+  CreateBuffer (buf, Nx, Ny, -32, 0.0, 1.0);
   float *Vout = (float *)buf[0].matrix.buffer;
 
   /* Make a one-row-high sample array that will go away when done with image */
-  buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
+  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, Npix_row, 1);
 
   // read each of the scanlines
-  for (int iy = 0; iy < cinfo.output_height; iy++) {
+  for (int iy = 0; iy < Ny; iy++) {
     jpeg_read_scanlines(&cinfo, buffer, 1);
 
-    /* Assume put_scanline_someplace wants a pointer and sample count. */
-    for (int ix = 0; ix < cinfo.output_width; ix ++) {
+    // for now just copy R + G + B as the total value:
+    for (int ix = 0; ix < Nx; ix ++) {
       unsigned char Rpix = buffer[0][3*ix + 0];
       unsigned char Gpix = buffer[0][3*ix + 1];
       unsigned char Bpix = buffer[0][3*ix + 2];
       float value = Rpix + Gpix + Bpix;
-      Vout[cinfo.output_width*iy + ix] = value;
+      Vout[Nx*iy + ix] = value;
     }
   }
@@ -126,12 +115,4 @@
   fclose (f);
   
-  /* 
-  if (!status) {
-    gprint (GP_ERR, "problem reading file, buffer not opened\n");
-    DeleteBuffer (buf);
-    return (FALSE);
-    } 
-  */
-
   buf[0].bitpix = buf[0].header.bitpix;    /* store the original values */
   buf[0].bscale = buf[0].header.bscale;    /* store the original values */
@@ -141,26 +122,4 @@
   if (VERBOSE) gprint (GP_LOG, "read "OFF_T_FMT" bytes from %s into buffer %s\n", buf[0].header.datasize + buf[0].matrix.datasize, argv[2], argv[1]);
 
-  blank = 0xffff;
-
-  /** now - convert the matrix values to floats for internal use **/
-  // gfits_convert_format (&buf[0].header, &buf[0].matrix, -32, 1.0, 0.0, blank, gfits_get_unsign_mode());
-
   return (TRUE);
 }
-
-# if (0)
-// XXX not sure about error handling here
-// cinfo.err = jpeg_std_error(&jerr.pub);
-// jerr.pub.error_exit = my_error_exit;
-
-/* Establish the setjmp return context for my_error_exit to use. */
-if (setjmp(jerr.setjmp_buffer)) {
-  /* If we get here, the JPEG code has signaled an error.
-   * We need to clean up the JPEG object, close the input file, and return.
-   */
-  jpeg_destroy_decompress(&cinfo);
-  fclose(f);
-  return 0;
-}
-# endif
-
