summaryrefslogtreecommitdiff
path: root/im/src/im_dibxbitmap.cpp
blob: 8fabd4af4164f7e62265b1adfb4f31a25d02743b (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
/** \file
 * \brief Conversion between imDib and imImage
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_dibxbitmap.cpp,v 1.1 2008/10/17 06:10:16 scuri Exp $
 */


#include <windows.h>
#include <assert.h>

#include "im.h"
#include "im_image.h"
#include "im_dib.h"
#include "im_util.h"


void imDibEncodeFromBitmap(imDib* dib, const unsigned char* data)
{
  int x, y;
  BYTE* bits;
  
  assert(dib);
  assert(dib->bmih->biBitCount > 16);
  assert(data);
  
  if (dib->bmih->biHeight < 0)
    bits = dib->bits + (dib->bits_size - dib->line_size); /* start of last line */
  else
    bits = dib->bits;
  
  for (y = 0; y < abs(dib->bmih->biHeight); y++)
  {
    for (x = 0; x < dib->bmih->biWidth; x++)
    {
      *bits++ = *(data+2);   // R
      *bits++ = *(data+1);   // G
      *bits++ = *(data+0);   // B

      data += 3;

      if (dib->bmih->biBitCount == 32)
        *bits++ = *data++;
    }
    
    bits += dib->pad_size;

    if (dib->bmih->biHeight < 0)
      bits -= 2*dib->line_size;
  }
}

void imDibDecodeToBitmap(const imDib* dib, unsigned char* data)
{
  int x, y, offset;
  unsigned short color;
  BYTE* bits;
  unsigned int rmask = 0, gmask = 0, bmask = 0, 
                roff = 0,  goff = 0,  boff = 0; /* pixel bit mask control when reading 16 and 32 bpp images */
  
  assert(dib);
  assert(dib->bmih->biBitCount > 8);
  assert(data);

  if (dib->bmih->biHeight < 0)
    bits = dib->bits + (dib->bits_size - dib->line_size); /* start of last line */
  else
    bits = dib->bits;
  
  if (dib->bmih->biBitCount == 16)
    offset = dib->line_size;  /* do not increment for each pixel, jump line */
  else
    offset = dib->pad_size;   /* increment for each pixel, jump pad */
  
  if (dib->bmih->biCompression == BI_BITFIELDS)
  {
    unsigned int Mask;
    unsigned int* palette = (unsigned int*)dib->bmic;
    
    rmask = Mask = palette[0];
    while (!(Mask & 0x01))
    {Mask >>= 1; roff++;}
    
    gmask = Mask = palette[1];
    while (!(Mask & 0x01))
    {Mask >>= 1; goff++;}
    
    bmask = Mask = palette[2];
    while (!(Mask & 0x01))
    {Mask >>= 1; boff++;}
  }
  else if (dib->bmih->biBitCount == 16)
  {
    bmask = 0x001F;
    gmask = 0x03E0;
    rmask = 0x7C00;
    boff = 0;
    goff = 5;
    roff = 10;
  }
  
  for (y = 0; y < abs(dib->bmih->biHeight); y++)
  {
    for (x = 0; x < dib->bmih->biWidth; x++)
    {
      if (dib->bmih->biBitCount == 16)
      {
        color = ((unsigned short*)bits)[x];
        *data++ = (unsigned char)((((rmask & color) >> roff) * 255) / (rmask >> roff));
        *data++ = (unsigned char)((((gmask & color) >> goff) * 255) / (gmask >> goff));
        *data++ = (unsigned char)((((bmask & color) >> boff) * 255) / (bmask >> boff));
      }
      else
      {
        *(data+2) = *bits++; // B
        *(data+1) = *bits++; // G
        *(data+0) = *bits++; // R

        data += 3;
        
        if (dib->bmih->biBitCount == 32)
          *data++ = *bits++;
      }
    }
    
    bits += offset;

    if (dib->bmih->biHeight < 0)
      bits -= 2*dib->line_size;
  }
}

imImage* imDibToImage(const imDib* dib)
{
  assert(dib);

  int color_space = IM_RGB;
  if (dib->bmih->biBitCount <= 8)
    color_space = IM_MAP;

  imImage* image = imImageCreate(dib->bmih->biWidth, abs(dib->bmih->biHeight), color_space, IM_BYTE);
  if (!image) 
    return NULL;

  if (image->color_space == IM_MAP)
  {
    image->palette_count = dib->palette_count;
    imDibDecodeToMap(dib, (imbyte*)image->data[0], image->palette);
  }
  else
  {
    imDibDecodeToRGBA(dib, (imbyte*)image->data[0], (imbyte*)image->data[1], (imbyte*)image->data[2], NULL);
  }

  return image;
}

imDib* imDibFromImage(const imImage* image)
{
  assert(image);
  assert(imImageIsBitmap(image));

  if (!imImageIsBitmap(image))
    return NULL;

  int bpp;
  if (image->color_space != IM_RGB)
    bpp = 8;
  else
    bpp = 24;

  imDib* dib = imDibCreate(image->width, image->height, bpp);     
  if (!dib) return NULL;

  if (image->color_space != IM_RGB)
    imDibEncodeFromMap(dib, (const imbyte*)image->data[0], image->palette, image->palette_count);
  else
    imDibEncodeFromRGBA(dib, (const imbyte*)image->data[0], (const imbyte*)image->data[1], (const imbyte*)image->data[2], NULL);

  return dib;
}