Source

compiler-libs-hack / ocaml / otherlibs / win32graph / draw.c

Full commit
  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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/***********************************************************************/
/*                                                                     */
/*                                OCaml                                */
/*                                                                     */
/*  Developed by Jacob Navia, based on code by J-M Geffroy and X Leroy */
/*                                                                     */
/*  Copyright 2001 Institut National de Recherche en Informatique et   */
/*  en Automatique.  All rights reserved.  This file is distributed    */
/*  under the terms of the GNU Library General Public License, with    */
/*  the special exception on linking described in file ../../LICENSE.  */
/*                                                                     */
/***********************************************************************/

#include <math.h>
#include "mlvalues.h"
#include "alloc.h"
#include "fail.h"
#include "libgraph.h"
#include "custom.h"
#include "memory.h"

HDC gcMetaFile;
int grdisplay_mode;
int grremember_mode;
GR_WINDOW grwindow;

static void GetCurrentPosition(HDC hDC,POINT *pt)
{
        MoveToEx(hDC,0,0,pt);
        MoveToEx(hDC,pt->x,pt->y,0);
}

static value gr_draw_or_fill_arc(value vx, value vy, value vrx, value vry,
                                 value vstart, value vend, BOOL fill);

CAMLprim value caml_gr_plot(value vx, value vy)
{
        int x = Int_val(vx);
        int y = Int_val(vy);
        gr_check_open();
        if(grremember_mode)
                SetPixel(grwindow.gcBitmap, x, Wcvt(y),grwindow.CurrentColor);
        if(grdisplay_mode) {
                SetPixel(grwindow.gc, x, Wcvt(y),grwindow.CurrentColor);
        }
        return Val_unit;
}

CAMLprim value caml_gr_moveto(value vx, value vy)
{
        grwindow.grx = Int_val(vx);
        grwindow.gry = Int_val(vy);
        if(grremember_mode)
                MoveToEx(grwindow.gcBitmap,grwindow.grx,Wcvt(grwindow.gry),0);
        if (grdisplay_mode)
                MoveToEx(grwindow.gc,grwindow.grx,Wcvt(grwindow.gry),0);
        return Val_unit;
}

CAMLprim value caml_gr_current_x(void)
{
        return Val_int(grwindow.grx);
}

CAMLprim value caml_gr_current_y(void)
{
        return Val_int(grwindow.gry);
}

CAMLprim value caml_gr_lineto(value vx, value vy)
{
        int x = Int_val(vx);
        int y = Int_val(vy);
        gr_check_open();
        SelectObject(grwindow.gc,grwindow.CurrentPen);
        SelectObject(grwindow.gcBitmap,grwindow.CurrentPen);
        if (grremember_mode)
                LineTo(grwindow.gcBitmap,x,Wcvt(y));
        if (grdisplay_mode)
                LineTo(grwindow.gc, x, Wcvt(y));
        grwindow.grx = x;
        grwindow.gry = y;
        return Val_unit;
}

CAMLprim value caml_gr_draw_rect(value vx, value vy, value vw, value vh)
{
        int     x, y, w, h;
        POINT pt[5];
        x=Int_val(vx);
        y=Wcvt(Int_val(vy));
        w=Int_val(vw);
        h=Int_val(vh);

        pt[0].x = x;         pt[0].y = y - h;
        pt[1].x = x + w;     pt[1].y = y - h;
        pt[2].x = x + w;     pt[2].y = y;
        pt[3].x = x;         pt[3].y = y;
        pt[4].x = x;         pt[4].y = y - h;
        if (grremember_mode) {
                Polyline(grwindow.gcBitmap,pt, 5);
        }
        if (grdisplay_mode) {
                Polyline(grwindow.gc,pt, 5);
        }
        return Val_unit;
}

CAMLprim value caml_gr_draw_text(value text,value x)
{
        POINT pt;
        int oldmode = SetBkMode(grwindow.gc,TRANSPARENT);
        SetBkMode(grwindow.gcBitmap,TRANSPARENT);
        SetTextAlign(grwindow.gcBitmap, TA_UPDATECP|TA_BOTTOM);
        SetTextAlign(grwindow.gc, TA_UPDATECP|TA_BOTTOM);
        if (grremember_mode) {
                TextOut(grwindow.gcBitmap,0,0,(char *)text,x);
        }
        if(grdisplay_mode) {
                TextOut(grwindow.gc,0,0,(char *)text,x);
        }
        GetCurrentPosition(grwindow.gc,&pt);
        grwindow.grx = pt.x;
        grwindow.gry = grwindow.height - pt.y;
        SetBkMode(grwindow.gc,oldmode);
        SetBkMode(grwindow.gcBitmap,oldmode);
        return Val_unit;
}

