summaryrefslogtreecommitdiff
blob: 72643a0b6c61ced819a1e7b351cd4b33e88ee1bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*====================================================================*
 -  Copyright (C) 2001 Leptonica.  All rights reserved.
 -
 -  Redistribution and use in source and binary forms, with or without
 -  modification, are permitted provided that the following conditions
 -  are met:
 -  1. Redistributions of source code must retain the above copyright
 -     notice, this list of conditions and the following disclaimer.
 -  2. Redistributions in binary form must reproduce the above
 -     copyright notice, this list of conditions and the following
 -     disclaimer in the documentation and/or other materials
 -     provided with the distribution.
 -
 -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
 -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *====================================================================*/

/*!
 * \file leptwin.c
 * <pre>
 *
 *    This file contains Leptonica routines needed only on Microsoft Windows
 *
 *    Currently it only contains one public function
 *    (based on dibsectn.c by jmh, 03-30-98):
 *
 *      HBITMAP    pixGetWindowsHBITMAP(PIX *pix)
 * </pre>
 */

#ifdef _WIN32
#include <stdlib.h>
#include <string.h>
#include "allheaders.h"
#include "leptwin.h"

/* Macro to determine the number of bytes per line in the DIB bits.
 * This accounts for DWORD alignment by adding 31 bits,
 * then dividing by 32, then rounding up to the next highest
 * count of 4-bytes.  Then, we multiply by 4 to get the total byte count.  */
#define BYTESPERLINE(Width, BPP) ((l_int32)((((DWORD)(Width) * (DWORD)(BPP) + 31) >> 5)) << 2)


/* **********************************************************************
  DWORD DSImageBitsSize(LPBITMAPINFO pbmi)

  PARAMETERS:
    LPBITMAPINFO - pointer to a BITMAPINFO describing a DIB

  RETURNS:
    DWORD    - the size, in bytes, of the DIB's image bits

  REMARKS:
    Calculates and returns the size, in bytes, of the image bits for
    the DIB described by the BITMAPINFO.
********************************************************************** */
static DWORD
DSImageBitsSize(LPBITMAPINFO pbmi)
{
    switch(pbmi->bmiHeader.biCompression)
    {
    case BI_RLE8:  /* wrong if haven't called DSCreateDIBSection or
                    * CreateDIBSection with this pbmi */
    case BI_RLE4:
        return pbmi->bmiHeader.biSizeImage;
        break;
    default:  /* should not have to use "default" */
    case BI_RGB:
    case BI_BITFIELDS:
        return BYTESPERLINE(pbmi->bmiHeader.biWidth, \
                   pbmi->bmiHeader.biBitCount * pbmi->bmiHeader.biPlanes) *
               pbmi->bmiHeader.biHeight;
        break;
    }
    return 0;
}

/* **********************************************************************
  DWORD ImageBitsSize(HBITMAP hbitmap)

  PARAMETERS:
    HBITMAP - hbitmap

  RETURNS:
    DWORD    - the size, in bytes, of the HBITMAP's image bits

  REMARKS:
    Calculates and returns the size, in bytes, of the image bits for
    the DIB described by the HBITMAP.
********************************************************************** */
static DWORD
ImageBitsSize(HBITMAP hBitmap)
{
    DIBSECTION  ds;

    GetObject(hBitmap, sizeof(DIBSECTION), &ds);
    switch( ds.dsBmih.biCompression )
    {
    case BI_RLE8:  /* wrong if haven't called DSCreateDIBSection or
                    * CreateDIBSection with this pbmi */
    case BI_RLE4:
        return ds.dsBmih.biSizeImage;
        break;
    default:  /* should not have to use "default" */
    case BI_RGB:
    case BI_BITFIELDS:
        return BYTESPERLINE(ds.dsBmih.biWidth, \
                            ds.dsBmih.biBitCount * ds.dsBmih.biPlanes) *
                            ds.dsBmih.biHeight;
        break;
    }
    return 0;
}

/*!
 * \brief   setColormap(LPBITMAPINFO pbmi, PIXCMAP *cmap)
 *
 * \param[in]    pbmi pointer to a BITMAPINFO describing a DIB
 * \param[in]    cmap leptonica colormap
 * \return  number of colors in cmap
 */
