tw_dump.c 7.17 KB
Newer Older
1
/*
2 3
 * TWAIN Plug-in
 * Copyright (C) 1999 Craig Setera
4
 * Craig Setera <setera@home.com>
5 6
 * 03/31/1999
 *
7 8 9 10
 * Updated for Mac OS X support
 * Brion Vibber <brion@pobox.com>
 * 07/22/2004
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 3 of the License, or
14 15 16 17 18 19 20 21
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 24 25 26 27 28 29 30
 *
 *
 * Based on (at least) the following plug-ins:
 * Screenshot
 * GIF
 * Randomize
 *
 * Any suggestions, bug-reports or patches are welcome.
31
 *
32 33 34 35 36 37 38 39 40 41
 * This plug-in interfaces to the TWAIN support library in order
 * to capture images from TWAIN devices directly into GIMP images.
 * The plug-in is capable of acquiring the following type of
 * images:
 * - B/W (1 bit images translated to grayscale B/W)
 * - Grayscale up to 16 bits per pixel
 * - RGB up to 16 bits per sample (24, 30, 36, etc.)
 * - Paletted images (both Gray and RGB)
 *
 * Prerequisites:
42 43
 * Should compile and run on both Win32 and Mac OS X 10.3 (possibly
 * also on 10.2).
44 45 46 47
 *
 * Known problems:
 * - Multiple image transfers will hang the plug-in.  The current
 *   configuration compiles with a maximum of single image transfers.
48 49
 * - On Mac OS X, canceling doesn't always close things out fully.
 * - Epson TWAIN driver on Mac OS X crashes the plugin when scanning.
50 51
 */

52
/*
53 54 55 56
 * Revision history
 *  (02/07/99)  v0.1   First working version (internal)
 *  (02/09/99)  v0.2   First release to anyone other than myself
 *  (02/15/99)  v0.3   Added image dump and read support for debugging
57
 *  (03/31/99)  v0.5   Added support for multi-byte samples and paletted
58
 *                     images.
59
 *  (07/23/04)  v0.6   Added Mac OS X support.
60
 */
61 62 63

#include "config.h"

64
#include <string.h>
65 66 67

#include <glib/gstdio.h>

68
#include "libgimp/gimp.h"
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#include "tw_dump.h"
#include "tw_func.h"
#include "tw_util.h"

/* File variables */
static FILE *outputFile = NULL;
extern pTW_SESSION twSession;

/*
 * dumpPreTransferCallback
 *
 * This callback function is called before any images
 * are transferred.  Set up the one time only stuff.
 */
void
dumpPreTransferCallback(void *clientData)
{
  /* Open our output file... Not settable... Always
   * write to the root directory.  Simplistic, but
   * gets the job done.
   */
91
  outputFile = g_fopen(DUMP_FILE, "wb");
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}

/*
 * dumpBeginTransferCallback
 *
 * The following function is called at the beginning
 * of each image transfer.
 */
int
dumpBeginTransferCallback(pTW_IMAGEINFO imageInfo, void *clientData)
{
  logBegin(imageInfo, clientData);

  /* Dump the image information */
  fwrite((void *) imageInfo, sizeof(TW_IMAGEINFO), 1, outputFile);

  /* Keep going */
  return TRUE;
}
111

112 113 114 115 116 117
/*
 * dumpDataTransferCallback
 *
 * The following function is called for each memory
 * block that is transferred from the data source.
 */
118
int
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
dumpDataTransferCallback(pTW_IMAGEINFO imageInfo,
			 pTW_IMAGEMEMXFER imageMemXfer,
			 void *clientData)
{
  int flag = 1;

  logData(imageInfo, imageMemXfer, clientData);

  /* Write a flag that says that this is a data packet */
  fwrite((void *) &flag, sizeof(int), 1, outputFile);

  /* Dump the memory information */
  fwrite((void *) imageMemXfer, sizeof(TW_IMAGEMEMXFER), 1, outputFile);
  fwrite((void *) imageMemXfer->Memory.TheMem,
	 1, imageMemXfer->BytesWritten, outputFile);

  /* Keep going */
  return TRUE;
}
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/*
 * dumpEndTransferCallback
 *
 * The following function is called at the end of the
 * image transfer.  The caller will be handed
 * the image transfer completion state.  The
 * following values (defined in twain.h) are
 * possible:
 *
 * TWRC_XFERDONE
 *  The transfer completed successfully
 * TWRC_CANCEL
 *  The transfer was completed by the user
 * TWRC_FAILURE
 *  The transfer failed.
 */
