The R Project SVN R

Rev

Rev 42641 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 42641 Rev 45017
Line 28... Line 28...
28
/*
28
/*
29
 *  Internal bitmap deletion function.
29
 *  Internal bitmap deletion function.
30
 */
30
 */
31
static void private_delbitmap(bitmap obj)
31
static void private_delbitmap(bitmap obj)
32
{
32
{
33
	if (obj->handle)
33
    if (obj->handle)
34
		DeleteObject((HBITMAP) obj->handle);
34
	DeleteObject((HBITMAP) obj->handle);
35
}
35
}
36
 
36
 
37
/*
37
/*
38
 *  Create/return the base bitmap object.
38
 *  Create/return the base bitmap object.
39
 */
39
 */
40
static object get_bitmap_base(void)
40
static object get_bitmap_base(void)
41
{
41
{
42
	static object bitmap_base = NULL;
42
    static object bitmap_base = NULL;
43
 
43
 
44
	if (! bitmap_base)
44
    if (! bitmap_base)
45
		bitmap_base = new_object(BaseObject, 0, NULL);
45
	bitmap_base = new_object(BaseObject, 0, NULL);
46
	return bitmap_base;
46
    return bitmap_base;
47
}
47
}
48
 
48
 
49
/*
49
/*
50
 *  Create a white bitmap.
50
 *  Create a white bitmap.
51
 */
51
 */
52
bitmap newbitmap(int width, int height, int depth)
52
bitmap newbitmap(int width, int height, int depth)
53
{
53
{
54
	BITMAP bm;
54
    BITMAP bm;
55
	HBITMAP hb, old;
55
    HBITMAP hb, old;
56
	HDC dc, screendc;
56
    HDC dc, screendc;
57
	bitmap obj;
57
    bitmap obj;
58
 
58
 
59
	screendc = GetDC(0); /* get screen dc */
59
    screendc = GetDC(0); /* get screen dc */
60
 
60
 
61
	if (depth == 1) /* monochrome bitmap required */
61
    if (depth == 1) /* monochrome bitmap required */
62
	{
62
    {
63
		hb = CreateBitmap(width, height, 1, 1, 0);
63
	hb = CreateBitmap(width, height, 1, 1, 0);
64
	}
64
    }
65
	else /* colour bitmap required */
65
    else /* colour bitmap required */
66
	{
66
    {
67
		hb = CreateCompatibleBitmap(screendc, width, height);
67
	hb = CreateCompatibleBitmap(screendc, width, height);
68
	}
68
    }
69
 
69
 
70
	if (! hb) {
70
    if (! hb) {
71
		ReleaseDC(0, screendc);
71
	ReleaseDC(0, screendc);
72
		return NULL;
72
	return NULL;
73
	}
73
    }
74
 
74
 
75
	dc = CreateCompatibleDC(screendc);
75
    dc = CreateCompatibleDC(screendc);
76
	old = SelectObject(dc, hb);
76
    old = SelectObject(dc, hb);
77
	PatBlt(dc, 0, 0, width, height, WHITENESS);
77
    PatBlt(dc, 0, 0, width, height, WHITENESS);
78
	SelectObject(dc, old);
78
    SelectObject(dc, old);
79
	DeleteDC(dc);
79
    DeleteDC(dc);
80
 
80
 
81
	ReleaseDC(0, screendc);
81
    ReleaseDC(0, screendc);
82
 
82
 
83
	GetObject(hb, sizeof(BITMAP), (LPSTR) &bm);
83
    GetObject(hb, sizeof(BITMAP), (LPSTR) &bm);
84
 
84
 
85
	obj = new_object(BitmapObject, hb, get_bitmap_base());
85
    obj = new_object(BitmapObject, hb, get_bitmap_base());
86
	if (! obj) {
86
    if (! obj) {
87
		DeleteObject(hb);
87
	DeleteObject(hb);
88
		return NULL;
88
	return NULL;
89
	}
89
    }
90
	obj->rect = rect(0,0,width,height);
90
    obj->rect = rect(0,0,width,height);
91
	obj->depth = (bm.bmPlanes * bm.bmBitsPixel);
91
    obj->depth = (bm.bmPlanes * bm.bmBitsPixel);
92
	obj->die = private_delbitmap;
92
    obj->die = private_delbitmap;
93
 
93
 
94
	return obj;
94
    return obj;
95
}
95
}
96
 
