[gimp/gimp-2-10] plug-ins: port twain to GEGL, 8-bit port only



commit 4464f31d6da5e603339c1126896d49b6e623567c
Author: Michael Natterer <mitch gimp org>
Date:   Fri Jul 12 23:50:23 2019 +0200

    plug-ins: port twain to GEGL, 8-bit port only
    
    This has only be tested to build, somebody please test if
    it actually works.
    
    (cherry picked from commit 12bed7be5bab5f733daff7f79210ae9f35858879)

 plug-ins/twain/Makefile.am |   1 +
 plug-ins/twain/twain.c     | 754 ++++++++++++++++++++++++---------------------
 2 files changed, 408 insertions(+), 347 deletions(-)
---
diff --git a/plug-ins/twain/Makefile.am b/plug-ins/twain/Makefile.am
index 64c0a9e42f..4e094a530d 100644
--- a/plug-ins/twain/Makefile.am
+++ b/plug-ins/twain/Makefile.am
@@ -54,6 +54,7 @@ LDADD = \
        $(libgimpcolor) \
        $(libgimpbase)  \
        $(GLIB_LIBS)    \
+       $(GEGL_LIBS)    \
        $(RT_LIBS)      \
        $(INTLLIBS)     \
        $(twain_LIBS)   \
diff --git a/plug-ins/twain/twain.c b/plug-ins/twain/twain.c
index 53a9927a0c..f80ec29e96 100644
--- a/plug-ins/twain/twain.c
+++ b/plug-ins/twain/twain.c
@@ -103,23 +103,29 @@
  * Definition of the run states
  */
 #define RUN_STANDARD 0
-#define RUN_DUMP 1
+#define RUN_DUMP     1
 #define RUN_READDUMP 2
 
 /* Global variables */
 pTW_SESSION twSession = NULL;
 
-static char        *destBuf = NULL;
+static char *destBuf = NULL;
 #ifdef _DEBUG
-static int         twain_run_mode = RUN_STANDARD;
+static int twain_run_mode = RUN_STANDARD;
 #endif
 
 /* Forward declarations */