CAMLprim value caml_gr_fill_rect(value vx, value vy, value vw, value vh)
{
        int x = Int_val(vx);
        int y = Int_val(vy);
        int w = Int_val(vw);
        int h = Int_val(vh);
        RECT rc;

        gr_check_open();
        rc.left = x;
        rc.top = Wcvt(y);
        rc.right = x+w;
        rc.bottom = Wcvt(y)-h;
        if (grdisplay_mode)
                FillRect(grwindow.gc,&rc,grwindow.CurrentBrush);
        if (grremember_mode)
                FillRect(grwindow.gcBitmap,&rc,grwindow.CurrentBrush);
        return Val_unit;
}

CAMLprim value caml_gr_sound(value freq, value vdur)
{
        Beep(freq,vdur);
        return Val_unit;
}

CAMLprim value caml_gr_point_color(value vx, value vy)
{
        int x = Int_val(vx);
        int y = Int_val(vy);
        COLORREF rgb;
        unsigned long b,g,r;

        gr_check_open();
        rgb = GetPixel(grwindow.gcBitmap,x,Wcvt(y));
        b = (unsigned long)((rgb & 0xFF0000) >> 16);
        g = (unsigned long)((rgb & 0x00FF00) >> 8);
        r = (unsigned long)(rgb & 0x0000FF);
        return Val_long((r<<16) + (g<<8) + b);
}

CAMLprim value caml_gr_circle(value x,value y,value radius)
{
        int left,top,right,bottom;

        gr_check_open();
        left = x - radius/2;
        top = Wcvt(y) - radius/2;
        right = left+radius;
        bottom = top+radius;
        Ellipse(grwindow.gcBitmap,left,top,right,bottom);
        return Val_unit;
}

CAMLprim value caml_gr_set_window_title(value text)
{
        SetWindowText(grwindow.hwnd,(char *)text);
        return Val_unit;
}

CAMLprim value caml_gr_draw_arc(value *argv, int argc)
{
  return gr_draw_or_fill_arc(argv[0], argv[1], argv[2], argv[3],
                             argv[4], argv[5], FALSE);
}

CAMLprim value caml_gr_draw_arc_nat(vx, vy, vrx, vry, vstart, vend)
{
  return gr_draw_or_fill_arc(vx, vy, vrx, vry, vstart, vend, FALSE);
}

CAMLprim value caml_gr_set_line_width(value vwidth)
{
        int width = Int_val(vwidth);
        HPEN oldPen,newPen;

        gr_check_open();
        oldPen = grwindow.CurrentPen;
        newPen = CreatePen(PS_SOLID,width,grwindow.CurrentColor);
        SelectObject(grwindow.gcBitmap,newPen);
        SelectObject(grwindow.gc,newPen);
        DeleteObject(oldPen);
        grwindow.CurrentPen = newPen;
        return Val_unit;
}

CAMLprim value caml_gr_set_color(value vcolor)
{
        HBRUSH oldBrush, newBrush;
        LOGBRUSH lb;
        LOGPEN pen;
        HPEN newPen;
        int color = Long_val(vcolor);

        int  r = (color & 0xFF0000) >> 16,
        g = (color & 0x00FF00) >> 8 ,
        b =  color & 0x0000FF;
        COLORREF c = RGB(r,g,b);
        memset(&lb,0,sizeof(lb));
        memset(&pen,0,sizeof(LOGPEN));
        gr_check_open();
        GetObject(grwindow.CurrentPen,sizeof(LOGPEN),&pen);
        pen.lopnColor = c;
        newPen = CreatePenIndirect(&pen);
        SelectObject(grwindow.gcBitmap,newPen);
        SelectObject(grwindow.gc,newPen);
        DeleteObject(grwindow.CurrentPen);
        grwindow.CurrentPen = newPen;
        SetTextColor(grwindow.gc,c);
        SetTextColor(grwindow.gcBitmap,c);
        oldBrush = grwindow.CurrentBrush;
        lb.lbStyle = BS_SOLID;
        lb.lbColor = c;
        newBrush = CreateBrushIndirect(&lb);
        SelectObject(grwindow.gc,newBrush);
        SelectObject(grwindow.gcBitmap,newBrush);
        DeleteObject(oldBrush);
        grwindow.CurrentBrush = newBrush;
        grwindow.CurrentColor = c;
        return Val_unit;
}