96
 
97
/*
97
/*
98
 *  Check an image to see if there are any transparent pixels.
98
 *  Check an image to see if there are any transparent pixels.
99
 *  Return 1 as soon as a transparent pixel is found, 0 if none are.
99
 *  Return 1 as soon as a transparent pixel is found, 0 if none are.
100
 */
100
 */
101
PROTECTED
101
PROTECTED
102
int has_transparent_pixels(image img)
102
int has_transparent_pixels(image img)
103
{
103
{
104
	long i, width, height, total;
104
    long i, width, height, total;
105
	rgb *palette;
105
    rgb *palette;
106
	byte *pixel8;
106
    byte *pixel8;
107
	rgb *pixel32;
107
    rgb *pixel32;
108
	rgb col;
108
    rgb col;
109
 
-
 
110
	if (! img)
-
 
111
		return 0;
-
 
112
 
-
 
113
	width = getwidth(img);
-
 
114
	height = getheight(img);
-
 
115
	total = width * height;
-
 
116
 
-
 
117
	palette = getpalette(img);
-
 
118
 
-
 
119
	pixel8 = getpixels(img);
-
 
120
	pixel32 = (rgb *) pixel8;
-
 
121
 
-
 
122
	if (getdepth(img) == 8) {
-
 
123
		for (i=0; i < total; i++) {
-
 
124
			col = pixel8[i];
-
 
125
			col = palette[col];
-
 
126
			if (getalpha(col) > 0x7F)
-
 
127
				return 1;
-
 
128
		}
-
 
129
	}
-
 
130
	else {
-
 
131
		for (i=0; i < total; i++) {
-
 
132
			col = pixel32[i];
-
 
133
			if (getalpha(col) > 0x7F)
-
 
134
				return 1;
-
 
135
		}
-
 
136
	}
-
 
137
 
109
 
-
 
110
    if (! img)
138
	return 0;
111
	return 0;
-
 
112
 
-
 
113
    width = getwidth(img);
-
 
114
    height = getheight(img);
-
 
115
    total = width * height;
-
 
116
 
-
 
117
    palette = getpalette(img);
-
 
118
 
-
 
119
    pixel8 = getpixels(img);
-
 
120
    pixel32 = (rgb *) pixel8;
-
 
121
 
-
 
122
    if (getdepth(img) == 8) {
-
 
123
	for (i=0; i < total; i++) {
-
 
124
	    col = pixel8[i];
-
 
125
	    col = palette[col];
-
 
126
	    if (getalpha(col) > 0x7F)
-
 
127
		return 1;
-
 
128
	}
-
 
129
    }
-
 
130
    else {
-
 
131
	for (i=0; i < total; i++) {
-
 
132
	    col = pixel32[i];
-
 
133
	    if (getalpha(col) > 0x7F)
-
 
134
		return 1;
-
 
135
	}
-
 
136
    }
-
 
137
 
-
 
138
    return 0;
139
}
139
}
140
 
140
 
141
/*
141
/*
142
 *  Create a bitmap version of an image.
142
 *  Create a bitmap version of an image.
143
 */
143
 */
