OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [services/] [gfx/] [mw/] [current/] [src/] [engine/] [devopen.c] - Blame information for rev 819

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
/*
2
 * Copyright (c) 1999, 2000, 2001 Greg Haerr <greg@censoft.com>
3
 * Portions Copyright (c) 1991 David I. Bell
4
 * Permission is granted to use, distribute, or modify this source,
5
 * provided that this copyright notice remains intact.
6
 *
7
 * Device-independent mid level screen device init routines
8
 *
9
 * These routines implement the smallest Microwindows engine level
10
 * interface to the screen driver.  By setting the NOFONTSORCLIPPING
11
 * config option, only these routines will be included, which can
12
 * be used to generate a low-level interface to the screen drivers
13
 * without dragging in any other GdXXX routines.
14
 */
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include "device.h"
18
#include "swap.h"
19
 
20
#if MSDOS | ELKS
21
#define NOSTDPAL8
22
#endif
23
 
24
/*
25
 * The following define can change depending on the window manager
26
 * usage of colors and layout of the 8bpp palette devpal8.c.
27
 * Color entries below this value won't be overwritten by user
28
 * programs or bitmap display conversion tables.
29
 */
30
#define FIRSTUSERPALENTRY       24  /* first writable pal entry over 16 color*/
31
 
32
       MWPIXELVAL gr_foreground;      /* current foreground color */
33
       MWPIXELVAL gr_background;      /* current background color */
34
       MWBOOL   gr_usebg;           /* TRUE if background drawn in pixmaps */
35
       int      gr_mode = MWMODE_COPY;      /* drawing mode */
36
/*static*/ MWPALENTRY   gr_palette[256];    /* current palette*/
37
/*static*/ int  gr_firstuserpalentry;/* first user-changable palette entry*/
38
/*static*/ int  gr_nextpalentry;    /* next available palette entry*/
39
static int      gr_pixtype;         /* screen pixel format*/
40
static long     gr_ncolors;         /* screen # colors*/
41
 
42
/*
43
 * Open low level graphics driver
44
 */
45
PSD
46
GdOpenScreen(void)
47
{
48
        PSD                     psd;
49
        MWPALENTRY *            stdpal;
50
        MWSCREENINFO            sinfo;
51
 
52
        psd = scrdev.Open(&scrdev);
53
        if (!psd)
54
                return NULL;
55
        GdGetScreenInfo(psd, &sinfo);
56
        gr_pixtype = sinfo.pixtype;
57
        gr_ncolors = sinfo.ncolors;
58
 
59
        /* assume no user changable palette entries*/
60
        gr_firstuserpalentry = (int)psd->ncolors;
61
 
62
        /* set palette according to system colors and devpalX.c*/
63
        switch((int)psd->ncolors) {
64
 
65
#if !defined(NOSTDPAL1) /* don't require stdpal1 if not needed */
66
        case 2:         /* 1bpp*/
67
        {
68
                extern MWPALENTRY       mwstdpal1[2];
69
                stdpal = mwstdpal1;
70
        }
71
        break;
72
#endif
73
 
74
#if !defined(NOSTDPAL2) /* don't require stdpal2 if not needed */
75
        case 4:         /* 2bpp*/
76
        {
77
                extern MWPALENTRY       mwstdpal2[4];
78
                stdpal = mwstdpal2;
79
        }
80
        break;
81
#endif
82
 
83
#if !defined(NOSTDPAL4)
84
        /* don't require stdpal4 if not needed */
85
        case 8:         /* 3bpp - not fully supported*/
86
        case 16:        /* 4bpp*/
87
        {
88
                extern MWPALENTRY       mwstdpal4[16];
89
                stdpal = mwstdpal4;
90
        }
91
        break;
92
#endif
93
 
94
#if !defined(NOSTDPAL8) /* don't require large stdpal8 if not needed */
95
        case 256:       /* 8bpp*/
96
        {
97
                extern MWPALENTRY       mwstdpal8[256];
98
#if xxxALPHABLEND
99
                /* don't change uniform palette if alpha blending*/
100
                gr_firstuserpalentry = 256;
101
#else
102
                /* start after last system-reserved color*/
103
                gr_firstuserpalentry = FIRSTUSERPALENTRY;
104
#endif
105
                stdpal = mwstdpal8;
106
        }
107
        break;
108
#endif  /* !defined(NOSTDPAL8)*/
109
 
110
        default:        /* truecolor*/
111
                /* no palette*/
112
                gr_firstuserpalentry = 0;
113
                stdpal = NULL;
114
        }
115
 
116
        /* reset next user palette entry, write hardware palette*/
117
        GdResetPalette();
118
        GdSetPalette(psd, 0, (int)psd->ncolors, stdpal);
119
#if xxxALPHABLEND
120
        /* one-time create alpha lookup table for 8bpp systems (takes ~1 sec)*/
121
        if(psd->ncolors == 256)
122
                init_alpha_lookup();
123
#endif
124
 
125
#if !NOFONTSORCLIPPING
126
        /* init local vars*/
127
        GdSetMode(MWMODE_COPY);
128
        GdSetForeground(GdFindColor(MWRGB(255, 255, 255)));     /* WHITE*/
129
        GdSetBackground(GdFindColor(MWRGB(0, 0, 0)));              /* BLACK*/
130
        GdSetUseBackground(TRUE);
131
        GdSetFont(GdCreateFont(psd, MWFONT_SYSTEM_VAR, 0, NULL));
132
#if DYNAMICREGIONS
133
        GdSetClipRegion(psd,
134
                GdAllocRectRegion(0, 0, psd->xvirtres, psd->yvirtres));
135
#else
136
        GdSetClipRects(psd, 0, NULL);
137
#endif /* DYNAMICREGIONS*/
138
#endif /* NOFONTSORCLIPPING*/
139
 
140
        /* fill black (actually fill to first palette entry or truecolor 0*/
141
        psd->FillRect(psd, 0, 0, psd->xvirtres-1, psd->yvirtres-1, 0);
142
        return psd;
143
}
144
 