static int
setColormap(LPBITMAPINFO  pbmi,
            PIXCMAP      *cmap)
{
l_int32  i, nColors, rval, gval, bval;

    nColors = pixcmapGetCount(cmap);
    for (i = 0; i < nColors; i++) {
        pixcmapGetColor(cmap, i, &rval, &gval, &bval);
        pbmi->bmiColors[i].rgbRed = rval;
        pbmi->bmiColors[i].rgbGreen = gval;
        pbmi->bmiColors[i].rgbBlue = bval;
        pbmi->bmiColors[i].rgbReserved = 0;
    }
    pbmi->bmiHeader.biClrUsed = nColors;
    return nColors;
}

/* **********************************************************************
  HBITMAP DSCreateBitmapInfo(l_int32 width, l_int32 height, l_int32 depth,
                             PIXCMAP *cmap)

  PARAMETERS:
    l_int32 width - Desired width of the DIBSection
    l_int32 height - Desired height of the DIBSection
    l_int32 depth - Desired bit-depth of the DIBSection
    PIXCMAP cmap - leptonica colormap for depths < 16

  RETURNS:
    LPBITMAPINFO - a ptr to BITMAPINFO of the desired size and bit-depth
                   NULL on failure

  REMARKS:
    Creates a BITMAPINFO based on the criteria passed in as parameters.

********************************************************************** */
static LPBITMAPINFO
DSCreateBitmapInfo(l_int32 width,
                   l_int32 height,
                   l_int32 depth,
                   PIXCMAP *cmap)
{
l_int32       nInfoSize;
LPBITMAPINFO  pbmi;
LPDWORD       pMasks;

    nInfoSize = sizeof(BITMAPINFOHEADER);
    if( depth <= 8 )
        nInfoSize += sizeof(RGBQUAD) * (1 << depth);
    if((depth == 16) || (depth == 32))
        nInfoSize += (3 * sizeof(DWORD));

        /* Create the header big enough to contain color table and
         * bitmasks if needed. */
    pbmi = (LPBITMAPINFO)malloc(nInfoSize);
    if (!pbmi)
        return NULL;

    ZeroMemory(pbmi, nInfoSize);
    pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    pbmi->bmiHeader.biWidth = width;
    pbmi->bmiHeader.biHeight = height;
    pbmi->bmiHeader.biPlanes = 1;
    pbmi->bmiHeader.biBitCount = depth;

        /* override below for 16 and 32 bpp */
    pbmi->bmiHeader.biCompression = BI_RGB;

        /*  ?? not sure if this is right?  */
    pbmi->bmiHeader.biSizeImage = DSImageBitsSize(pbmi);

    pbmi->bmiHeader.biXPelsPerMeter = 0;
    pbmi->bmiHeader.biYPelsPerMeter = 0;
    pbmi->bmiHeader.biClrUsed = 0;  /* override below */
    pbmi->bmiHeader.biClrImportant = 0;

    switch(depth)
    {
        case 24:
                /* 24bpp requires no special handling */
            break;
        case 16:
                /* if it's 16bpp, fill in the masks and override the
                 * compression.  These are the default masks -- you
                 * could change them if needed. */
            pMasks = (LPDWORD)(pbmi->bmiColors);
            pMasks[0] = 0x00007c00;
            pMasks[1] = 0x000003e0;
            pMasks[2] = 0x0000001f;
            pbmi->bmiHeader.biCompression = BI_BITFIELDS;
            break;
        case 32:
                /* if it's 32 bpp, fill in the masks and override
                 * the compression */
            pMasks = (LPDWORD)(pbmi->bmiColors);
            /*pMasks[0] = 0x00ff0000; */
            /*pMasks[1] = 0x0000ff00; */
            /*pMasks[2] = 0x000000ff; */
            pMasks[0] = 0xff000000;
            pMasks[1] = 0x00ff0000;
            pMasks[2] = 0x0000ff00;

            pbmi->bmiHeader.biCompression = BI_BITFIELDS;
            break;
        case 8:
        case 4:
        case 1:
            setColormap(pbmi, cmap);
            break;
    }
    return pbmi;
}