144
bitmap imagetobitmap(image img)
144
bitmap imagetobitmap(image img)
145
{
145
{
146
	HBITMAP hb;
146
    HBITMAP hb;
147
	HDC screendc;
147
    HDC screendc;
148
	bitmap obj;
148
    bitmap obj;
149
	int width, height, depth, palsize;
149
    int width, height, depth, palsize;
150
	unsigned long x, y, i, row_bytes;
150
    unsigned long x, y, i, row_bytes;
151
	byte *block, *data, *pixel8;
151
    byte *block, *data, *pixel8;
152
	rgb *palette, *pixel32;
152
    rgb *palette, *pixel32;
153
	rgb colour;
153
    rgb colour;
154
	unsigned long size;
154
    unsigned long size;
155
	BITMAPINFO * bmi;
155
    BITMAPINFO * bmi;
156
 
156
 
157
	if (! img)
157
    if (! img)
158
		return NULL;
158
	return NULL;
159
 
-
 
160
	/* remember some facts about the image */
-
 
161
	width = getwidth(img);
-
 
162
	height = getheight(img);
-
 
163
	depth = (getdepth(img) == 8) ? 8 : 24;
-
 
164
	palette = getpalette(img);
-
 
165
	palsize = getpalettesize(img);
-
 
166
	pixel8 = getpixels(img);
-
 
167
	pixel32 = (rgb *) pixel8;
-
 
168
 
-
 
169
	/* create DIB info in memory */
-
 
170
	size = sizeof(BITMAPINFOHEADER) + 4;
-
 
171
	if (depth == 8) {
-
 
172
		row_bytes = ((width + 3) / 4) * 4;
-
 
173
		size = size + (sizeof(RGBQUAD) * palsize);
-
 
174
		size = size + (row_bytes * height);
-
 
175
	} else {
-
 
176
		row_bytes = (((width * 3) + 3) / 4) * 4;
-
 
177
		size = size + (row_bytes * height);
-
 
178
	}
-
 
179
 
159
 
-
 
160
    /* remember some facts about the image */
-
 
161
    width = getwidth(img);
-
 
162
    height = getheight(img);
-
 
163
    depth = (getdepth(img) == 8) ? 8 : 24;
-
 
164
    palette = getpalette(img);
-
 
165
    palsize = getpalettesize(img);
-
 
166
    pixel8 = getpixels(img);
-
 
167
    pixel32 = (rgb *) pixel8;
-
 
168
 
-
 
169
    /* create DIB info in memory */
-
 
170
    size = sizeof(BITMAPINFOHEADER) + 4;
-
 
171
    if (depth == 8) {
-
 
172
	row_bytes = ((width + 3) / 4) * 4;
-
 
173
	size = size + (sizeof(RGBQUAD) * palsize);
-
 
174
	size = size + (row_bytes * height);
-
 
175
    } else {
-
 
176
	row_bytes = (((width * 3) + 3) / 4) * 4;
-
 
177
	size = size + (row_bytes * height);
-
 
178
    }
-
 
179
 
180
	/* create the block, align on LONG boundary
180
    /* create the block, align on LONG boundary
181
	block = array(size, byte);
181
       block = array(size, byte);
182
	i = ((long)block) % 4;
182
       i = ((long)block) % 4;
183
	bmi = (BITMAPINFO *) (block + (4 - i));
183
       bmi = (BITMAPINFO *) (block + (4 - i));
184
	
184
	
185
	malloc will have done the alignment, which needs to 
185
       malloc will have done the alignment, which needs to 
186
	be for pointers.
186
       be for pointers.
187
	*/
187
    */
188
	block = array(size, byte);
188
    block = array(size, byte);
189
	bmi = (BITMAPINFO *) block;
189
    bmi = (BITMAPINFO *) block;
190
	
190
	
191
 
191
 
192
	/* assign header info */
192
    /* assign header info */
193
	bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
193
    bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
194
	bmi->bmiHeader.biWidth = width;
194
    bmi->bmiHeader.biWidth = width;
195
	bmi->bmiHeader.biHeight = height;
195
    bmi->bmiHeader.biHeight = height;
196
	bmi->bmiHeader.biPlanes = 1;
196
    bmi->bmiHeader.biPlanes = 1;
197
	bmi->bmiHeader.biBitCount = depth;
197
    bmi->bmiHeader.biBitCount = depth;
198
	bmi->bmiHeader.biCompression = BI_RGB;
198
    bmi->bmiHeader.biCompression = BI_RGB;
199
	bmi->bmiHeader.biClrUsed = palsize;
199
    bmi->bmiHeader.biClrUsed = palsize;
200
 
200
 
201
	/* assign colour table */
201
    /* assign colour table */
202
	for (i=0; i < palsize; i++) {
202
    for (i=0; i < palsize; i++) {
203
		colour  = palette[i];
203
	colour  = palette[i];
204
		data = (byte *) (& bmi->bmiColors[i]);
204
	data = (byte *) (& bmi->bmiColors[i]);
205
		data[0] = getblue(colour);
205
	data[0] = getblue(colour);
206
		data[1] = getgreen(colour);
206
	data[1] = getgreen(colour);
207
		data[2] = getred(colour);
207
	data[2] = getred(colour);
208
		data[3] = 0;
208
	data[3] = 0;
209
	}
209
    }
210
 
210
 
211
	/* assign the bitmap data itself */
211
    /* assign the bitmap data itself */
212
	data = (byte *) (& bmi->bmiColors[palsize]);
212
    data = (byte *) (& bmi->bmiColors[palsize]);
213
 
213
 
214
	if (depth == 8)
214
    if (depth == 8)
215
	  for (y=0; y < height; y++) {
215
	for (y=0; y < height; y++) {
216
	    for (x=0; x < width; x++) {
216
	    for (x=0; x < width; x++) {
217
		i = ((height - y - 1) * row_bytes) + x;
217
		i = ((height - y - 1) * row_bytes) + x;
218
		data[i] = pixel8[y * width + x];
218
		data[i] = pixel8[y * width + x];
219
	    }
219
	    }
220
	  }
220
	}
221
	else
221
    else
222
	  for (y=0; y < height; y++) {
222
	for (y=0; y < height; y++) {
223
	    for (x=0; x < width; x++) {
223
	    for (x=0; x < width; x++) {
224
		colour = pixel32[y * width + x];
224
		colour = pixel32[y * width + x];
225
		i = ((height - y - 1) * row_bytes) + (x * 3);
225
		i = ((height - y - 1) * row_bytes) + (x * 3);
226
		data[i] = getblue(colour);
226
		data[i] = getblue(colour);
227
		data[i+1] = getgreen(colour);
227
		data[i+1] = getgreen(colour);
228
		data[i+2] = getred(colour);
228
		data[i+2] = getred(colour);
229
	    }
229
	    }
230
	  }
230
	}
231
 
231
 
232
	/* create the bitmap from the DIB data */
232
    /* create the bitmap from the DIB data */
233
 
233
 
234
	screendc = GetDC(0);	/* get screen dc */
234
    screendc = GetDC(0);	/* get screen dc */
235
	hb = CreateDIBitmap(screendc, & bmi->bmiHeader, CBM_INIT,
235
    hb = CreateDIBitmap(screendc, & bmi->bmiHeader, CBM_INIT,
236
			data, bmi, DIB_RGB_COLORS);
236
			data, bmi, DIB_RGB_COLORS);
237
	ReleaseDC(0, screendc);	/* release screen dc */
237
    ReleaseDC(0, screendc);	/* release screen dc */
238
 
238
 
239
	/* tidy up */
239
    /* tidy up */
240
	discard(block);
240
    discard(block);
241
 
241
 
242
	/* create the bitmap */
242
    /* create the bitmap */
243
	obj = new_object(BitmapObject, hb, get_bitmap_base());
243
    obj = new_object(BitmapObject, hb, get_bitmap_base());
244
	if (! obj) {
244
    if (! obj) {
245
		DeleteObject(hb);
245
	DeleteObject(hb);
246
		return NULL;
246
	return NULL;
247
	}
247
    }
248
	obj->rect = rect(0,0,width,height);
248
    obj->rect = rect(0,0,width,height);
249
	obj->depth = depth;
249
    obj->depth = depth;
250
	obj->die = private_delbitmap;
250
    obj->die = private_delbitmap;
251
 
251
 
252
	return obj;
252
    return obj;
253
}
253
}
254
 