145
/*
146
 * Close low level graphics driver
147
 */
148
void
149
GdCloseScreen(PSD psd)
150
{
151
        psd->Close(psd);
152
}
153
 
154
/* Set dynamic screen portrait mode, return new mode*/
155
int
156
GdSetPortraitMode(PSD psd, int portraitmode)
157
{
158
        /* set portrait mode if supported*/
159
        if (psd->SetPortrait)
160
                psd->SetPortrait(psd, portraitmode);
161
        return psd->portrait;
162
}
163
 
164
/*
165
 * Return about the screen.
166
 */
167
void
168
GdGetScreenInfo(PSD psd, PMWSCREENINFO psi)
169
{
170
        psd->GetScreenInfo(psd, psi);
171
        GdGetButtonInfo(&psi->buttons);
172
        GdGetModifierInfo(&psi->modifiers, NULL);
173
        GdGetCursorPos(&psi->xpos, &psi->ypos);
174
}
175
 
176
/* reset palette to empty except for system colors*/
177
void
178
GdResetPalette(void)
179
{
180
        /* note: when palette entries are changed, all
181
         * windows may need to be redrawn
182
         */
183
        gr_nextpalentry = gr_firstuserpalentry;
184
}
185
 
186
/* set the system palette section to the passed palette entries*/
187
void
188
GdSetPalette(PSD psd, int first, int count, MWPALENTRY *palette)
189
{
190
        int     i;
191
 
192
        /* no palette management needed if running truecolor*/
193
        if(psd->pixtype != MWPF_PALETTE)
194
                return;
195
 
196
        /* bounds check against # of device color entries*/
197
        if(first + count > (int)psd->ncolors)
198
                count = (int)psd->ncolors - first;
199
        if(count >= 0 && first < (int)psd->ncolors) {
200
                psd->SetPalette(psd, first, count, palette);
201
 
202
                /* copy palette for GdFind*Color*/
203
                for(i=0; i<count; ++i)
204
                        gr_palette[i+first] = palette[i];
205
        }
206
}
207
 
208
/* get system palette entries, return count*/
209
int
210
GdGetPalette(PSD psd, int first, int count, MWPALENTRY *palette)
211
{
212
        int     i;
213
 
214
        /* no palette if running truecolor*/
215
        if(psd->pixtype != MWPF_PALETTE)
216
                return 0;
217
 
218
        /* bounds check against # of device color entries*/
219
        if(first + count > (int)psd->ncolors)
220
                if( (count = (int)psd->ncolors - first) <= 0)
221
                        return 0;
222
 
223
        for(i=0; i<count; ++i)
224
                *palette++ = gr_palette[i+first];
225
 
226
        return count;
227
}
228
 