/* **********************************************************************
  HBITMAP DSCreateDIBSection(l_int32 width, l_int32 height, l_int32 depth,
                             PIXCMAP *cmap)

  PARAMETERS:
    l_int32 width - Desired width of the DIBSection
    l_int32 height - Desired height of the DIBSection
    l_int32 depth - Desired bit-depth of the DIBSection
    PIXCMAP cmap - leptonica colormap for depths < 16

  RETURNS:
    HBITMAP      - a DIBSection HBITMAP of the desired size and bit-depth
                   NULL on failure

  REMARKS:
    Creates a DIBSection based on the criteria passed in as parameters.

********************************************************************** */
static HBITMAP
DSCreateDIBSection(l_int32  width,
                   l_int32  height,
                   l_int32  depth,
                   PIXCMAP  *cmap)
{
HBITMAP       hBitmap;
l_int32       nInfoSize;
LPBITMAPINFO  pbmi;
HDC           hRefDC;
LPBYTE        pBits;

    pbmi = DSCreateBitmapInfo (width, height, depth, cmap);
    if (!pbmi)
        return NULL;

    hRefDC = GetDC(NULL);
    hBitmap = CreateDIBSection(hRefDC, pbmi, DIB_RGB_COLORS,
                               (void **) &pBits, NULL, 0);
    nInfoSize = GetLastError();
    ReleaseDC(NULL, hRefDC);
    free(pbmi);

    return hBitmap;
}


/*!
 * \brief   pixGetWindowsHBITMAP()
 *
 * \param[in]    pix
 * \return  Windows hBitmap, or NULL on error
 *
 * <pre>
 * Notes:
 *      (1) It's the responsibility of the caller to destroy the
 *          returned hBitmap with a call to DeleteObject (or with
 *          something that eventually calls DeleteObject).
 * </pre>
 */
HBITMAP
pixGetWindowsHBITMAP(PIX  *pix)
{
l_int32    width, height, depth;
l_uint32  *data;
HBITMAP    hBitmap = NULL;
BITMAP     bm;
DWORD      imageBitsSize;
PIX       *pixt = NULL;
PIXCMAP   *cmap;

    PROCNAME("pixGetWindowsHBITMAP");
    if (!pix)
        return (HBITMAP)ERROR_PTR("pix not defined", procName, NULL);

    pixGetDimensions(pix, &width, &height, &depth);
    cmap = pixGetColormap(pix);

    if (depth == 24) depth = 32;
    if (depth == 2) {
        pixt = pixConvert2To8(pix, 0, 85, 170, 255, TRUE);
        if (!pixt)
            return (HBITMAP)ERROR_PTR("unable to convert pix from 2bpp to 8bpp",
                    procName, NULL);
        depth = pixGetDepth(pixt);
        cmap = pixGetColormap(pixt);
    }

    if (depth < 16) {
        if (!cmap)
            cmap = pixcmapCreateLinear(depth, 1<<depth);
    }

    hBitmap = DSCreateDIBSection(width, height, depth, cmap);
    if (!hBitmap)
        return (HBITMAP)ERROR_PTR("Unable to create HBITMAP", procName, NULL);

        /* By default, Windows assumes bottom up images */
    if (pixt)
        pixt = pixFlipTB(pixt, pixt);
    else
        pixt = pixFlipTB(NULL, pix);

        /* "standard" color table assumes bit off=black */
    if (depth == 1) {
        pixInvert(pixt, pixt);
    }

        /* Don't byte swap until done manipulating pix! */
    if (depth <= 16)
        pixEndianByteSwap(pixt);

    GetObject (hBitmap, sizeof(BITMAP), &bm);
    imageBitsSize = ImageBitsSize(hBitmap);
    data = pixGetData(pixt);
    if (data) {
        memcpy (bm.bmBits, data, imageBitsSize);
    } else {
        DeleteObject (hBitmap);
        hBitmap = NULL;
    }
    pixDestroy(&pixt);

    return hBitmap;
}

#endif   /* _WIN32 */