254
 
255
#ifdef UNUSED
255
#ifdef UNUSED
256
/*
256
/*
257
 *  The loadicon function returns a HBITMAP of an ICON from resources.
257
 *  The loadicon function returns a HBITMAP of an ICON from resources.
258
 */
258
 */
259
static bitmap loadicon(const char *name)
259
static bitmap loadicon(const char *name)
260
{
260
{
261
	HICON i;
261
    HICON i;
262
	HDC dc;
262
    HDC dc;
263
	HBITMAP old;
263
    HBITMAP old;
264
	bitmap obj;
264
    bitmap obj;
265
 
-
 
266
	if (this_instance == 0)
-
 
267
		return NULL;
-
 
268
	if ((i = LoadIcon(this_instance, name)) == 0)
-
 
269
		return NULL;
-
 
270
	if ((obj = newbitmap(32, 32, 0)) == NULL)
-
 
271
		return NULL;
-
 
272
 
-
 
273
	dc = CreateCompatibleDC(0);
-
 
274
	old = SelectObject(dc, (HBITMAP)obj->handle);
-
 
275
	DrawIcon(dc, 0, 0, i);
-
 
276
	SelectObject(dc, old);
-
 
277
	DeleteDC(dc);
-
 
278
	DestroyIcon(i);
-
 
279
 
265
 
-
 
266
    if (this_instance == 0)
-
 
267
	return NULL;
-
 
268
    if ((i = LoadIcon(this_instance, name)) == 0)
-
 
269
	return NULL;
-
 
270
    if ((obj = newbitmap(32, 32, 0)) == NULL)
-
 
271
	return NULL;
-
 
272
 
-
 
273
    dc = CreateCompatibleDC(0);
-
 
274
    old = SelectObject(dc, (HBITMAP)obj->handle);
-
 
275
    DrawIcon(dc, 0, 0, i);
-
 
276
    SelectObject(dc, old);
-
 
277
    DeleteDC(dc);
-
 
278
    DestroyIcon(i);
-
 
279
 
280
	return obj;
280
    return obj;
281
}
281
}
282
 