229
/*
230
 * Convert a palette-independent value to a hardware color
231
 */
232
MWPIXELVAL
233
GdFindColor(MWCOLORVAL c)
234
{
235
        /*
236
         * Handle truecolor displays.  Note that the MWF_PALINDEX
237
         * bit is ignored when running truecolor drivers.
238
         */
239
        switch(gr_pixtype) {
240
        case MWPF_TRUECOLOR0888:
241
        case MWPF_TRUECOLOR888:
242
                /* create 24 bit 8/8/8 pixel (0x00RRGGBB) from RGB colorval*/
243
                /*RGB2PIXEL888(REDVALUE(c), GREENVALUE(c), BLUEVALUE(c))*/
244
                return COLOR2PIXEL888(c);
245
 
246
        case MWPF_TRUECOLOR565:
247
                /* create 16 bit 5/6/5 format pixel from RGB colorval*/
248
                /*RGB2PIXEL565(REDVALUE(c), GREENVALUE(c), BLUEVALUE(c))*/
249
                return COLOR2PIXEL565(c);
250
 
251
        case MWPF_TRUECOLOR555:
252
                /* create 16 bit 5/5/5 format pixel from RGB colorval*/
253
                /*RGB2PIXEL555(REDVALUE(c), GREENVALUE(c), BLUEVALUE(c))*/
254
                return COLOR2PIXEL555(c);
255
 
256
        case MWPF_TRUECOLOR332:
257
                /* create 8 bit 3/3/2 format pixel from RGB colorval*/
258
                /*RGB2PIXEL332(REDVALUE(c), GREENVALUE(c), BLUEVALUE(c))*/
259
                return COLOR2PIXEL332(c);
260
 
261
        case MWPF_TRUECOLOR233:
262
                /* create 8 bit 2/3/3 format pixel from RGB colorval*/
263
                /*RGB2PIXEL332(REDVALUE(c), GREENVALUE(c), BLUEVALUE(c))*/
264
                return COLOR2PIXEL233(c);
265
        }
266
 
267
        /* case MWPF_PALETTE: must be running 1, 2, 4 or 8 bit palette*/
268
 
269
        /*
270
         * Check if color is a palette index.  Note that the index
271
         * isn't error checked against the system palette, for speed.
272
         */
273
        if(c & MWF_PALINDEX)
274
                return (c & 0xff);
275
 
276
        /* search palette for closest match*/
277
        return GdFindNearestColor(gr_palette, (int)gr_ncolors, c);
278
}
279
 
280
/*
281
 * Search a palette to find the nearest color requested.
282
 * Uses a weighted squares comparison.
283
 */
284
MWPIXELVAL
285
GdFindNearestColor(MWPALENTRY *pal, int size, MWCOLORVAL cr)
286
{
287
        MWPALENTRY *    rgb;
288
        int             r, g, b;
289
        int             R, G, B;
290
        long            diff = 0x7fffffffL;
291
        long            sq;
292
        int             best = 0;
293
 
294
        r = REDVALUE(cr);
295
        g = GREENVALUE(cr);
296
        b = BLUEVALUE(cr);
297
        for(rgb=pal; diff && rgb < &pal[size]; ++rgb) {
298
                R = rgb->r - r;
299
                G = rgb->g - g;
300
                B = rgb->b - b;
301
#if 1
302
                /* speedy linear distance method*/
303
                sq = abs(R) + abs(G) + abs(B);
304
#else
305
                /* slower distance-cubed with luminance adjustment*/
306
                /* gray is .30R + .59G + .11B*/
307
                /* = (R*77 + G*151 + B*28)/256*/
308
                sq = (long)R*R*30*30 + (long)G*G*59*59 + (long)B*B*11*11;
309
#endif
310
 
311
                if(sq < diff) {
312
                        best = rgb - pal;
313
                        if((diff = sq) == 0)
314
                                return best;
315
                }
316
        }
317
        return best;
318
}
319
 
