tw_util.c 4.64 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 <https://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 65
#include <glib.h>		/* Needed when compiling with gcc */

66
#include <glib/gstdio.h>
67

68 69 70 71
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
72

73 74 75 76 77 78 79
#include "tw_util.h"
#ifdef _DEBUG
#include "tw_func.h"
FILE *logFile = NULL;
#endif

#ifdef _DEBUG
80

81 82 83 84 85 86 87 88 89 90 91 92
/*
 * LogMessage
 */
void
LogMessage(char *format, ...)
{
  va_list args;
  time_t time_of_day;
  char *ctime_string;

  /* Open the log file as necessary */
  if (!logFile)
93
    logFile = g_fopen(DEBUG_LOGFILE, "w");
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

  time_of_day = time(NULL);
  ctime_string = ctime(&time_of_day);
  ctime_string[19] = '\0';

  fprintf(logFile, "[%s] ", (ctime_string + 11));
  va_start(args, format);
  vfprintf(logFile, format, args);
  fflush(logFile);
  va_end(args);
}

void
logBegin(pTW_IMAGEINFO imageInfo, void *clientData)
{
  int i;
  char buffer[256];
111

112 113 114 115 116
  LogMessage("\n");
  LogMessage("*************************************\n");
  LogMessage("\n");
  LogMessage("Image transfer begin:\n");
  /*	LogMessage("\tClient data: %s\n", (char *) clientData); */
117

118 119 120 121 122 123 124 125 126 127 128 129 130
  /* Log the image information */
  LogMessage("Image information:\n");
  LogMessage("\tXResolution: %f\n", FIX32ToFloat(imageInfo->XResolution));
  LogMessage("\tYResolution: %f\n", FIX32ToFloat(imageInfo->YResolution));
  LogMessage("\tImageWidth: %d\n", imageInfo->ImageWidth);
  LogMessage("\tImageLength: %d\n", imageInfo->ImageLength);
  LogMessage("\tSamplesPerPixel: %d\n", imageInfo->SamplesPerPixel);
  sprintf(buffer, "\tBitsPerSample: {");
  for (i = 0; i < 8; i++) {
    if (imageInfo->BitsPerSample[i])
      strcat(buffer, "1");
    else
      strcat(buffer, "0");
131

132 133 134 135
    if (i != 7)
      strcat(buffer, ",");
  }
  LogMessage("%s}\n", buffer);
136

137 138 139 140
  LogMessage("\tBitsPerPixel: %d\n", imageInfo->BitsPerPixel);
  LogMessage("\tPlanar: %s\n", (imageInfo->Planar ? "TRUE" : "FALSE"));
  LogMessage("\tPixelType: %d\n", imageInfo->PixelType);
  /* Compression */
141

142
}
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
void
logData(pTW_IMAGEINFO imageInfo,
	pTW_IMAGEMEMXFER imageMemXfer,
	void *clientData)
{
  LogMessage("Image transfer callback called:\n");
  LogMessage("\tClient data: %s\n", (char *) clientData);
  LogMessage("Memory block transferred:\n");
  LogMessage("\tBytesPerRow = %d\n", imageMemXfer->BytesPerRow);
  LogMessage("\tColumns = %d\n", imageMemXfer->Columns);
  LogMessage("\tRows = %d\n", imageMemXfer->Rows);
  LogMessage("\tXOffset = %d\n", imageMemXfer->XOffset);
  LogMessage("\tYOffset = %d\n", imageMemXfer->YOffset);
  LogMessage("\tBytesWritten = %d\n", imageMemXfer->BytesWritten);
}
159

160 161 162 163 164 165 166 167 168 169 170
#else

/*
 * LogMessage
 */
void
LogMessage(char *format, ...)
{
}

#endif /* DEBUG */