282
 
283
/*
283
/*
284
 *  The loadpict function returns BITMAP resources.
284
 *  The loadpict function returns BITMAP resources.
285
 */
285
 */
286
static bitmap loadpict(const char *name)
286
static bitmap loadpict(const char *name)
287
{
287
{
288
	BITMAP bm;
288
    BITMAP bm;
289
	HBITMAP hb;
289
    HBITMAP hb;
290
	bitmap obj;
290
    bitmap obj;
291
 
291
 
292
	if (this_instance == 0)
292
    if (this_instance == 0)
293
		return NULL;
293
	return NULL;
294
	hb = LoadBitmap(this_instance, name);
294
    hb = LoadBitmap(this_instance, name);
295
	GetObject(hb, sizeof(BITMAP), (LPSTR) &bm);
295
    GetObject(hb, sizeof(BITMAP), (LPSTR) &bm);
296
 
296
 
297
	obj = new_object(BitmapObject, hb, get_bitmap_base());
297
    obj = new_object(BitmapObject, hb, get_bitmap_base());
298
	if (obj) {
298
    if (obj) {
299
		obj->rect = rect(0, 0, bm.bmWidth, bm.bmHeight);
299
	obj->rect = rect(0, 0, bm.bmWidth, bm.bmHeight);
300
		obj->depth = (bm.bmPlanes * bm.bmBitsPixel);
300
	obj->depth = (bm.bmPlanes * bm.bmBitsPixel);
301
		obj->die = private_delbitmap;
301
	obj->die = private_delbitmap;
302
	}
302
    }
303
	return obj;
303
    return obj;
304
}
304
}
305
 
305
 
306
/*
306
/*
307
 *  The loadbitmap function finds and returns bitmaps from resources,
307
 *  The loadbitmap function finds and returns bitmaps from resources,
308
 *  or from an image file.
308
 *  or from an image file.
309
 */
309
 */
310
bitmap loadbitmap(const char *name)
310
bitmap loadbitmap(const char *name)
311
{
311
{
312
	bitmap obj;
312
    bitmap obj;
313
 
313
 
314
	if ((obj = loadpict(name)) != NULL)
314
    if ((obj = loadpict(name)) != NULL)
315
		return obj;
315
	return obj;
316
	if ((obj = loadicon(name)) != NULL)
316
    if ((obj = loadicon(name)) != NULL)
317
		return obj;
317
	return obj;
318
	if ((obj = imagetobitmap(loadimage(name))) != NULL)
318
    if ((obj = imagetobitmap(loadimage(name))) != NULL)
319
		return obj;
319
	return obj;
320
	return NULL;
320
    return NULL;
321
}
321
}
322
 
322
 