int
dumpEndTransferCallback(int completionState, int pendingCount, void *clientData)
{
  int flag = 0;

  /* Write a flag that says that this is a data packet */
  fwrite((void *) &flag, sizeof(int), 1, outputFile);

  /* Write the necessary data */
  fwrite(&completionState, sizeof(int), 1, outputFile);
  fwrite(&pendingCount, sizeof(int), 1, outputFile);

  /* Only ever transfer a single image */
  return FALSE;
}

/*
 * dumpPostTransferCallback
 *
 * This callback function will be called
 * after all possible images have been
 * transferred.
 */
void
dumpPostTransferCallback(int pendingCount, void *clientData)
{
  char buffer[128];

  /* Shut things down. */
  if (pendingCount != 0)
    cancelPendingTransfers(twSession);

  /* This will close the datasource and datasource
   * manager.  Then the message queue will be shut
   * down and the run() procedure will finally be
   * able to finish.
   */
  disableDS(twSession);
  closeDS(twSession);
  closeDSM(twSession);
195

196 197 198 199 200 201 202 203
  /* Close the dump file */
  fclose(outputFile);

  /* Tell the user */
  sprintf(buffer, "Image dumped to file %s\n", DUMP_FILE);
  gimp_message(buffer);

  /* Post a message to close up the application */
204
  twainQuitApplication ();
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
}

/*
 * readDumpedImage
 *
 * Get a previously dumped image.
 */
void readDumpedImage(pTW_SESSION twSession)
{
  int moreData;
  int completionState;
  int pendingCount;

  TW_IMAGEINFO imageInfo;
  TW_IMAGEMEMXFER imageMemXfer;

  /* Open our output file... Not settable... Always
   * write to the root directory.  Simplistic, but
   * gets the job done.
   */
225
  FILE *inputFile = g_fopen(DUMP_FILE, "rb");
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

  /*
   * Inform our application that we are getting ready
   * to transfer images.
   */
  (*twSession->transferFunctions->preTxfrCb)(NULL);

  /* Read the image information */
  fread((void *) &imageInfo, sizeof(TW_IMAGEINFO), 1, inputFile);

  /* Call the begin transfer callback */
  if (!(*twSession->transferFunctions->txfrBeginCb)(&imageInfo, twSession->clientData))
    return;

  /* Read all of the data packets */
  fread((void *) &moreData, sizeof(int), 1, inputFile);
  while (moreData) {
    /* Read the memory information */
    fread((void *) &imageMemXfer, sizeof(TW_IMAGEMEMXFER), 1, inputFile);
245
    imageMemXfer.Memory.TheMem = (TW_MEMREF) g_malloc (imageMemXfer.BytesWritten);
246 247 248 249
    fread((void *) imageMemXfer.Memory.TheMem,
	  1, imageMemXfer.BytesWritten, inputFile);

    /* Call the data transfer callback function */
250 251 252
    if (!(*twSession->transferFunctions->txfrDataCb)
	(&imageInfo,
	 &imageMemXfer,
253 254 255 256
	 twSession->clientData))
      return;

    /* Clean up the memory */
257
    g_free (imageMemXfer.Memory.TheMem);
258 259 260 261 262 263 264 265 266 267 268 269 270 271

    /* Check for continuation */
    fread((void *) &moreData, sizeof(int), 1, inputFile);
  }

  /* Grab the final information */
  fread(&completionState, sizeof(int), 1, inputFile);
  fread(&pendingCount, sizeof(int), 1, inputFile);

  if (twSession->transferFunctions->txfrEndCb)
    (*twSession->transferFunctions->txfrEndCb)(completionState, 0,
					       twSession->clientData);

  /* Post a message to close up the application */
272
  twainQuitApplication ();
273
}