static value gr_draw_or_fill_arc(value vx, value vy, value vrx, value vry,
                                 value vstart, value vend, BOOL fill)
{
        int x, y, r_x, r_y, start, end;
        int     x1, y1, x2, y2, x3, y3, x4, y4;
        double cvt = 3.141592653/180.0;

        r_x = Int_val(vrx);
        r_y = Int_val(vry);
        if ((r_x < 0) || (r_y < 0))
                invalid_argument("draw_arc: radius must be positive");
        x     = Int_val(vx);
        y     = Int_val(vy);
        start = Int_val(vstart);
        end   = Int_val(vend);

        // Upper-left corner of bounding rect.
        x1=     x - r_x;
        y1=     y + r_y;
        // Lower-right corner of bounding rect.
        x2=     x + r_x;
        y2=     y - r_y;
        // Starting point
        x3=x + (int)(100.0*cos(cvt*start));
        y3=y + (int)(100.0*sin(cvt*start));
        // Ending point
        x4=x + (int)(100.0*cos(cvt*end));
        y4=y + (int)(100.0*sin(cvt*end));

        if (grremember_mode) {
                SelectObject(grwindow.gcBitmap,grwindow.CurrentPen);
                SelectObject(grwindow.gcBitmap,grwindow.CurrentBrush);
                if( fill )
                        Pie(grwindow.gcBitmap,x1, Wcvt(y1), x2, Wcvt(y2),
                                x3, Wcvt(y3), x4, Wcvt(y4));
                else
                        Arc(grwindow.gcBitmap,x1, Wcvt(y1), x2, Wcvt(y2),
                                x3, Wcvt(y3), x4, Wcvt(y4));
        }
        if( grdisplay_mode ) {
                SelectObject(grwindow.gc,grwindow.CurrentPen);
                SelectObject(grwindow.gc,grwindow.CurrentBrush);
                if (fill)
                        Pie(grwindow.gc,x1, Wcvt(y1), x2, Wcvt(y2),
                                x3, Wcvt(y3), x4, Wcvt(y4));
                else
                        Arc(grwindow.gc,x1, Wcvt(y1), x2, Wcvt(y2),
                                x3, Wcvt(y3), x4, Wcvt(y4));
        }
        return Val_unit;
}

CAMLprim value caml_gr_show_bitmap(value filename,int x,int y)
{
        AfficheBitmap(filename,grwindow.gcBitmap,x,Wcvt(y));
        AfficheBitmap(filename,grwindow.gc,x,Wcvt(y));
        return Val_unit;
}



CAMLprim value caml_gr_get_mousex(void)
{
        POINT pt;
        GetCursorPos(&pt);
        MapWindowPoints(HWND_DESKTOP,grwindow.hwnd,&pt,1);
        return pt.x;
}

CAMLprim value caml_gr_get_mousey(void)
{
        POINT pt;
        GetCursorPos(&pt);
        MapWindowPoints(HWND_DESKTOP,grwindow.hwnd,&pt,1);
        return grwindow.height - pt.y - 1;
}


static void gr_font(char *fontname)
{
        HFONT hf = CreationFont(fontname);

        if (hf && hf != INVALID_HANDLE_VALUE) {
                HFONT oldFont = SelectObject(grwindow.gc,hf);
                SelectObject(grwindow.gcBitmap,hf);
                DeleteObject(grwindow.CurrentFont);
                grwindow.CurrentFont = hf;
        }
}

CAMLprim value caml_gr_set_font(value fontname)
{
        gr_check_open();
        gr_font(String_val(fontname));
        return Val_unit;
}

CAMLprim value caml_gr_set_text_size (value sz)
{
        return Val_unit;
}

CAMLprim value caml_gr_draw_char(value chr)
{
        char str[1];
        gr_check_open();
        str[0] = Int_val(chr);
        caml_gr_draw_text((value)str, 1);
        return Val_unit;
}

CAMLprim value caml_gr_draw_string(value str)
{
        gr_check_open();
        caml_gr_draw_text(str, string_length(str));
        return Val_unit;
}

CAMLprim value caml_gr_text_size(value str)
{
        SIZE extent;
        value res;

        mlsize_t len = string_length(str);
        if (len > 32767) len = 32767;

        GetTextExtentPoint(grwindow.gc,String_val(str), len,&extent);

        res = alloc_tuple(2);
        Field(res, 0) = Val_long(extent.cx);
        Field(res, 1) = Val_long(extent.cy);

        return res;
}