323
/*
323
/*
324
 *  The following functions are obsolete from version 2.4.
324
 *  The following functions are obsolete from version 2.4.
325
 */
325
 */
326
 
326
 
327
void setbitmapdata(bitmap obj, unsigned char *data)
327
void setbitmapdata(bitmap obj, unsigned char *data)
328
{
328
{
329
	rect r;
329
    rect r;
330
	int depth, size, row_bytes;
330
    int depth, size, row_bytes;
331
	int x, y;
331
    int x, y;
332
	unsigned char *newdata = NULL;
332
    unsigned char *newdata = NULL;
333
 
333
 
334
	r = obj->rect;
334
    r = obj->rect;
335
	depth = obj->depth;
335
    depth = obj->depth;
336
 
336
 
337
	/* Calculate source row bytes. */
337
    /* Calculate source row bytes. */
338
	row_bytes = ((depth * r.width) + 7) / 8;
338
    row_bytes = ((depth * r.width) + 7) / 8;
339
	/* Each row must be a multiple of 16 bits wide. */
339
    /* Each row must be a multiple of 16 bits wide. */
340
	if (row_bytes % 2) {
340
    if (row_bytes % 2) {
341
		/* Odd number of bytes, must assign into new array. */
341
	/* Odd number of bytes, must assign into new array. */
342
		size = (row_bytes+1) * r.height;
342
	size = (row_bytes+1) * r.height;
343
		newdata = array (size, byte);
343
	newdata = array (size, byte);
344
		if (! newdata)
344
	if (! newdata)
345
			return;
345
	    return;
346
		for (y=0; y<r.height; y++) {
346
	for (y=0; y<r.height; y++) {
347
			for (x=0; x<row_bytes; x++) {
347
	    for (x=0; x<row_bytes; x++) {
348
				newdata[y*(row_bytes+1)+x] =
348
		newdata[y*(row_bytes+1)+x] =
349
					data[y*row_bytes+x];
349
		    data[y*row_bytes+x];
350
			}
350
	    }
351
		}
-
 
352
		SetBitmapBits((HBITMAP)obj->handle, size,
-
 
353
				(LPSTR)newdata);
-
 
354
		discard(newdata);
-
 
355
	} else {
-
 
356
		/* Correct format already! */
-
 
357
		size = row_bytes * r.height;
-
 
358
		SetBitmapBits((HBITMAP)obj->handle, size, (LPSTR)data);
-
 
359
	}
351
	}
-
 
352
	SetBitmapBits((HBITMAP)obj->handle, size,
-
 
353
		      (LPSTR)newdata);
-
 
354
	discard(newdata);
-
 
355
    } else {
-
 
356
	/* Correct format already! */
-
 
357
	size = row_bytes * r.height;
-
 
358
	SetBitmapBits((HBITMAP)obj->handle, size, (LPSTR)data);
-
 
359
    }
360
}
360
}
361
 
361
 