320
#if !VXWORKS
321
#include <unistd.h>
322
#include <fcntl.h>
323
/*
324
 * Create .bmp file from framebuffer data
325
 *
326
 * 1, 4, 8, 16, 24 and 32 bpp supported
327
 */
328
#define BI_RGB          0L
329
#define BI_RLE8         1L
330
#define BI_RLE4         2L
331
#define BI_BITFIELDS    3L
332
 
333
typedef unsigned char   BYTE;
334
typedef unsigned short  WORD;
335
typedef unsigned long   DWORD;
336
typedef long            LONG;
337
 
338
#pragma pack(1)
339
/* windows style bmp*/
340
typedef struct {
341
        /* BITMAPFILEHEADER*/
342
        BYTE    bfType[2];
343
        DWORD   bfSize;
344
        WORD    bfReserved1;
345
        WORD    bfReserved2;
346
        DWORD   bfOffBits;
347
        /* BITMAPINFOHEADER*/
348
        DWORD   BiSize;
349
        LONG    BiWidth;
350
        LONG    BiHeight;
351
        WORD    BiPlanes;
352
        WORD    BiBitCount;
353
        DWORD   BiCompression;
354
        DWORD   BiSizeImage;
355
        LONG    BiXpelsPerMeter;
356
        LONG    BiYpelsPerMeter;
357
        DWORD   BiClrUsed;
358
        DWORD   BiClrImportant;
359
} BMPHEAD;
360
#pragma pack()
361
 
362
/* r/g/b masks for non-palette bitmaps*/
363
#define RMASK332        0xe0
364
#define GMASK332        0x1c
365
#define BMASK332        0x03
366
#define RMASK233        0x07
367
#define GMASK233        0x38
368
#define BMASK233        0xC0
369
#define RMASK555        0x7c00
370
#define GMASK555        0x03e0
371
#define BMASK555        0x001f
372
#define RMASK565        0xf800
373
#define GMASK565        0x07e0
374
#define BMASK565        0x001f
375
#define RMASK888        0xff0000
376
#define GMASK888        0x00ff00
377
#define BMASK888        0x0000ff
378
 
379
#if defined(HAVE_FILEIO)
380
static void
381
putsw(unsigned long dw, FILE *ofp)
382
{
383
        /* little-endian storage of shortword*/
384
        putc((unsigned char)dw, ofp);
385
        dw >>= 8;
386
        putc((unsigned char)dw, ofp);
387
}
388
 
389
static void
390
putdw(unsigned long dw, FILE *ofp)
391
{
392
        /* little-endian storage of longword*/
393
        putc((unsigned char)dw, ofp);
394
        dw >>= 8;
395
        putc((unsigned char)dw, ofp);
396
        dw >>= 8;
397
        putc((unsigned char)dw, ofp);
398
        dw >>= 8;
399
        putc((unsigned char)dw, ofp);
400
}
401
#endif /* HAVE_FILEIO*/
402
 