-void preTransferCallback(void *);
-int  beginTransferCallback(pTW_IMAGEINFO, void *);
-int  dataTransferCallback(pTW_IMAGEINFO, pTW_IMAGEMEMXFER, void *);
-int  endTransferCallback(int, int, void *);
-void postTransferCallback(int, void *);
+void preTransferCallback   (void             *clientData);
+int  beginTransferCallback (pTW_IMAGEINFO     imageInfo,
+                            void             *clientData);
+int  dataTransferCallback  (pTW_IMAGEINFO     imageInfo,
+                            pTW_IMAGEMEMXFER  imageMemXfer,
+                            void             *clientData);
+int  endTransferCallback   (int               completionState,
+                            int               pendingCount,
+                            void             *clientData);
+void postTransferCallback  (int               pendingCount,
+                            void             *clientData);
 
 static void query (void);
 static void run   (const gchar      *name,
@@ -128,7 +134,7 @@ static void run   (const gchar      *name,
                   gint             *nreturn_vals,
                   GimpParam       **return_vals);
 
-/* This plug-in's functions */
+
 const GimpPlugInInfo PLUG_IN_INFO =
 {
   NULL,    /* init_proc */
@@ -137,13 +143,14 @@ const GimpPlugInInfo PLUG_IN_INFO =
   run,     /* run_proc */
 };
 
-extern void set_gimp_PLUG_IN_INFO_PTR(GimpPlugInInfo *);
+extern void set_gimp_PLUG_IN_INFO_PTR (GimpPlugInInfo *);
 
 /* Data structure holding data between runs */
 /* Currently unused... Eventually may be used
  * to track dialog data.
  */
-typedef struct {
+typedef struct
+{
   gchar  sourceName[34];
   gfloat xResolution;
   gfloat yResolution;
@@ -165,12 +172,14 @@ static TwainValues twainvals =
 };
 
 /* The standard callback functions */
-TXFR_CB_FUNCS standardCbFuncs = {
+TXFR_CB_FUNCS standardCbFuncs =
+{
   preTransferCallback,
   beginTransferCallback,
   dataTransferCallback,
   endTransferCallback,
-  postTransferCallback };
+  postTransferCallback
+};
 
 /******************************************************************
  * Dump handling
@@ -178,30 +187,32 @@ TXFR_CB_FUNCS standardCbFuncs = {
 
 #ifdef _DEBUG
 /* The dumper callback functions */
-TXFR_CB_FUNCS dumperCbFuncs = {
+TXFR_CB_FUNCS dumperCbFuncs =
+{
   dumpPreTransferCallback,
   dumpBeginTransferCallback,
   dumpDataTransferCallback,
   dumpEndTransferCallback,
-  dumpPostTransferCallback };
+  dumpPostTransferCallback
+};
 
 void
-setRunMode(char *argv[])
+setRunMode (char *argv[])
 {
-  char *exeName = strrchr(argv[0], '\\') + 1;
+  char *exeName = strrchr (argv[0], '\\') + 1;
 
-  LogMessage("Executable name: %s\n", exeName);
+  LogMessage ("Executable name: %s\n", exeName);
 
-  if (!_stricmp(exeName, DUMP_NAME))
+  if (!_stricmp (exeName, DUMP_NAME))
     twain_run_mode = RUN_DUMP;
 
-  if (!_stricmp(exeName, RUNDUMP_NAME))
+  if (!_stricmp (exeName, RUNDUMP_NAME))
     twain_run_mode = RUN_READDUMP;
 }
 #endif /* _DEBUG */
 
 #ifndef TWAIN_ALTERNATE_MAIN
-MAIN()
+MAIN ()
 #endif
 
 int
@@ -222,7 +233,7 @@ scanImage (void)
  * the TWAIN runtime.
  */
 static pTW_IDENTITY
-getAppIdentity(void)
+getAppIdentity (void)
 {
   pTW_IDENTITY appIdentity = g_new (TW_IDENTITY, 1);
 
@@ -252,23 +263,24 @@ getAppIdentity(void)
  * datasource manager calls.
  */
 pTW_SESSION
-initializeTwain(void)
+initializeTwain (void)
 {
   pTW_IDENTITY appIdentity;
 
   /* Get our application's identity */
-  appIdentity = getAppIdentity();
+  appIdentity = getAppIdentity ();
 
   /* Create a new session object */
-  twSession = newSession(appIdentity);
+  twSession = newSession (appIdentity);
 
   /* Register our image transfer callback functions */
 #ifdef _DEBUG
   if (twain_run_mode == RUN_DUMP)
-    registerTransferCallbacks(twSession, &dumperCbFuncs, NULL);
+    registerTransferCallbacks (twSession, &dumperCbFuncs, NULL);
   else
 #endif /* _DEBUG */
-    registerTransferCallbacks(twSession, &standardCbFuncs, NULL);
+    registerTransferCallbacks (twSession, &standardCbFuncs, NULL);
+
   return twSession;
 }
 
@@ -303,38 +315,38 @@ query (void)
   if (twain_run_mode == RUN_DUMP)
     {
       /* the installation of the plugin */
-      gimp_install_procedure(PLUG_IN_D_NAME,
-                             PLUG_IN_DESCRIPTION,
-                             PLUG_IN_HELP,
-                             PLUG_IN_AUTHOR,
-                             PLUG_IN_COPYRIGHT,
-                             PLUG_IN_VERSION,
-                             "TWAIN (Dump)...",
-                             NULL,
-                             GIMP_PLUGIN,
-                             NUMBER_IN_ARGS,
-                             NUMBER_OUT_ARGS,
-                             args,
-                             return_vals);
+      gimp_install_procedure (PLUG_IN_D_NAME,
+                              PLUG_IN_DESCRIPTION,
+                              PLUG_IN_HELP,
+                              PLUG_IN_AUTHOR,
+                              PLUG_IN_COPYRIGHT,
+                              PLUG_IN_VERSION,
+                              "TWAIN (Dump)...",
+                              NULL,
+                              GIMP_PLUGIN,
+                              NUMBER_IN_ARGS,
+                              NUMBER_OUT_ARGS,
+                              args,
+                              return_vals);
 
       gimp_plugin_menu_register (PLUG_IN_D_NAME, "<Image>/File/Create/Acquire");
     }
   else if (twain_run_mode == RUN_READDUMP)
     {
       /* the installation of the plugin */
-      gimp_install_procedure(PLUG_IN_R_NAME,
-                             PLUG_IN_DESCRIPTION,
-                             PLUG_IN_HELP,
-                             PLUG_IN_AUTHOR,
-                             PLUG_IN_COPYRIGHT,
-                             PLUG_IN_VERSION,
-                             "TWAIN (Read)...",
-                             NULL,
-                             GIMP_PLUGIN,
-                             NUMBER_IN_ARGS,
-                             NUMBER_OUT_ARGS,
-                             args,
-                             return_vals);
+      gimp_install_procedure (PLUG_IN_R_NAME,
+                              PLUG_IN_DESCRIPTION,
+                              PLUG_IN_HELP,
+                              PLUG_IN_AUTHOR,
+                              PLUG_IN_COPYRIGHT,
+                              PLUG_IN_VERSION,
+                              "TWAIN (Read)...",
+                              NULL,
+                              GIMP_PLUGIN,
+                              NUMBER_IN_ARGS,
+                              NUMBER_OUT_ARGS,
+                              args,
+                              return_vals);
 
       gimp_plugin_menu_register (PLUG_IN_R_NAME, "<Image>/File/Create/Acquire");
     }
@@ -342,19 +354,19 @@ query (void)
 #endif /* _DEBUG */
     {
       /* the installation of the plugin */
-      gimp_install_procedure(PLUG_IN_NAME,
-                             PLUG_IN_DESCRIPTION,
-                             PLUG_IN_HELP,
-                             PLUG_IN_AUTHOR,
-                             PLUG_IN_COPYRIGHT,
-                             PLUG_IN_VERSION,
-                             N_("_Scanner/Camera..."),
-                             NULL,
-                             GIMP_PLUGIN,
-                             NUMBER_IN_ARGS,
-                             NUMBER_OUT_ARGS,
-                             args,
-                             return_vals);
+      gimp_install_procedure (PLUG_IN_NAME,
+                              PLUG_IN_DESCRIPTION,
+                              PLUG_IN_HELP,
+                              PLUG_IN_AUTHOR,
+                              PLUG_IN_COPYRIGHT,
+                              PLUG_IN_VERSION,
+                              N_("_Scanner/Camera..."),
+                              NULL,
+                              GIMP_PLUGIN,
+                              NUMBER_IN_ARGS,
+                              NUMBER_OUT_ARGS,
+                              args,
+                              return_vals);
 
       gimp_plugin_menu_register (PLUG_IN_NAME, "<Image>/File/Create/Acquire");
     }
@@ -382,54 +394,58 @@ run (const gchar      *name,
   /* Initialize the return values
    * Always return at least the status to the caller.
    */
-  values[0].type = GIMP_PDB_STATUS;
+  values[0].type          = GIMP_PDB_STATUS;
   values[0].data.d_status = GIMP_PDB_SUCCESS;
+
   *nreturn_vals = 1;
   *return_vals = values;
 
   INIT_I18N ();
+  gegl_init (NULL, NULL);
 
   /* Before we get any further, verify that we have
    * TWAIN and that there is actually a datasource
    * to be used in doing the acquire.
    */
-  if (!twainIsAvailable()) {
-    values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
-    return;
-  }
+  if (! twainIsAvailable ())
+    {
+      values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
+      return;
+    }
 
   /* Set up the rest of the return parameters */
-  values[1].type = GIMP_PDB_INT32;
+  values[1].type         = GIMP_PDB_INT32;
   values[1].data.d_int32 = 0;
-  values[2].type = GIMP_PDB_INT32ARRAY;
+  values[2].type              = GIMP_PDB_INT32ARRAY;
   values[2].data.d_int32array = g_new (gint32, MAX_IMAGES);
 
   /* How are we running today? */
-  switch (run_mode) {
-  case GIMP_RUN_INTERACTIVE:
-    /* Retrieve values from the last run...
-     * Currently ignored
-     */
-    gimp_get_data(PLUG_IN_NAME, &twainvals);
-    break;
-
-  case GIMP_RUN_NONINTERACTIVE:
-    /* Currently, we don't do non-interactive calls.
-     * Bail if someone tries to call us non-interactively
-     */
-    values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
-    return;
-
-  case GIMP_RUN_WITH_LAST_VALS:
-    /* Retrieve values from the last run...
-     * Currently ignored
-     */
-    gimp_get_data(PLUG_IN_NAME, &twainvals);
-    break;
-
-  default:
-    break;
-  } /* switch */
+  switch (run_mode)
+    {
+    case GIMP_RUN_INTERACTIVE:
+      /* Retrieve values from the last run...
+       * Currently ignored
+       */
+      gimp_get_data (PLUG_IN_NAME, &twainvals);
+      break;
+
+    case GIMP_RUN_NONINTERACTIVE:
+      /* Currently, we don't do non-interactive calls.
+       * Bail if someone tries to call us non-interactively
+       */
+      values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
+      return;
+
+    case GIMP_RUN_WITH_LAST_VALS:
+      /* Retrieve values from the last run...
+       * Currently ignored
+       */
+      gimp_get_data (PLUG_IN_NAME, &twainvals);
+      break;
+
+    default:
+      break;
+    }
 
   /* Have we succeeded so far? */
   if (values[0].data.d_status == GIMP_PDB_SUCCESS)
@@ -438,21 +454,25 @@ run (const gchar      *name,
   /* Check to make sure we got at least one valid
    * image.
    */
-  if (values[1].data.d_int32 > 0) {
-    /* An image was captured from the TWAIN
-     * datasource.  Do final Interactive
-     * steps.
-     */
-    if (run_mode == GIMP_RUN_INTERACTIVE) {
-      /* Store variable states for next run */
-      gimp_set_data(PLUG_IN_NAME, &twainvals, sizeof (TwainValues));
+  if (values[1].data.d_int32 > 0)
+    {
+      /* An image was captured from the TWAIN
+       * datasource.  Do final Interactive
+       * steps.
+       */
+      if (run_mode == GIMP_RUN_INTERACTIVE)
+        {
+          /* Store variable states for next run */
+          gimp_set_data (PLUG_IN_NAME, &twainvals, sizeof (TwainValues));
+        }
+
+      /* Set return values */
+      *nreturn_vals = 3;
+    }
+  else
+    {
+      values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
     }
-
-    /* Set return values */
-    *nreturn_vals = 3;
-  } else {
-    values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
-  }
 }
 
 /***********************************************************************
@@ -462,14 +482,15 @@ run (const gchar      *name,
 /* Data used to carry data between each of
  * the callback function calls.
  */
-typedef struct {
-  gint32 image_id;
-  gint32 layer_id;
-  GimpPixelRgn pixel_rgn;
-  GimpDrawable *drawable;
-  pTW_PALETTE8 paletteData;
-  int totalPixels;
-  int completedPixels;
+typedef struct
+{
+  gint32        image_id;
+  gint32        layer_id;
+  GeglBuffer   *buffer;
+  const Babl   *format;
+  pTW_PALETTE8  paletteData;
+  int           totalPixels;
+  int           completedPixels;
 } ClientDataStruct, *pClientDataStruct;
 
 /*
@@ -479,7 +500,7 @@ typedef struct {
  * are transferred.  Set up the one time only stuff.
  */
 void
-preTransferCallback(void *clientData)
+preTransferCallback (void *clientData)
 {
   /* Initialize our progress dialog */
   gimp_progress_init (_("Transferring data from scanner/camera"));
@@ -492,104 +513,118 @@ preTransferCallback(void *clientData)
  * of each image transfer.
  */
 int
-beginTransferCallback(pTW_IMAGEINFO imageInfo, void *clientData)
+beginTransferCallback (pTW_IMAGEINFO  imageInfo,
+                       void          *clientData)
 {
-  int imageType, layerType;
-
   pClientDataStruct theClientData = g_new (ClientDataStruct, 1);
 
+  const Babl *format;
+  int         imageType;
+  int         layerType;
+
+
 #ifdef _DEBUG
-  logBegin(imageInfo, clientData);
+  logBegin (imageInfo, clientData);
 #endif
 
   /* Decide on the image type */
-  switch (imageInfo->PixelType) {
-  case TWPT_BW:
-  case TWPT_GRAY:
-    /* Set up the image and layer types */
-    imageType = GIMP_GRAY;
-    layerType = GIMP_GRAY_IMAGE;
-    break;
-
-  case TWPT_RGB:
-    /* Set up the image and layer types */
-    imageType = GIMP_RGB;
-    layerType = GIMP_RGB_IMAGE;
-    break;
-
-  case TWPT_PALETTE:
-    /* Get the palette data */
-    theClientData->paletteData = g_new (TW_PALETTE8, 1);
-    twSession->twRC = callDSM(APP_IDENTITY(twSession), DS_IDENTITY(twSession),
-                             DG_IMAGE, DAT_PALETTE8, MSG_GET,
-                             (TW_MEMREF) theClientData->paletteData);
-    if (twSession->twRC != TWRC_SUCCESS)
-      return FALSE;
+  switch (imageInfo->PixelType)
+    {
+    case TWPT_BW:
+    case TWPT_GRAY:
+      /* Set up the image and layer types */
+      imageType = GIMP_GRAY;
+      layerType = GIMP_GRAY_IMAGE;
 
-    switch (theClientData->paletteData->PaletteType) {
-    case TWPA_RGB:
+      format = babl_format ("Y' u8");
+      break;
+
+    case TWPT_RGB:
       /* Set up the image and layer types */
       imageType = GIMP_RGB;
       layerType = GIMP_RGB_IMAGE;
+
+      format = babl_format ("R'G'B' u8");
       break;
 
-    case TWPA_GRAY:
-      /* Set up the image and layer types */
-      imageType = GIMP_GRAY;
-      layerType = GIMP_GRAY_IMAGE;
+    case TWPT_PALETTE:
+      /* Get the palette data */
+      theClientData->paletteData = g_new (TW_PALETTE8, 1);
+      twSession->twRC = callDSM (APP_IDENTITY (twSession),
+                                 DS_IDENTITY (twSession),
+                                 DG_IMAGE, DAT_PALETTE8, MSG_GET,
+                                 (TW_MEMREF) theClientData->paletteData);
+      if (twSession->twRC != TWRC_SUCCESS)
+        return FALSE;
+
+      switch (theClientData->paletteData->PaletteType)
+        {
+        case TWPA_RGB:
+          /* Set up the image and layer types */
+          imageType = GIMP_RGB;
+          layerType = GIMP_RGB_IMAGE;
+
+          format = babl_format ("R'G'B' u8");
+          break;
+
+        case TWPA_GRAY:
+          /* Set up the image and layer types */
+          imageType = GIMP_GRAY;
+          layerType = GIMP_GRAY_IMAGE;
+
+          format = babl_format ("Y' u8");
+          break;
+
+        default:
+          return FALSE;
+        }
       break;
 
     default:
+      /* We don't know how to deal with anything other than
+       * the types listed above.  Bail for any other image
+       * type.
+       */
       return FALSE;
     }
-    break;
-
-  default:
-    /* We don't know how to deal with anything other than
-     * the types listed above.  Bail for any other image
-     * type.
-     */
-    return FALSE;
-  }
 
   /* Create the GIMP image */
-  theClientData->image_id = gimp_image_new(imageInfo->ImageWidth,
-                                          imageInfo->ImageLength, imageType);
+  theClientData->image_id = gimp_image_new (imageInfo->ImageWidth,
+                                            imageInfo->ImageLength,
+                                            imageType);
 
   /* Set the actual resolution */
   gimp_image_set_resolution (theClientData->image_id,
-                             FIX32ToFloat(imageInfo->XResolution),
-                             FIX32ToFloat(imageInfo->YResolution));
+                             FIX32ToFloat (imageInfo->XResolution),
+                             FIX32ToFloat (imageInfo->YResolution));
   gimp_image_set_unit (theClientData->image_id, GIMP_UNIT_INCH);
 
   /* Create a layer */
-  theClientData->layer_id = gimp_layer_new(theClientData->image_id,
-                                          _("Background"),
-                                          imageInfo->ImageWidth,
-                                          imageInfo->ImageLength,
-                                          layerType, 100, GIMP_LAYER_MODE_NORMAL);
+  theClientData->layer_id = gimp_layer_new (theClientData->image_id,
+                                            _("Background"),
+                                            imageInfo->ImageWidth,
+                                            imageInfo->ImageLength,
+                                            layerType, 100,
+                                            GIMP_LAYER_MODE_NORMAL);
 
   /* Add the layer to the image */
-  gimp_image_insert_layer(theClientData->image_id,
-                          theClientData->layer_id, -1, 0);
+  gimp_image_insert_layer (theClientData->image_id,
+                           theClientData->layer_id, -1, 0);
 
   /* Update the progress dialog */
-  theClientData->totalPixels = imageInfo->ImageWidth * imageInfo->ImageLength;
+  theClientData->totalPixels     = imageInfo->ImageWidth * imageInfo->ImageLength;
   theClientData->completedPixels = 0;
-  gimp_progress_update((double) 0);
 
-  /* Get our drawable */
-  theClientData->drawable = gimp_drawable_get(theClientData->layer_id);
+  gimp_progress_update (0.0);
 
-  /* Initialize a pixel region for writing to the image */
-  gimp_pixel_rgn_init(&(theClientData->pixel_rgn), theClientData->drawable,
-                     0, 0, imageInfo->ImageWidth, imageInfo->ImageLength,
-                     TRUE, FALSE);
+  theClientData->buffer = gimp_drawable_get_buffer (theClientData->layer_id);
+  theClientData->format = format;
 
   /* Store our client data for the data transfer callbacks */
   if (clientData)
     g_free (clientData);
-  setClientData(twSession, (void *) theClientData);
+
+  setClientData (twSession, (void *) theClientData);
 
   /* Make sure to return TRUE to continue the image
    * transfer
@@ -610,40 +645,43 @@ beginTransferCallback(pTW_IMAGEINFO imageInfo, void *clientData)
  */
 static char bitMasks[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
 static int
-bitTransferCallback(pTW_IMAGEINFO imageInfo,
-                   pTW_IMAGEMEMXFER imageMemXfer,
-                   void *clientData)
+bitTransferCallback (pTW_IMAGEINFO     imageInfo,
+                     pTW_IMAGEMEMXFER  imageMemXfer,
+                     void             *clientData)
 {
-  int row, col, offset;
+  int   row, col, offset;
   char *srcBuf;
-  int rows = imageMemXfer->Rows;
-  int cols = imageMemXfer->Columns;
+  int   rows = imageMemXfer->Rows;
+  int   cols = imageMemXfer->Columns;
   pClientDataStruct theClientData = (pClientDataStruct) clientData;
 
   /* Allocate a buffer as necessary */
-  if (!destBuf)
+  if (! destBuf)
     destBuf = g_new (char, rows * cols);
 
   /* Unpack the image data from bits into bytes */
   srcBuf = (char *) imageMemXfer->Memory.TheMem;
   offset = 0;
-  for (row = 0; row < rows; row++) {
-    for (col = 0; col < cols; col++) {
-      char byte = srcBuf[(row * imageMemXfer->BytesPerRow) + (col / 8)];
-      destBuf[offset++] = ((byte & bitMasks[col % 8]) != 0) ? 255 : 0;
+  for (row = 0; row < rows; row++)
+    {
+      for (col = 0; col < cols; col++)
+        {
+          char byte = srcBuf[(row * imageMemXfer->BytesPerRow) + (col / 8)];
+          destBuf[offset++] = ((byte & bitMasks[col % 8]) != 0) ? 255 : 0;
+        }
     }
-  }
 
   /* Update the complete chunk */
-  gimp_pixel_rgn_set_rect(&(theClientData->pixel_rgn),
-                         (guchar *) destBuf,
-                         imageMemXfer->XOffset, imageMemXfer->YOffset,
-                         cols, rows);
+  gegl_buffer_set (theClientData->buffer,
+                   GEGL_RECTANGLE (imageMemXfer->XOffset, imageMemXfer->YOffset,
+                                   cols, rows), 0,
+                   theClientData->format, destBuf,
+                   GEGL_AUTO_ROWSTRIDE);
 
   /* Update the user on our progress */
   theClientData->completedPixels += (cols * rows);
-  gimp_progress_update((double) theClientData->completedPixels /
-                      (double) theClientData->totalPixels);
+  gimp_progress_update ((double) theClientData->completedPixels /
+                        (double) theClientData->totalPixels);
 
   return TRUE;
 }
@@ -659,19 +697,19 @@ bitTransferCallback(pTW_IMAGEINFO imageInfo,
  * 8 bits per sample understood by GIMP.
  */
 static int
-oneBytePerSampleTransferCallback(pTW_IMAGEINFO imageInfo,
-                    pTW_IMAGEMEMXFER imageMemXfer,
-                    void *clientData)
+oneBytePerSampleTransferCallback (pTW_IMAGEINFO     imageInfo,
+                                  pTW_IMAGEMEMXFER  imageMemXfer,
+                                  void             *clientData)
 {
-  int row;
+  int   row;
   char *srcBuf;
-  int bytesPerPixel = imageInfo->BitsPerPixel / 8;
-  int rows = imageMemXfer->Rows;
-  int cols = imageMemXfer->Columns;
+  int   bytesPerPixel = imageInfo->BitsPerPixel / 8;
+  int   rows = imageMemXfer->Rows;
+  int   cols = imageMemXfer->Columns;
   pClientDataStruct theClientData = (pClientDataStruct) clientData;
 
   /* Allocate a buffer as necessary */
-  if (!destBuf)
+  if (! destBuf)
     destBuf = g_new (char, rows * cols * bytesPerPixel);
 
   /* The bytes coming from the source may not be padded in
@@ -683,23 +721,25 @@ oneBytePerSampleTransferCallback(pTW_IMAGEINFO imageInfo,
    * a non-padded chunk for GIMP.
    */
   srcBuf = (char *) imageMemXfer->Memory.TheMem;
-  for (row = 0; row < rows; row++) {
-    /* Copy the current row */
-    memcpy((destBuf + (row * bytesPerPixel * cols)),
-          (srcBuf + (row * imageMemXfer->BytesPerRow)),
-          (bytesPerPixel * cols));
-  }
+  for (row = 0; row < rows; row++)
+    {
+      /* Copy the current row */
+      memcpy ((destBuf + (row * bytesPerPixel * cols)),
+              (srcBuf + (row * imageMemXfer->BytesPerRow)),
+              (bytesPerPixel * cols));
+    }
 
   /* Update the complete chunk */
-  gimp_pixel_rgn_set_rect(&(theClientData->pixel_rgn),
-                         (guchar *) destBuf,
-                         imageMemXfer->XOffset, imageMemXfer->YOffset,
-                         cols, rows);
+  gegl_buffer_set (theClientData->buffer,
+                   GEGL_RECTANGLE (imageMemXfer->XOffset, imageMemXfer->YOffset,
+                                   cols, rows), 0,
+                   theClientData->format, destBuf,
+                   GEGL_AUTO_ROWSTRIDE);
 
   /* Update the user on our progress */
   theClientData->completedPixels += (cols * rows);
-  gimp_progress_update((double) theClientData->completedPixels /
-                      (double) theClientData->totalPixels);
+  gimp_progress_update ((double) theClientData->completedPixels /
+                        (double) theClientData->totalPixels);
 
   return TRUE;
 }
@@ -712,22 +752,21 @@ oneBytePerSampleTransferCallback(pTW_IMAGEINFO imageInfo,
  * the image type is Grayscale or RGB.
  */
 static int
-twoBytesPerSampleTransferCallback(pTW_IMAGEINFO imageInfo,
-                    pTW_IMAGEMEMXFER imageMemXfer,
-                    void *clientData)
+twoBytesPerSampleTransferCallback (pTW_IMAGEINFO     imageInfo,
+                                   pTW_IMAGEMEMXFER  imageMemXfer,
+                                   void             *clientData)
 {
-  static float ratio = 0.00390625;
-  int row, col, sample;
-  char *destByte;
-  int rows = imageMemXfer->Rows;
-  int cols = imageMemXfer->Columns;
-
-  TW_UINT16 *samplePtr;
+  static float  ratio = 0.00390625;
+  int           row, col, sample;
+  char         *destByte;
+  int           rows = imageMemXfer->Rows;
+  int           cols = imageMemXfer->Columns;
+  TW_UINT16    *samplePtr;
 
   pClientDataStruct theClientData = (pClientDataStruct) clientData;
 
   /* Allocate a buffer as necessary */
-  if (!destBuf)
+  if (! destBuf)
     destBuf = g_new (char, rows * cols * imageInfo->SamplesPerPixel);
 
   /* The bytes coming from the source may not be padded in
@@ -741,41 +780,45 @@ twoBytesPerSampleTransferCallback(pTW_IMAGEINFO imageInfo,
    * per sample.
    */
   /* Work through the rows */
-  for (row = 0; row < rows; row++) {
-    /* The start of this source row */
-    samplePtr = (TW_UINT16 *)
-      ((char *) imageMemXfer->Memory.TheMem + (row * imageMemXfer->BytesPerRow));
-
-    /* The start of this dest row */
-    destByte = destBuf + (row * imageInfo->SamplesPerPixel * cols);
-
-    /* Work through the columns */
-    for (col = 0; col < cols; col++) {
-      /* Finally, work through each of the samples */
-      for (sample = 0; sample < imageInfo->SamplesPerPixel; sample++) {
-                               /* Get the value */
-       TW_UINT16 value = *samplePtr;
-
-                               /* Move the sample pointer */
-       samplePtr++;
-
-                               /* Place in the destination */
-       *destByte = (char) ((float) value * (float) ratio);
-       destByte++;
-      }
+  for (row = 0; row < rows; row++)
+    {
+      /* The start of this source row */
+      samplePtr = (TW_UINT16 *)
+        ((char *) imageMemXfer->Memory.TheMem + (row * imageMemXfer->BytesPerRow));
+
+      /* The start of this dest row */
+      destByte = destBuf + (row * imageInfo->SamplesPerPixel * cols);
+
+      /* Work through the columns */
+      for (col = 0; col < cols; col++)
+        {
+          /* Finally, work through each of the samples */
+          for (sample = 0; sample < imageInfo->SamplesPerPixel; sample++)
+            {
+              /* Get the value */
+              TW_UINT16 value = *samplePtr;
+
+              /* Move the sample pointer */
+              samplePtr++;
+
+              /* Place in the destination */
+              *destByte = (char) ((float) value * (float) ratio);
+              destByte++;
+            }
+        }
     }
-  }
 
   /* Send the complete chunk */
-  gimp_pixel_rgn_set_rect(&(theClientData->pixel_rgn),
-                         (guchar *) destBuf,
-                         imageMemXfer->XOffset, imageMemXfer->YOffset,
-                         cols, rows);
+  gegl_buffer_set (theClientData->buffer,
+                   GEGL_RECTANGLE (imageMemXfer->XOffset, imageMemXfer->YOffset,
+                                   cols, rows), 0,
+                   theClientData->format, destBuf,
+                   GEGL_AUTO_ROWSTRIDE);
 
   /* Update the user on our progress */
   theClientData->completedPixels += (cols * rows);
-  gimp_progress_update((double) theClientData->completedPixels /
-                      (double) theClientData->totalPixels);
+  gimp_progress_update ((double) theClientData->completedPixels /
+                        (double) theClientData->totalPixels);
 
   return TRUE;
 }
@@ -793,15 +836,16 @@ twoBytesPerSampleTransferCallback(pTW_IMAGEINFO imageInfo,
  * the pixels.
  */
 static int
-palettedTransferCallback(pTW_IMAGEINFO imageInfo,
-                        pTW_IMAGEMEMXFER imageMemXfer,
-                        void *clientData)
+palettedTransferCallback (pTW_IMAGEINFO     imageInfo,
+                          pTW_IMAGEMEMXFER  imageMemXfer,
+                          void             *clientData)
 {
-  int channelsPerEntry;
-  int row, col;
-  int rows = imageMemXfer->Rows;
-  int cols = imageMemXfer->Columns;
-  char *destPtr = NULL, *srcPtr = NULL;
+  int   channelsPerEntry;
+  int   row, col;
+  int   rows = imageMemXfer->Rows;
+  int   cols = imageMemXfer->Columns;
+  char *destPtr = NULL;
+  char *srcPtr = NULL;
 
   /* Get the client data */
   pClientDataStruct theClientData = (pClientDataStruct) clientData;
@@ -811,48 +855,53 @@ palettedTransferCallback(pTW_IMAGEINFO imageInfo,
     (theClientData->paletteData->PaletteType == TWPA_RGB) ? 3 : 1;
 
   /* Allocate a buffer as necessary */
-  if (!destBuf)
+  if (! destBuf)
     destBuf = g_new (char, rows * cols * channelsPerEntry);
 
   /* Work through the rows */
   destPtr = destBuf;
-  for (row = 0; row < rows; row++) {
-    srcPtr = (char *) ((char *) imageMemXfer->Memory.TheMem +
-                      (row * imageMemXfer->BytesPerRow));
-
-    /* Work through the columns */
-    for (col = 0; col < cols; col++) {
-      /* Get the palette index */
-      int index = (unsigned char) *srcPtr;
-      srcPtr++;
-
-      switch (theClientData->paletteData->PaletteType) {
-      case TWPA_GRAY:
-       *destPtr = theClientData->paletteData->Colors[index].Channel1;
-       destPtr++;
-       break;
-
-      case TWPA_RGB:
-       *destPtr = theClientData->paletteData->Colors[index].Channel1;
-       destPtr++;
-       *destPtr = theClientData->paletteData->Colors[index].Channel2;
-       destPtr++;
-       *destPtr = theClientData->paletteData->Colors[index].Channel3;
-       destPtr++;
-      }
+  for (row = 0; row < rows; row++)
+    {
+      srcPtr = (char *) ((char *) imageMemXfer->Memory.TheMem +
+                         (row * imageMemXfer->BytesPerRow));
+
+      /* Work through the columns */
+      for (col = 0; col < cols; col++)
+        {
+          /* Get the palette index */
+          int index = (unsigned char) *srcPtr;
+
+          srcPtr++;
+
+          switch (theClientData->paletteData->PaletteType)
+            {
+            case TWPA_GRAY:
+              *destPtr = theClientData->paletteData->Colors[index].Channel1;
+              destPtr++;
+              break;
+
+            case TWPA_RGB:
+              *destPtr = theClientData->paletteData->Colors[index].Channel1;
+              destPtr++;
+              *destPtr = theClientData->paletteData->Colors[index].Channel2;
+              destPtr++;
+              *destPtr = theClientData->paletteData->Colors[index].Channel3;
+              destPtr++;
+            }
+        }
     }
-  }
 
   /* Send the complete chunk */
-  gimp_pixel_rgn_set_rect(&(theClientData->pixel_rgn),
-                         (guchar *) destBuf,
-                         imageMemXfer->XOffset, imageMemXfer->YOffset,
-                         cols, rows);
+  gegl_buffer_set (theClientData->buffer,
+                   GEGL_RECTANGLE (imageMemXfer->XOffset, imageMemXfer->YOffset,
+                                   cols, rows), 0,
+                   theClientData->format, destBuf,
+                   GEGL_AUTO_ROWSTRIDE);
 
   /* Update the user on our progress */
   theClientData->completedPixels += (cols * rows);
-  gimp_progress_update((double) theClientData->completedPixels /
-                      (double) theClientData->totalPixels);
+  gimp_progress_update ((double) theClientData->completedPixels /
+                        (double) theClientData->totalPixels);
 
   return TRUE;
 }
@@ -864,38 +913,42 @@ palettedTransferCallback(pTW_IMAGEINFO imageInfo,
  * block that is transferred from the data source.
  */
 int
-dataTransferCallback(pTW_IMAGEINFO imageInfo,
-                    pTW_IMAGEMEMXFER imageMemXfer,
-                    void *clientData)
+dataTransferCallback (pTW_IMAGEINFO     imageInfo,
+                      pTW_IMAGEMEMXFER  imageMemXfer,
+                      void             *clientData)
 {
 #ifdef _DEBUG
-  logData(imageInfo, imageMemXfer, clientData);
+  logData (imageInfo, imageMemXfer, clientData);
 #endif
 
   /* Choose the appropriate transfer handler */
-  switch (imageInfo->PixelType) {
-  case TWPT_PALETTE:
-    return palettedTransferCallback(imageInfo, imageMemXfer, clientData);
+  switch (imageInfo->PixelType)
+    {
+    case TWPT_PALETTE:
+      return palettedTransferCallback (imageInfo, imageMemXfer, clientData);
 
-  case TWPT_BW:
-    return bitTransferCallback(imageInfo, imageMemXfer, clientData);
+    case TWPT_BW:
+      return bitTransferCallback (imageInfo, imageMemXfer, clientData);
 
-  case TWPT_GRAY:
-  case TWPT_RGB:
-    switch (imageInfo->BitsPerPixel / imageInfo->SamplesPerPixel) {
-    case 8:
-      return oneBytePerSampleTransferCallback(imageInfo, imageMemXfer, clientData);
+    case TWPT_GRAY:
+    case TWPT_RGB:
+      switch (imageInfo->BitsPerPixel / imageInfo->SamplesPerPixel)
+        {
+        case 8:
+          return oneBytePerSampleTransferCallback (imageInfo, imageMemXfer,
+                                                   clientData);
 
-    case 16:
-      return twoBytesPerSampleTransferCallback(imageInfo, imageMemXfer, clientData);
+        case 16:
+          return twoBytesPerSampleTransferCallback (imageInfo, imageMemXfer,
+                                                    clientData);
+
+        default:
+          return FALSE;
+        }
 
     default:
       return FALSE;
     }
-
-  default:
-    return FALSE;
-  }
 }
 
 /*
@@ -915,36 +968,42 @@ dataTransferCallback(pTW_IMAGEINFO imageInfo,
  *  The transfer failed.
  */
 int
-endTransferCallback(int completionState, int pendingCount, void *clientData)
+endTransferCallback (int   completionState,
+                     int   pendingCount,
+                     void *clientData)
 {
   pClientDataStruct theClientData = (pClientDataStruct) clientData;
 
-  LogMessage("endTransferCallback: CompState = %d, pending = %d\n",
-            completionState, pendingCount);
+  LogMessage ("endTransferCallback: CompState = %d, pending = %d\n",
+              completionState, pendingCount);
 
   /* Clean up and detach from the drawable */
-  if (destBuf) {
-    g_free (destBuf);
-    destBuf = NULL;
-  }
-  gimp_drawable_flush(theClientData->drawable);
-  gimp_drawable_detach(theClientData->drawable);
+  if (destBuf)
+    {
+      g_free (destBuf);
+      destBuf = NULL;
+    }
+
+  g_object_unref (theClientData->buffer);
 
   /* Make sure to check our return code */
-  if (completionState == TWRC_XFERDONE) {
-    /* We have a completed image transfer */
-    values[2].type = GIMP_PDB_INT32ARRAY;
-    values[2].data.d_int32array[values[1].data.d_int32++] =
-      theClientData->image_id;
-
-    /* Display the image */
-    LogMessage("Displaying image %d\n", theClientData->image_id);
-    gimp_display_new (theClientData->image_id);
-  } else {
-    /* The transfer did not complete successfully */
-    LogMessage("Deleting image\n");
-    gimp_image_delete(theClientData->image_id);
-  }
+  if (completionState == TWRC_XFERDONE)
+    {
+      /* We have a completed image transfer */
+      values[2].type = GIMP_PDB_INT32ARRAY;
+      values[2].data.d_int32array[values[1].data.d_int32++] =
+        theClientData->image_id;
+
+      /* Display the image */
+      LogMessage ("Displaying image %d\n", theClientData->image_id);
+      gimp_display_new (theClientData->image_id);
+    }
+  else
+    {
+      /* The transfer did not complete successfully */
+      LogMessage ("Deleting image\n");
+      gimp_image_delete (theClientData->image_id);
+    }
 
   /* Shut down if we have received all of the possible images */
   return (values[1].data.d_int32 < MAX_IMAGES);
@@ -958,7 +1017,8 @@ endTransferCallback(int completionState, int pendingCount, void *clientData)
  * transferred.
  */
 void
-postTransferCallback(int pendingCount, void *clientData)
+postTransferCallback (int   pendingCount,
+                      void *clientData)
 {
   /* Shut things down. */
   if (pendingCount != 0)
@@ -969,9 +1029,9 @@ postTransferCallback(int pendingCount, void *clientData)
    * down and the run() procedure will finally be
    * able to finish.
    */
-  disableDS(twSession);
-  closeDS(twSession);
-  closeDSM(twSession);
+  disableDS (twSession);
+  closeDS (twSession);
+  closeDSM (twSession);
 
   /* Post a message to close up the application */
   twainQuitApplication ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]