362
void getbitmapdata(bitmap obj, unsigned char *data)
362
void getbitmapdata(bitmap obj, unsigned char *data)
363
{
363
{
364
	rect r;
364
    rect r;
365
	int depth, size, row_bytes;
365
    int depth, size, row_bytes;
366
	int x, y;
366
    int x, y;
367
	unsigned char *newdata = NULL;
367
    unsigned char *newdata = NULL;
368
 
368
 
369
	r = obj->rect;
369
    r = obj->rect;
370
	depth = obj->depth;
370
    depth = obj->depth;
371
 
371
 
372
	/* Calculate destination row bytes. */
372
    /* Calculate destination row bytes. */
373
	row_bytes = ((depth * r.width) + 7) / 8;
373
    row_bytes = ((depth * r.width) + 7) / 8;
374
	/* Each row must be a multiple of 16 bits wide. */
374
    /* Each row must be a multiple of 16 bits wide. */
375
	if (row_bytes % 2) {
375
    if (row_bytes % 2) {
376
		/* Odd number of bytes, must assign into new array. */
376
	/* Odd number of bytes, must assign into new array. */
377
		size = (row_bytes+1) * r.height;
377
	size = (row_bytes+1) * r.height;
378
		newdata = array (size, byte);
378
	newdata = array (size, byte);
379
		if (! newdata)
379
	if (! newdata)
380
			return;
380
	    return;
381
		GetBitmapBits((HBITMAP)obj->handle, size,
381
	GetBitmapBits((HBITMAP)obj->handle, size,
382
				(LPSTR)newdata);
382
		      (LPSTR)newdata);
383
		for (y=0; y<r.height; y++) {
383
	for (y=0; y<r.height; y++) {
384
			for (x=0; x<row_bytes; x++) {
384
	    for (x=0; x<row_bytes; x++) {
385
				data[y*row_bytes+x] =
385
		data[y*row_bytes+x] =
386
					newdata[y*(row_bytes+1)+x];
386
		    newdata[y*(row_bytes+1)+x];
387
			}
387
	    }
388
		}
-
 
389
		discard(newdata);
-
 
390
	} else {
-
 
391
		/* Correct format already! */
-
 
392
		size = row_bytes * r.height;
-
 
393
		GetBitmapBits((HBITMAP)obj->handle, size, (LPSTR)data);
-
 
394
	}
388
	}
-
 
389
	discard(newdata);
-
 
390
    } else {
-
 
391
	/* Correct format already! */
-
 
392
	size = row_bytes * r.height;
-
 
393
	GetBitmapBits((HBITMAP)obj->handle, size, (LPSTR)data);
-
 
394
    }
395
}
395
}
396
 
396
 
397
bitmap createbitmap(int width, int height, int depth, unsigned char *data)
397
bitmap createbitmap(int width, int height, int depth, unsigned char *data)
398
{
398
{
399
	bitmap obj;
399
    bitmap obj;
400
 
400
 
401
	if ((obj = newbitmap(width, height, depth)) == NULL)
401
    if ((obj = newbitmap(width, height, depth)) == NULL)
402
		return NULL;
402
	return NULL;
403
	if (data)
403
    if (data)
404
		setbitmapdata(obj, data);
404
	setbitmapdata(obj, data);
405
	return obj;
405
    return obj;
406
}
406
}
407
#endif
407
#endif
408
 
408
 
409
void getbitmapdata2(bitmap obj, unsigned char **data)
409
void getbitmapdata2(bitmap obj, unsigned char **data)
410
{
410
{
411
	rect r = obj->rect;
411
    rect r = obj->rect;
412
	int depth = 32, size, ret;
412
    int depth = 32, size, ret;
413
	BITMAPINFO bmi;
413
    BITMAPINFO bmi;
414
 
414
 
415
	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
415
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
416
	bmi.bmiHeader.biWidth = r.width;
416
    bmi.bmiHeader.biWidth = r.width;
417
	bmi.bmiHeader.biHeight = -r.height;
417
    bmi.bmiHeader.biHeight = -r.height;
418
	bmi.bmiHeader.biPlanes = 1;
418
    bmi.bmiHeader.biPlanes = 1;
419
	bmi.bmiHeader.biBitCount = depth;
419
    bmi.bmiHeader.biBitCount = depth;
420
	bmi.bmiHeader.biCompression = BI_RGB;
420
    bmi.bmiHeader.biCompression = BI_RGB;
421
	bmi.bmiHeader.biClrUsed = 0;
421
    bmi.bmiHeader.biClrUsed = 0;
422
 
422
 
423
	size = 4 * r.width * r.height;
423
    size = 4 * r.width * r.height;
424
	*data = (unsigned char *) malloc(size);
424
    *data = (unsigned char *) malloc(size);
425
	if(*data) {
425
    if(*data) {
426
	    ret = GetDIBits(get_context(obj), (HBITMAP)obj->handle, 
426
	ret = GetDIBits(get_context(obj), (HBITMAP)obj->handle, 
427
			    0, r.height, (LPSTR)*data, &bmi, DIB_RGB_COLORS);
427
			0, r.height, (LPSTR)*data, &bmi, DIB_RGB_COLORS);
428
	    if(!ret) {
428
	if(!ret) {
429
		free(*data);
429
	    free(*data);
430
		*data = NULL;
430
	    *data = NULL;
431
	    }
-
 
432
	}
431
	}
-
 
432
    }
433
}
433
}
434
 
434