CAMLprim value caml_gr_fill_poly(value vect)
{
        int n_points, i;
        POINT   *p,*poly;
        n_points = Wosize_val(vect);
        if (n_points < 3)
                gr_fail("fill_poly: not enough points",0);

        poly = (POINT *)malloc(n_points*sizeof(POINT));

        p = poly;
        for( i = 0; i < n_points; i++ ){
                p->x = Int_val(Field(Field(vect,i),0));
                p->y = Wcvt(Int_val(Field(Field(vect,i),1)));
                p++;
        }
        if (grremember_mode) {
                SelectObject(grwindow.gcBitmap,grwindow.CurrentBrush);
                Polygon(grwindow.gcBitmap,poly,n_points);
        }
        if (grdisplay_mode) {
                SelectObject(grwindow.gcBitmap,grwindow.CurrentBrush);
                Polygon(grwindow.gc,poly,n_points);
        }
        free(poly);

        return Val_unit;
}

CAMLprim value caml_gr_fill_arc(value *argv, int argc)
{
  return gr_draw_or_fill_arc(argv[0], argv[1], argv[2], argv[3],
                             argv[4], argv[5], TRUE);
}

CAMLprim value caml_gr_fill_arc_nat(vx, vy, vrx, vry, vstart, vend)
{
  return gr_draw_or_fill_arc(vx, vy, vrx, vry, vstart, vend, TRUE);
}

// Image primitives
struct image {
        int w;
        int h;
        HBITMAP data;
        HBITMAP mask;
};

#define Width(i) (((struct image *)Data_custom_val(i))->w)
#define Height(i) (((struct image *)Data_custom_val(i))->h)
#define Data(i) (((struct image *)Data_custom_val(i))->data)
#define Mask(i) (((struct image *)Data_custom_val(i))->mask)
#define Max_image_mem 500000

static void finalize_image (value i)
{
        DeleteObject (Data(i));
        if (Mask(i) != NULL) DeleteObject(Mask(i));
}

static struct custom_operations image_ops = {
        "_image",
        finalize_image,
        custom_compare_default,
        custom_hash_default,
        custom_serialize_default,
        custom_deserialize_default,
        custom_compare_ext_default
};

CAMLprim value caml_gr_create_image(value vw, value vh)
{
        HBITMAP cbm;
        value res;
        int w = Int_val(vw);
        int h = Int_val(vh);

        if (w < 0 || h < 0)
                gr_fail("create_image: width and height must be positive",0);

        cbm = CreateCompatibleBitmap(grwindow.gc, w, h);
        if (cbm == NULL)
                gr_fail("create_image: cannot create bitmap", 0);
        res = alloc_custom(&image_ops, sizeof(struct image),
                w * h, Max_image_mem);
        if (res) {
                Width (res) = w;
                Height (res) = h;
                Data (res) = cbm;
                Mask (res) = NULL;
        }
        return res;
}

CAMLprim value caml_gr_blit_image (value i, value x, value y)
{
        HBITMAP oldBmp = SelectObject(grwindow.tempDC,Data(i));
        int xsrc = Int_val(x);
        int ysrc = Wcvt(Int_val(y) + Height(i) - 1);
        BitBlt(grwindow.tempDC,0, 0, Width(i), Height(i),
                grwindow.gcBitmap, xsrc, ysrc, SRCCOPY);
        SelectObject(grwindow.tempDC,oldBmp);
        return Val_unit;
}


CAMLprim value caml_gr_draw_image(value i, value x, value y)
{
        HBITMAP oldBmp;

        int xdst = Int_val(x);
        int ydst = Wcvt(Int_val(y)+Height(i)-1);
        if (Mask(i) == NULL) {
                if (grremember_mode) {
                        oldBmp = SelectObject(grwindow.tempDC,Data(i));
                        BitBlt(grwindow.gcBitmap,xdst, ydst, Width(i), Height(i),
                                grwindow.tempDC, 0, 0, SRCCOPY);
                        SelectObject(grwindow.tempDC,oldBmp);
                }
                if (grdisplay_mode) {
                        oldBmp = SelectObject(grwindow.tempDC,Data(i));
                        BitBlt(grwindow.gc,xdst, ydst, Width(i), Height(i),
                                grwindow.tempDC, 0, 0, SRCCOPY);
                        SelectObject(grwindow.tempDC,oldBmp);
                }
        }
        else {
                if (grremember_mode) {
                        oldBmp = SelectObject(grwindow.tempDC,Mask(i));
                        BitBlt(grwindow.gcBitmap,xdst, ydst, Width(i), Height(i),
                                grwindow.tempDC, 0, 0, SRCAND);
                        SelectObject(grwindow.tempDC,Data(i));
                        BitBlt(grwindow.gcBitmap,xdst, ydst, Width(i), Height(i),
                                grwindow.tempDC, 0, 0, SRCPAINT);
                        SelectObject(grwindow.tempDC,oldBmp);
                }
                if (grdisplay_mode) {
                        oldBmp = SelectObject(grwindow.tempDC,Mask(i));
                        BitBlt(grwindow.gc,xdst, ydst, Width(i), Height(i),
                                grwindow.tempDC, 0, 0, SRCAND);
                        SelectObject(grwindow.tempDC,Data(i));
                        BitBlt(grwindow.gc,xdst, ydst, Width(i), Height(i),
                                grwindow.tempDC, 0, 0, SRCPAINT);
                        SelectObject(grwindow.tempDC,oldBmp);
                }
        }

        return Val_unit;
}

