home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / twain / tw_util.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  5.5 KB  |  205 lines

  1. /*  
  2.  * TWAIN Plug-in
  3.  * Copyright (C) 1999 Craig Setera
  4.  * Craig Setera <setera@home.com>
  5.  * 03/31/1999
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20.  *
  21.  *
  22.  * Based on (at least) the following plug-ins:
  23.  * Screenshot
  24.  * GIF
  25.  * Randomize
  26.  *
  27.  * Any suggestions, bug-reports or patches are welcome.
  28.  * 
  29.  * This plug-in interfaces to the TWAIN support library in order
  30.  * to capture images from TWAIN devices directly into GIMP images.
  31.  * The plug-in is capable of acquiring the following type of
  32.  * images:
  33.  * - B/W (1 bit images translated to grayscale B/W)
  34.  * - Grayscale up to 16 bits per pixel
  35.  * - RGB up to 16 bits per sample (24, 30, 36, etc.)
  36.  * - Paletted images (both Gray and RGB)
  37.  *
  38.  * Prerequisites:
  39.  *  This plug-in will not compile on anything other than a Win32
  40.  *  platform.  Although the TWAIN documentation implies that there
  41.  *  is TWAIN support available on Macintosh, I neither have a 
  42.  *  Macintosh nor the interest in porting this.  If anyone else
  43.  *  has an interest, consult www.twain.org for more information on
  44.  *  interfacing to TWAIN.
  45.  *
  46.  * Known problems:
  47.  * - Multiple image transfers will hang the plug-in.  The current
  48.  *   configuration compiles with a maximum of single image transfers.
  49.  */
  50.  
  51. /* 
  52.  * Revision history
  53.  *  (02/07/99)  v0.1   First working version (internal)
  54.  *  (02/09/99)  v0.2   First release to anyone other than myself
  55.  *  (02/15/99)  v0.3   Added image dump and read support for debugging
  56.  *  (03/31/99)  v0.5   Added support for multi-byte samples and paletted 
  57.  *                     images.
  58.  */
  59. #include <glib.h>        /* Needed when compiling with gcc */
  60.  
  61. #include <stdio.h>
  62. #include <windows.h>
  63. #include <stdarg.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. #include <time.h>
  67. #include "tw_util.h"
  68. #ifdef _DEBUG
  69. #include "tw_func.h"
  70. FILE *logFile = NULL;
  71. #endif
  72.  
  73. #ifdef _DEBUG
  74. /*
  75.  * LogMessage
  76.  */
  77. void
  78. LogMessage(char *format, ...)
  79. {
  80.   va_list args;
  81.   time_t time_of_day;
  82.   char *ctime_string;
  83.  
  84.   /* Open the log file as necessary */
  85.   if (!logFile)
  86.     logFile = fopen("c:\\twain.log", "w");
  87.  
  88.   time_of_day = time(NULL);
  89.   ctime_string = ctime(&time_of_day);
  90.   ctime_string[19] = '\0';
  91.  
  92.   fprintf(logFile, "[%s] ", (ctime_string + 11));
  93.   va_start(args, format);
  94.   vfprintf(logFile, format, args);
  95.   fflush(logFile);
  96.   va_end(args);
  97. }
  98.  
  99. /*
  100.  * LogLastWinError
  101.  *
  102.  * Log the last Windows error as returned by
  103.  * GetLastError.
  104.  */
  105. void
  106. LogLastWinError(void) 
  107. {
  108.     LPVOID lpMsgBuf;
  109.     
  110.     FormatMessage( 
  111.         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
  112.         FORMAT_MESSAGE_FROM_SYSTEM | 
  113.         FORMAT_MESSAGE_IGNORE_INSERTS,
  114.         NULL,
  115.         GetLastError(),
  116.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  117.         (LPTSTR) &lpMsgBuf,
  118.         0,
  119.         NULL 
  120.         );
  121.     
  122.     LogMessage("%s\n", lpMsgBuf);
  123.     
  124.     /* Free the buffer. */
  125.     LocalFree( lpMsgBuf );
  126.     
  127. }
  128.  
  129. void
  130. logBegin(pTW_IMAGEINFO imageInfo, void *clientData)
  131. {
  132.   int i;
  133.   char buffer[256];
  134.         
  135.   LogMessage("\n");
  136.   LogMessage("*************************************\n");
  137.   LogMessage("\n");
  138.   LogMessage("Image transfer begin:\n");
  139.   /*    LogMessage("\tClient data: %s\n", (char *) clientData); */
  140.         
  141.   /* Log the image information */
  142.   LogMessage("Image information:\n");
  143.   LogMessage("\tXResolution: %f\n", FIX32ToFloat(imageInfo->XResolution));
  144.   LogMessage("\tYResolution: %f\n", FIX32ToFloat(imageInfo->YResolution));
  145.   LogMessage("\tImageWidth: %d\n", imageInfo->ImageWidth);
  146.   LogMessage("\tImageLength: %d\n", imageInfo->ImageLength);
  147.   LogMessage("\tSamplesPerPixel: %d\n", imageInfo->SamplesPerPixel);
  148.   sprintf(buffer, "\tBitsPerSample: {");
  149.   for (i = 0; i < 8; i++) {
  150.     if (imageInfo->BitsPerSample[i])
  151.       strcat(buffer, "1");
  152.     else
  153.       strcat(buffer, "0");
  154.             
  155.     if (i != 7)
  156.       strcat(buffer, ",");
  157.   }
  158.   LogMessage("%s}\n", buffer);
  159.         
  160.   LogMessage("\tBitsPerPixel: %d\n", imageInfo->BitsPerPixel);
  161.   LogMessage("\tPlanar: %s\n", (imageInfo->Planar ? "TRUE" : "FALSE"));
  162.   LogMessage("\tPixelType: %d\n", imageInfo->PixelType);
  163.   /* Compression */
  164.         
  165. }
  166.     
  167. void
  168. logData(pTW_IMAGEINFO imageInfo,
  169.     pTW_IMAGEMEMXFER imageMemXfer,
  170.     void *clientData)
  171. {
  172.   LogMessage("Image transfer callback called:\n");
  173.   LogMessage("\tClient data: %s\n", (char *) clientData);
  174.   LogMessage("Memory block transferred:\n");
  175.   LogMessage("\tBytesPerRow = %d\n", imageMemXfer->BytesPerRow);
  176.   LogMessage("\tColumns = %d\n", imageMemXfer->Columns);
  177.   LogMessage("\tRows = %d\n", imageMemXfer->Rows);
  178.   LogMessage("\tXOffset = %d\n", imageMemXfer->XOffset);
  179.   LogMessage("\tYOffset = %d\n", imageMemXfer->YOffset);
  180.   LogMessage("\tBytesWritten = %d\n", imageMemXfer->BytesWritten);
  181. }
  182.     
  183. #else
  184.  
  185. /*
  186.  * LogMessage
  187.  */
  188. void
  189. LogMessage(char *format, ...)
  190. {
  191. }
  192.  
  193. /*
  194.  * LogLastWinError
  195.  *
  196.  * Log the last Windows error as returned by
  197.  * GetLastError.
  198.  */
  199. void
  200. LogLastWinError(void) 
  201. {
  202. }
  203.  
  204. #endif /* DEBUG */
  205.