403
/* create .bmp file from framebuffer data*/
404
int
405
GdCaptureScreen(char *path)
406
{
407
#if defined(HAVE_FILEIO)
408
        int     ifd, i, j;
409
        FILE *  ofp;
410
        int     cx, cy, extra, bpp, bytespp, ncolors, sizecolortable;
411
        unsigned long rmask, gmask, bmask;
412
        unsigned char *cptr;
413
        unsigned short *sptr;
414
        unsigned long *lptr;
415
        BMPHEAD bmp;
416
        unsigned char buf[2048*4];
417
 
418
        ofp = fopen(path, "wb");
419
        if (!ofp)
420
                return 1;
421
        ifd = open("/dev/fb0", 0);
422
 
423
        cx = scrdev.xvirtres;
424
        cy = scrdev.yvirtres;
425
        bpp = scrdev.bpp;
426
        bytespp = (bpp+7)/8;
427
 
428
        /* dword right padded*/
429
        extra = (cx*bytespp) & 3;
430
        if (extra)
431
                extra = 4 - extra;
432
        ncolors = (bpp <= 8)? (1<<bpp): 0;
433
        /* color table is either palette or 3 longword r/g/b masks*/
434
        sizecolortable = ncolors? ncolors*4: 3*4;
435
        if (bpp == 24)
436
                sizecolortable = 0;      /* special case 24bpp has no table*/
437
 
438
        /* fill out bmp header*/
439
        memset(&bmp, 0, sizeof(bmp));
440
        bmp.bfType[0] = 'B';
441
        bmp.bfType[1] = 'M';
442
        bmp.bfSize = dwswap(sizeof(bmp) + sizecolortable + (long)(cx+extra)*cy*bytespp);
443
        bmp.bfOffBits = dwswap(sizeof(bmp) + sizecolortable);
444
        bmp.BiSize = dwswap(40);
445
        bmp.BiWidth = dwswap(cx);
446
        bmp.BiHeight = dwswap(cy);
447
        bmp.BiPlanes = wswap(1);
448
        bmp.BiBitCount = wswap(bpp);
449
        bmp.BiCompression = dwswap((bpp==16 || bpp==32)? BI_BITFIELDS: BI_RGB);
450
        bmp.BiSizeImage = dwswap((long)(cx+extra)*cy*bytespp);
451
        bmp.BiClrUsed = dwswap((bpp <= 8)? ncolors: 0);
452
        /*bmp.BiClrImportant = 0;*/
453
 
454
        /* write header*/
455
        fwrite(&bmp, sizeof(bmp), 1, ofp);
456
 
457
        /* write colortable*/
458
        if (sizecolortable) {
459
                if(bpp <= 8) {
460
                        /* write palette*/
461
                        for(i=0; i<ncolors; i++) {
462
                                putc(gr_palette[i].b, ofp);
463
                                putc(gr_palette[i].g, ofp);
464
                                putc(gr_palette[i].r, ofp);
465
                                putc(0, ofp);
466
                        }
467
                } else {
468
                        /* write 3 r/g/b masks*/
469
                        switch (gr_pixtype) {
470
                        case MWPF_TRUECOLOR0888:
471
                        default:
472
                                rmask = RMASK888;
473
                                gmask = GMASK888;
474
                                bmask = BMASK888;
475
                                break;
476
                        case MWPF_TRUECOLOR565:
477
                                rmask = RMASK565;
478
                                gmask = GMASK565;
479
                                bmask = BMASK565;
480
                                break;
481
                        case MWPF_TRUECOLOR555:
482
                                rmask = RMASK555;
483
                                gmask = GMASK555;
484
                                bmask = BMASK555;
485
                                break;
486
                        case MWPF_TRUECOLOR332:
487
                                rmask = RMASK332;
488
                                gmask = GMASK332;
489
                                bmask = BMASK332;
490
                                break;
491
                        case MWPF_TRUECOLOR233:
492
                                rmask = RMASK233;
493
                                gmask = GMASK233;
494
                                bmask = BMASK233;
495
                                break;
496
                        }
497
                        putdw(rmask, ofp);
498
                        putdw(gmask, ofp);
499
                        putdw(bmask, ofp);
500
                }
501
        }
502
 
503
        /* write image data, upside down ;)*/
504
        for(i=cy-1; i>=0; --i) {
505
                long base = sizeof(bmp) + sizecolortable + (long)i*cx*bytespp;
506
                fseek(ofp, base, SEEK_SET);
507
                read(ifd, buf, cx*bytespp);
508
                switch (bpp) {
509
                case 32:
510
                        lptr = (unsigned long *)buf;
511
                        for(j=0; j<cx; ++j)
512
                                putdw(*lptr++, ofp);
513
                        break;
514
                case 24:
515
                        cptr = (unsigned char *)buf;
516
                        for(j=0; j<cx; ++j) {
517
                                putc(*cptr++, ofp);
518
                                putc(*cptr++, ofp);
519
                                putc(*cptr++, ofp);
520
                        }
521
                        break;
522
                case 16:
523
                        sptr = (unsigned short *)buf;
524
                        for(j=0; j<cx; ++j)
525
                                putsw(*sptr++, ofp);
526
                        break;
527
                default:
528
                        cptr = (unsigned char *)buf;
529
                        for(j=0; j<cx; ++j)
530
                                putc(*cptr++, ofp);
531
                        break;
532
                }
533
                for(j=0; j<extra; ++j)
534
                        putc(0, ofp);            /* DWORD pad each line*/
535
        }
536
 
537
        fclose(ofp);
538
        close(ifd);
539
#endif /* HAVE_FILEIO*/
540
        return 0;
541
}
542
#endif /* !VXWORKS*/

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.