CAMLprim value caml_gr_make_image(value matrix)
{
        int width, height,has_transp,i,j;
        value img;
        HBITMAP oldBmp;
        height = Wosize_val(matrix);
        if (height == 0) {
                width = 0;
        }
        else {
                width = Wosize_val(Field(matrix, 0));
                for (i = 1; i < height; i++) {
                        if (width != (int) Wosize_val(Field(matrix, i)))
                                gr_fail("make_image: non-rectangular matrix",0);
                }
        }
        Begin_roots1(matrix)
                img = caml_gr_create_image(Val_int(width), Val_int(height));
        End_roots();
        has_transp = 0;
        oldBmp = SelectObject(grwindow.tempDC,Data(img));
        for (i = 0; i < height; i++) {
                for (j = 0; j < width; j++) {
                        int col = Long_val (Field (Field (matrix, i), j));
                        if (col == -1){
                                has_transp = 1;
                                SetPixel(grwindow.tempDC,j, i, 0);
                        }
                        else {
                                int red = (col >> 16) & 0xFF;
                                int green = (col >> 8) & 0xFF;
                                int blue = col & 0xFF;
                                SetPixel(grwindow.tempDC,j, i, RGB(red, green, blue));
                        }
                }
        }
        SelectObject(grwindow.tempDC,oldBmp);
        if (has_transp) {
                HBITMAP  cbm;
                cbm = CreateCompatibleBitmap(grwindow.gc, width, height);
                Mask(img) = cbm;
                oldBmp = SelectObject(grwindow.tempDC,Mask(img));
                for (i = 0; i < height; i++) {
                        for (j = 0; j < width; j++) {
                                int col = Long_val (Field (Field (matrix, i), j));
                                SetPixel(grwindow.tempDC,j, i, col == -1 ? 0xFFFFFF : 0);
                        }
                }
                SelectObject(grwindow.tempDC,oldBmp);
        }
        return img;
}

static value alloc_int_vect(mlsize_t size)
{
        value res;
        mlsize_t i;

        if (size == 0) return Atom(0);
        if (size <= Max_young_wosize) {
                res = alloc(size, 0);
        }
        else {
                res = alloc_shr(size, 0);
        }
        for (i = 0; i < size; i++) {
                Field(res, i) = Val_long(0);
        }
        return res;
}

CAMLprim value caml_gr_dump_image (value img)
{
        int height = Height(img);
        int width = Width(img);
        value matrix = Val_unit;
        int i, j;
        HBITMAP oldBmp;

        Begin_roots2(img, matrix)
                matrix = alloc_int_vect (height);
        for (i = 0; i < height; i++) {
                modify (&Field (matrix, i), alloc_int_vect (width));
        }
        End_roots();

        oldBmp = SelectObject(grwindow.tempDC,Data(img));
        for (i = 0; i < height; i++) {
                for (j = 0; j < width; j++) {
                        int col = GetPixel(grwindow.tempDC,j, i);
                        int blue = (col >> 16) & 0xFF;
                        int green = (col >> 8) & 0xFF;
                        int red = col & 0xFF;
                        Field(Field(matrix, i), j) = Val_long((red << 16) +
                                        (green << 8) + blue);
                }
        }
        SelectObject(grwindow.tempDC,oldBmp);
        if (Mask(img) != NULL) {
                oldBmp = SelectObject(grwindow.tempDC,Mask(img));
                for (i = 0; i < height; i++) {
                        for (j = 0; j < width; j++) {
                                if (GetPixel(grwindow.tempDC,j, i) != 0)
                                        Field(Field(matrix, i), j) =
                                                Val_long(-1);
                        }
                }
                SelectObject(grwindow.tempDC,oldBmp);
        }
        return matrix;
}