update for HEAD-2003091401
[reactos.git] / lib / twain / ds_image.c
1 /*
2  * Copyright 2000 Corel Corporation
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 //#include "config.h"
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "twain.h"
28 #include "twain_i.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(twain);
32
33 /* DG_IMAGE/DAT_CIECOLOR/MSG_GET */
34 TW_UINT16 TWAIN_CIEColorGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
35                              TW_MEMREF pData)
36 {
37     FIXME ("stub!\n");
38
39     return TWRC_FAILURE;
40 }
41
42 /* DG_IMAGE/DAT_EXTIMAGEINFO/MSG_GET */
43 TW_UINT16 TWAIN_ExtImageInfoGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
44                                  TW_MEMREF pData)
45 {
46     FIXME ("stub!\n");
47
48     return TWRC_FAILURE;
49 }
50
51 /* DG_IMAGE/DAT_GRAYRESPONSE/MSG_RESET */
52 TW_UINT16 TWAIN_GrayResponseReset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
53                                    TW_MEMREF pData)
54 {
55     FIXME ("stub!\n");
56
57     return TWRC_FAILURE;
58 }
59
60 /* DG_IMAGE/DAT_GRAYRESPONSE/MSG_SET */
61 TW_UINT16 TWAIN_GrayResponseSet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
62                                  TW_MEMREF pData)
63 {
64     FIXME ("stub!\n");
65
66     return TWRC_FAILURE;
67 }
68
69 /* DG_IMAGE/DAT_IMAGEFILEXFER/MSG_GET */
70 TW_UINT16 TWAIN_ImageFileXferGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
71                                   TW_MEMREF pData)
72 {
73     FIXME ("stub!\n");
74
75     return TWRC_FAILURE;
76 }
77
78 /* DG_IMAGE/DAT_IMAGEINFO/MSG_GET */
79 TW_UINT16 TWAIN_ImageInfoGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
80                               TW_MEMREF pData)
81 {
82 #ifndef HAVE_SANE
83     return TWRC_FAILURE;
84 #else
85     TW_UINT16 twRC = TWRC_SUCCESS;
86     pTW_IMAGEINFO pImageInfo = (pTW_IMAGEINFO) pData;
87     activeDS *pSource = TWAIN_LookupSource (pDest);
88     SANE_Status status;
89
90     TRACE("DG_IMAGE/DAT_IMAGEINFO/MSG_GET\n");
91
92     if (!pSource)
93     {
94         twRC = TWRC_FAILURE;
95         DSM_twCC = TWCC_BADDEST;
96     }
97     else if (pSource->currentState != 6 && pSource->currentState != 7)
98     {
99         twRC = TWRC_FAILURE;
100         pSource->twCC = TWCC_SEQERROR;
101     }
102     else
103     {
104         if (pSource->currentState == 6)
105         {
106             /* return general image description information about the image about to be transfer */
107             status = sane_get_parameters (pSource->deviceHandle, &pSource->sane_param);
108         }
109
110         pImageInfo->XResolution.Whole = -1;
111         pImageInfo->XResolution.Frac = 0;
112         pImageInfo->YResolution.Whole = -1;
113         pImageInfo->YResolution.Frac = 0;
114         pImageInfo->ImageWidth = pSource->sane_param.pixels_per_line;
115         pImageInfo->ImageLength = pSource->sane_param.lines;
116         if (pSource->sane_param.depth == 24)
117         {
118             pImageInfo->SamplesPerPixel = 3;
119             pImageInfo->BitsPerSample[0] = 8;
120             pImageInfo->BitsPerSample[1] = 8;
121             pImageInfo->BitsPerSample[2] = 8;
122             pImageInfo->BitsPerPixel = 24;
123             pImageInfo->Planar = TRUE;
124             pImageInfo->PixelType = TWPT_RGB;
125             pImageInfo->Compression = TWCP_NONE;
126         }
127         else if (pSource->sane_param.depth == 8)
128         {
129             /* FIXME: fill the image info structure for 8-bit image */
130         }
131     }
132
133     return twRC;
134 #endif
135 }
136
137 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GET */
138 TW_UINT16 TWAIN_ImageLayoutGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
139                                 TW_MEMREF pData)
140 {
141     FIXME ("stub!\n");
142
143     return TWRC_FAILURE;
144 }
145
146 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GETDEFAULT */
147 TW_UINT16 TWAIN_ImageLayoutGetDefault (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
148                                        TW_MEMREF pData)
149 {
150     FIXME ("stub!\n");
151
152     return TWRC_FAILURE;
153 }
154
155 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_RESET */
156 TW_UINT16 TWAIN_ImageLayoutReset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
157                                   TW_MEMREF pData)
158 {
159     FIXME ("stub!\n");
160
161     return TWRC_FAILURE;
162 }
163
164 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_SET */
165 TW_UINT16 TWAIN_ImageLayoutSet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
166                                 TW_MEMREF pData)
167 {
168     FIXME ("stub!\n");
169
170     return TWRC_FAILURE;
171 }
172
173 /* DG_IMAGE/DAT_IMAGEMEMXFER/MSG_GET */
174 TW_UINT16 TWAIN_ImageMemXferGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
175                                  TW_MEMREF pData)
176 {
177     FIXME ("stub!\n");
178
179     return TWRC_FAILURE;
180 }
181
182 /* DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET */
183 TW_UINT16 TWAIN_ImageNativeXferGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
184                                     TW_MEMREF pData)
185 {
186 #ifndef HAVE_SANE
187     return TWRC_FAILURE;
188 #else
189     TW_UINT16 twRC = TWRC_SUCCESS;
190     pTW_UINT32 pHandle = (pTW_UINT32) pData;
191     activeDS *pSource = TWAIN_LookupSource (pDest);
192     SANE_Status status;
193     SANE_Byte buffer[32*1024];
194     int buff_len;
195     HBITMAP hDIB;
196     BITMAPINFO bmpInfo;
197     VOID *pBits;
198     HDC dc;
199
200     TRACE("DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET\n");
201
202     if (!pSource)
203     {
204         twRC = TWRC_FAILURE;
205         DSM_twCC = TWCC_NODS;
206     }
207     else if (pSource->currentState != 6)
208     {
209         twRC = TWRC_FAILURE;
210         pSource->twCC = TWCC_SEQERROR;
211     }
212     else
213     {
214         /* Transfer an image from the source to the application */
215         status = sane_start (pSource->deviceHandle);
216         if (status != SANE_STATUS_GOOD)
217         {
218             WARN("sane_start: %s\n", sane_strstatus (status));
219             sane_cancel (pSource->deviceHandle);
220             pSource->twCC = TWCC_OPERATIONERROR;
221             return TWRC_FAILURE;
222         }
223
224         status = sane_get_parameters (pSource->deviceHandle, &pSource->sane_param);
225         if (status != SANE_STATUS_GOOD)
226         {
227             WARN("sane_get_parameters: %s\n", sane_strstatus (status));
228             sane_cancel (pSource->deviceHandle);
229             pSource->twCC = TWCC_OPERATIONERROR;
230             return TWRC_FAILURE;
231         }
232
233         TRACE("Acquiring image %dx%dx%d bits (format=%d last=%d) from sane...\n"
234               , pSource->sane_param.pixels_per_line, pSource->sane_param.lines,
235               pSource->sane_param.depth, pSource->sane_param.format,
236               pSource->sane_param.last_frame);
237
238         ZeroMemory (&bmpInfo, sizeof (BITMAPINFO));
239         bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
240         bmpInfo.bmiHeader.biWidth = pSource->sane_param.pixels_per_line;
241         bmpInfo.bmiHeader.biHeight = pSource->sane_param.lines;
242         bmpInfo.bmiHeader.biPlanes = 1;
243         bmpInfo.bmiHeader.biBitCount = pSource->sane_param.depth;
244         bmpInfo.bmiHeader.biCompression = BI_RGB;
245         bmpInfo.bmiHeader.biSizeImage = 0;
246         bmpInfo.bmiHeader.biXPelsPerMeter = 0;
247         bmpInfo.bmiHeader.biYPelsPerMeter = 0;
248         bmpInfo.bmiHeader.biClrUsed = 1;
249         bmpInfo.bmiHeader.biClrImportant = 0;
250         bmpInfo.bmiColors[0].rgbBlue = 128;
251         bmpInfo.bmiColors[0].rgbGreen = 128;
252         bmpInfo.bmiColors[0].rgbRed = 128;
253         hDIB = CreateDIBSection ((dc = GetDC(pSource->hwndOwner)), &bmpInfo,
254                                  DIB_RGB_COLORS, &pBits, 0, 0);
255         if (!hDIB)
256         {
257             sane_cancel (pSource->deviceHandle);
258             pSource->twCC = TWCC_LOWMEMORY;
259             return TWRC_FAILURE;
260         }
261
262         do
263         {
264             status = sane_read (pSource->deviceHandle, buffer,
265                                 sizeof (buffer),  &buff_len);
266             if (status == SANE_STATUS_GOOD)
267             {
268                 /* FIXME: put code for coverting the image data into DIB here */
269
270             }
271             else if (status != SANE_STATUS_EOF)
272             {
273                 WARN("sane_read: %s\n", sane_strstatus (status));
274                 sane_cancel (pSource->deviceHandle);
275                 pSource->twCC = TWCC_OPERATIONERROR;
276                 return TWRC_FAILURE;
277             }
278         } while (status == SANE_STATUS_GOOD);
279
280         sane_cancel (pSource->deviceHandle);
281         ReleaseDC (pSource->hwndOwner, dc);
282         *pHandle = (TW_UINT32)hDIB;
283         twRC = TWRC_XFERDONE;
284         pSource->twCC = TWCC_SUCCESS;
285         pSource->currentState = 7;
286     }
287     return twRC;
288 #endif
289 }
290
291 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GET */
292 TW_UINT16 TWAIN_JPEGCompressionGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
293                                     TW_MEMREF pData)
294 {
295     FIXME ("stub!\n");
296
297     return TWRC_FAILURE;
298 }
299
300 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GETDEFAULT */
301 TW_UINT16 TWAIN_JPEGCompressionGetDefault (pTW_IDENTITY pOrigin,
302                                            pTW_IDENTITY pDest,
303                                            TW_MEMREF pData)
304 {
305     FIXME ("stub!\n");
306
307     return TWRC_FAILURE;
308 }
309
310 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_RESET */
311 TW_UINT16 TWAIN_JPEGCompressionReset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
312                                       TW_MEMREF pData)
313 {
314     FIXME ("stub!\n");
315
316     return TWRC_FAILURE;
317 }
318
319 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_SET */
320 TW_UINT16 TWAIN_JPEGCompressionSet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
321                                     TW_MEMREF pData)
322 {
323     FIXME ("stub!\n");
324
325     return TWRC_FAILURE;
326 }
327
328 /* DG_IMAGE/DAT_PALETTE8/MSG_GET */
329 TW_UINT16 TWAIN_Palette8Get (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
330                              TW_MEMREF pData)
331 {
332     FIXME ("stub!\n");
333
334     return TWRC_FAILURE;
335 }
336
337 /* DG_IMAGE/DAT_PALETTE8/MSG_GETDEFAULT */
338 TW_UINT16 TWAIN_Palette8GetDefault (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
339                                     TW_MEMREF pData)
340 {
341     FIXME ("stub!\n");
342
343     return TWRC_FAILURE;
344 }
345
346 /* DG_IMAGE/DAT_PALETTE8/MSG_RESET */
347 TW_UINT16 TWAIN_Palette8Reset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
348                                TW_MEMREF pData)
349 {
350     FIXME ("stub!\n");
351
352     return TWRC_FAILURE;
353 }
354
355 /* DG_IMAGE/DAT_PALETTE8/MSG_SET */
356 TW_UINT16 TWAIN_Palette8Set (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
357                              TW_MEMREF pData)
358 {
359     FIXME ("stub!\n");
360
361     return TWRC_FAILURE;
362 }
363
364 /* DG_IMAGE/DAT_RGBRESPONSE/MSG_RESET */
365 TW_UINT16 TWAIN_RGBResponseReset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
366                                   TW_MEMREF pData)
367 {
368     FIXME ("stub!\n");
369
370     return TWRC_FAILURE;
371 }
372
373 /* DG_IMAGE/DAT_RGBRESPONSE/MSG_SET */
374 TW_UINT16 TWAIN_RGBResponseSet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
375                                 TW_MEMREF pData)
376 {
377     FIXME ("stub!\n");
378
379     return TWRC_FAILURE;
380 }