-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
870 lines (776 loc) · 25.6 KB
/
display.c
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/*
* Display system for QEmacs
*
* Copyright (c) 2000 Fabrice Bellard.
* Copyright (c) 2002-2017 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
static QEDisplay *first_dpy;
/* dummy display driver for initialization time */
static int dummy_dpy_init(QEditScreen *s, qe__unused__ int w, qe__unused__ int h)
{
s->charset = &charset_8859_1;
return 0;
}
static void dummy_dpy_close(qe__unused__ QEditScreen *s)
{
}
static void dummy_dpy_flush(qe__unused__ QEditScreen *s)
{
}
static int dummy_dpy_is_user_input_pending(qe__unused__ QEditScreen *s)
{
return 0;
}
static void dummy_dpy_fill_rectangle(qe__unused__ QEditScreen *s,
qe__unused__ int x1, qe__unused__ int y1,
qe__unused__ int w, qe__unused__ int h,
qe__unused__ QEColor color)
{
}
static void dummy_dpy_xor_rectangle(qe__unused__ QEditScreen *s,
qe__unused__ int x1, qe__unused__ int y1,
qe__unused__ int w, qe__unused__ int h,
qe__unused__ QEColor color)
{
}
static QEFont *dummy_dpy_open_font(qe__unused__ QEditScreen *s,
qe__unused__ int style, qe__unused__ int size)
{
return NULL;
}
static void dummy_dpy_close_font(qe__unused__ QEditScreen *s,
qe__unused__ QEFont **fontp)
{
}
static void dummy_dpy_text_metrics(qe__unused__ QEditScreen *s,
qe__unused__ QEFont *font,
QECharMetrics *metrics,
qe__unused__ const unsigned int *str,
qe__unused__ int len)
{
metrics->font_ascent = 1;
metrics->font_descent = 0;
metrics->width = len;
}
static void dummy_dpy_draw_text(qe__unused__ QEditScreen *s,
qe__unused__ QEFont *font,
qe__unused__ int x, qe__unused__ int y,
qe__unused__ const unsigned int *str,
qe__unused__ int len,
qe__unused__ QEColor color)
{
}
static void dummy_dpy_set_clip(qe__unused__ QEditScreen *s,
qe__unused__ int x, qe__unused__ int y,
qe__unused__ int w, qe__unused__ int h)
{
}
static QEDisplay const dummy_dpy = {
"dummy", 1, 1,
NULL, /* dpy_probe */
dummy_dpy_init,
dummy_dpy_close,
dummy_dpy_flush,
dummy_dpy_is_user_input_pending,
dummy_dpy_fill_rectangle,
dummy_dpy_xor_rectangle,
dummy_dpy_open_font,
dummy_dpy_close_font,
dummy_dpy_text_metrics,
dummy_dpy_draw_text,
dummy_dpy_set_clip,
NULL, /* dpy_selection_activate */
NULL, /* dpy_selection_request */
NULL, /* dpy_invalidate */
NULL, /* dpy_cursor_at */
NULL, /* dpy_bmp_alloc */
NULL, /* dpy_bmp_free */
NULL, /* dpy_bmp_draw */
NULL, /* dpy_bmp_lock */
NULL, /* dpy_bmp_unlock */
NULL, /* dpy_draw_picture */
NULL, /* dpy_full_screen */
NULL, /* dpy_describe */
NULL, /* next */
};
/*----------------*/
void fill_rectangle(QEditScreen *s,
int x1, int y1, int w, int h, QEColor color)
{
int x2 = x1 + w;
int y2 = y1 + h;
/* quick clip rejection */
if (x2 <= s->clip_x1 || y2 <= s->clip_y1 ||
x1 >= s->clip_x2 || y1 >= s->clip_y2)
return;
/* region update */
if (x2 > s->clip_x2)
x2 = s->clip_x2;
if (y2 > s->clip_y2)
y2 = s->clip_y2;
if (x1 < s->clip_x1)
x1 = s->clip_x1;
if (y1 < s->clip_y1)
y1 = s->clip_y1;
/* rejection if zero size */
if (x1 >= x2 || y1 >= y2)
return;
s->dpy.dpy_fill_rectangle(s, x1, y1, x2 - x1, y2 - y1, color);
}
void xor_rectangle(QEditScreen *s,
int x, int y, int w, int h, QEColor color)
{
/* intersect with clip region */
int x1 = max(s->clip_x1, x);
int y1 = max(s->clip_y1, y);
int x2 = min(s->clip_x2, x1 + w);
int y2 = min(s->clip_y2, y1 + h);
if (x1 < x2 && y1 < y2) {
s->dpy.dpy_xor_rectangle(s, x1, y1, x2 - x1, y2 - y1, color);
}
}
/* set the clip rectangle (and does not clip by the previous one) */
void set_clip_rectangle(QEditScreen *s, CSSRect *r)
{
int x1, y1, x2, y2;
x1 = r->x1;
y1 = r->y1;
x2 = r->x2;
y2 = r->y2;
if (x2 > s->width)
x2 = s->width;
if (y2 > s->height)
y2 = s->height;
if (x1 < 0)
x1 = 0;
if (y1 < 0)
y1 = 0;
s->clip_x1 = x1;
s->clip_y1 = y1;
s->clip_x2 = x2;
s->clip_y2 = y2;
s->dpy.dpy_set_clip(s, x1, y1, x2 - x1, y2 - y1);
}
void push_clip_rectangle(QEditScreen *s, CSSRect *r0, CSSRect *r)
{
int x1, y1, x2, y2;
/* save old rectangle */
r0->x1 = s->clip_x1;
r0->y1 = s->clip_y1;
r0->x2 = s->clip_x2;
r0->y2 = s->clip_y2;
/* load and clip new rectangle against the current one */
x1 = r->x1;
y1 = r->y1;
x2 = r->x2;
y2 = r->y2;
if (x2 > s->clip_x2)
x2 = s->clip_x2;
if (y2 > s->clip_y2)
y2 = s->clip_y2;
if (x1 < s->clip_x1)
x1 = s->clip_x1;
if (y1 < s->clip_y1)
y1 = s->clip_y1;
/* set new rectangle */
s->clip_x1 = x1;
s->clip_y1 = y1;
s->clip_x2 = x2;
s->clip_y2 = y2;
s->dpy.dpy_set_clip(s, x1, y1, x2 - x1, y2 - y1);
}
int qe_register_display(QEDisplay *dpy)
{
QEDisplay **pp;
pp = &first_dpy;
while (*pp != NULL)
pp = &(*pp)->next;
*pp = dpy;
dpy->next = NULL;
return 0;
}
QEDisplay *probe_display(void)
{
QEDisplay *p, *dpy;
int probe_max, probe;
p = first_dpy;
dpy = NULL;
probe_max = 0;
while (p != NULL) {
probe = p->dpy_probe ? p->dpy_probe() : 0;
if (probe >= probe_max) {
probe_max = probe;
dpy = p;
}
p = p->next;
}
return dpy;
}
int screen_init(QEditScreen *s, QEDisplay *dpy, int w, int h)
{
s->dpy = dpy ? *dpy : dummy_dpy;
return s->dpy.dpy_init(s, w, h);
}
/* simple font cache */
#define FONT_CACHE_SIZE 32
static QEFont dummy_font;
static QEFont *font_cache[FONT_CACHE_SIZE];
static int font_cache_timestamp = 0;
void free_font_cache(QEditScreen *s)
{
int i;
for (i = 0; i < FONT_CACHE_SIZE; i++) {
close_font(s, &font_cache[i]);
}
}
QEFont *select_font(QEditScreen *s, int style, int size)
{
QEFont *fc;
int i, min_ts, min_index;
min_ts = INT_MAX;
min_index = -1;
for (i = 0; i < FONT_CACHE_SIZE; i++) {
fc = font_cache[i];
if (fc) {
if (fc->style == style && fc->size == size) {
goto found;
}
if (fc->timestamp < min_ts && fc->refcount <= 0) {
min_ts = fc->timestamp;
min_index = i;
}
} else {
min_ts = 0;
min_index = i;
}
}
/* not found : open new font */
if (min_index < 0) {
put_error(NULL, "Font cache full");
goto fail;
}
if (font_cache[min_index]) {
close_font(s, &font_cache[min_index]);
}
fc = open_font(s, style, size);
if (!fc) {
if (style & QE_FONT_FAMILY_FALLBACK_MASK)
return NULL;
put_error(NULL, "open_font: cannot open style=%X size=%d",
style, size);
goto fail;
}
fc->style = style;
fc->size = size;
font_cache[min_index] = fc;
found:
fc->timestamp = font_cache_timestamp;
font_cache_timestamp++;
fc->refcount++;
return fc;
fail:
/* select_font never returns NULL */
/* CG: This is bogus, dummy font is not device compatible? */
fc = &dummy_font;
fc->system_font = 1;
return fc;
}
QEBitmap *bmp_alloc(QEditScreen *s, int width, int height, int flags)
{
QEBitmap *b;
if (!s->dpy.dpy_bmp_alloc)
return NULL;
b = qe_mallocz(QEBitmap);
if (!b)
return NULL;
b->width = width;
b->height = height;
b->flags = flags;
if (s->dpy.dpy_bmp_alloc(s, b) < 0) {
qe_free(&b);
return NULL;
}
return b;
}
void bmp_free(QEditScreen *s, QEBitmap **bp)
{
if (*bp) {
s->dpy.dpy_bmp_free(s, *bp);
qe_free(bp);
}
}
#if 0
/* bitmap cache */
typedef struct QECachedBitmap {
QEBitmap *bitmap;
char url[1];
int refcount;
struct QECachedBitmap *next;
} QECachedBitmap;
/* dst_w or dst_h can be zero if unspecified */
/* XXX: add scaling for printing */
QECachedBitmap *cbmp_open(QEditScreen *s,
const char *url,
int dst_w, int dst_h)
{
}
/* return the DESTINATION size of the bitmap. return non zero if
unknown size */
int cbmp_get_size(QECachedBitmap *bitmap, int *w_ptr, int *h_ptr)
{
}
void cbmp_draw(QEditScreen *s, QECachedBitmap *bitmap,
int dst_x, int dst_y, int dst_w, int dst_h,
int offset_x, int offset_y, int flags)
{
}
void cbmp_close(QEditScreen *cbmp)
{
}
#endif
/* draw only the border of a rectangle */
void fill_border(EditState *s, int x, int y, int w, int h, int color)
{
int x0, y0, w0, h0, w1, w2, h1, h2;
/* fill the background */
x0 = s->xleft;
y0 = s->ytop;
w0 = s->width;
h0 = s->height;
w1 = max(0, x);
w2 = max(0, w0 - (x + w));
h1 = max(0, y);
h2 = max(0, h0 - (y + h));
if (w1) fill_rectangle(s->screen, x0, y0, w1, h0, color);
if (w2) fill_rectangle(s->screen, x0 + w0 - w2, y0, w2, h0, color);
if (h1) fill_rectangle(s->screen, x0 + w1, y0, w0 - w1 - w2, h1, color);
if (h2) fill_rectangle(s->screen, x0 + w1, y0 + h0 - h2, w0 - w1 - w2, h2, color);
}
/*---------------- QEPicture handling functions ----------------*/
int qe_draw_picture(QEditScreen *s, int dst_x, int dst_y, int dst_w, int dst_h,
const QEPicture *ip,
int src_x, int src_y, int src_w, int src_h,
int flags, QEColor col)
{
#ifndef CONFIG_TINY
int x1 = dst_x;
int y1 = dst_y;
int x2 = x1 + dst_w;
int y2 = y1 + dst_h;
int w1, h1;
/* quick clip rejection */
if (x2 <= s->clip_x1 || y2 <= s->clip_y1 ||
x1 >= s->clip_x2 || y1 >= s->clip_y2)
return 1;
/* region update */
if (x2 > s->clip_x2)
x2 = s->clip_x2;
if (y2 > s->clip_y2)
y2 = s->clip_y2;
if (x1 < s->clip_x1)
x1 = s->clip_x1;
if (y1 < s->clip_y1)
y1 = s->clip_y1;
/* rejection if zero size */
w1 = x2 - x1;
h1 = y2 - y1;
if (w1 < 0 || h1 < 0)
return 1;
if (w1 != dst_w) {
int dx = min((x1 - dst_x) * src_w / dst_w, src_w - 1);
src_x += dx;
src_w = clamp(src_w * w1 / dst_w, 1, src_w - dx);
}
if (h1 != dst_h) {
int dy = min((y1 - dst_y) * src_h / dst_h, src_h - 1);
src_y += dy;
src_h = clamp(src_h * h1 / dst_h, 1, src_h - dy);
}
if (s->dpy.dpy_draw_picture &&
!s->dpy.dpy_draw_picture(s, x1, y1, w1, h1,
ip, src_x, src_y, src_w, src_h, flags)) {
return 0;
} else {
if (col != COLOR_TRANSPARENT) {
/* if image cannot be displayed, just draw a rectangle */
fill_rectangle(s, x1, y1, w1, h1, col);
}
return 2;
}
#else
return 1;
#endif
}
#ifndef CONFIG_TINY
static int qe_picture_format_bits(QEBitmapFormat format) {
switch (format) {
case QEBITMAP_FORMAT_1BIT:
return 1;
case QEBITMAP_FORMAT_4BIT:
return 4;
case QEBITMAP_FORMAT_8BIT:
return 8;
case QEBITMAP_FORMAT_RGB565:
case QEBITMAP_FORMAT_RGB555:
return 16;
case QEBITMAP_FORMAT_RGB24:
case QEBITMAP_FORMAT_BGR24:
return 24;
case QEBITMAP_FORMAT_RGBA32:
case QEBITMAP_FORMAT_BGRA32:
return 32;
case QEBITMAP_FORMAT_YUV420P:
/* unsupported for now */
default:
return 0;
}
}
QEPicture *qe_create_picture(int width, int height,
QEBitmapFormat format, int flags)
{
QEPicture *ip;
unsigned int bits;
bits = qe_picture_format_bits(format);
if (bits <= 0)
return NULL;
ip = qe_mallocz(QEPicture);
if (ip) {
/* align pixmap lines on 64 bit boundaries */
unsigned int wb = width * bits + 63 / 64 * 8;
ip->width = width;
ip->height = height;
ip->format = format;
ip->data[0] = qe_malloc_array(unsigned char, wb * height);
ip->linesize[0] = wb;
}
return ip;
}
void qe_free_picture(QEPicture **ipp) {
if (*ipp) {
qe_free(&(*ipp)->data[0]);
qe_free(ipp);
}
}
int qe_picture_set_palette(QEPicture *ip, int mode,
unsigned char *p, int count, int tcolor)
{
int i, r, g, b, incr;
if (!ip)
return 1;
if (mode == 0)
mode = QE_PAL_QECOLOR;
incr = mode & 15;
b = (mode >> 4) & 15;
g = (mode >> 8) & 15;
r = (mode >> 12) & 15;
ip->tcolor = tcolor;
if (ip->palette_size != 256) {
qe_free(&ip->palette);
ip->palette_size = 256;
ip->palette = qe_malloc_array(QEColor, 256);
if (ip->palette == NULL) {
ip->palette_size = 0;
return -1;
}
/* Default colors to standard palette */
memcpy(ip->palette, xterm_colors, 256 * sizeof(*ip->palette));
}
count = min(count, 256);
for (i = 0; i < count; i++) {
ip->palette[i] = QERGB(p[r], p[g], p[b]);
p += incr;
}
return 0;
}
static int qe_picture_scale(QEPicture *to, int dst_x, int dst_y, int dst_w, int dst_h,
const QEPicture *from,
int src_x, int src_y, int src_w, int src_h, int flags)
{
if (from->format != to->format)
return 1;
if (from->format != QEBITMAP_FORMAT_RGBA32
&& from->format != QEBITMAP_FORMAT_BGRA32)
return 1;
{ // prevent -Wdeclaration-after-statement warning
#if 0
/* Bilinear interpolation */
uint32_t w = src_w, h = src_h, w2 = dst_w, h2 = dst_h;
uint32_t x_ratio = ((w - 1) << 16) / w2;
uint32_t y_ratio = ((h - 1) << 16) / h2;
uint32_t pitch = from->linesize[0];
uint32_t pitch2 = to->linesize[0];
uint32_t y = 0;
for (uint32_t i = 0; i < h2; i++, y += y_ratio) {
uint32_t yr = y >> 16;
uint32_t y_diff = y & 0xFFFF;
const uint8_t *src = from->data[0] + (src_y + yr) * pitch + src_x * 4;
uint8_t *dst = to->data[0] + (dst_y + i) * pitch2 + dst_x * 4;
uint32_t x = 0;
for (uint32_t j = 0; j < w2; j++, x += x_ratio) {
uint32_t xr = x >> 16;
uint32_t x_diff = x & 0xFFFF;
uint32_t m0 = (0x10000 - x_diff) * (0x10000 - y_diff);
uint32_t m1 = x_diff * (0x10000 - y_diff);
uint32_t m2 = (0x10000 - x_diff) * y_diff;
uint32_t m3 = x_diff * y_diff;
for (int k = 0; k < 3; k++) {
uint32_t v = (src[xr * 4 + k + 0] * (int64_t)m0 +
src[xr * 4 + k + 4] * (int64_t)m1 +
src[xr * 4 + k + pitch + 0] * (int64_t)m2 +
src[xr * 4 + k + pitch + 4] * (int64_t)m3) >> 32;
dst[j * 4 + k] = (uint8_t)v;
}
}
}
#else
int x, y, sx, sy, sx0, sy0, dx, dy, width = dst_w, height = dst_h;
/* Nearest-neighbor interpolation */
dx = dy = sx0 = sy0 = 0;
if (dst_w > 1) {
if (dst_w > src_w) {
dx = src_w * 0x10000 / dst_w;
} else {
dx = (src_w - 1) * 0x10000 / (dst_w - 1);
sx0 = 0x8000;
}
}
if (dst_h > 1) {
if (dst_h > src_h) {
dy = src_h * 0x10000 / dst_h;
} else {
dy = (src_h - 1) * 0x10000 / (dst_h - 1);
sy0 = 0x8000;
}
}
for (y = 0, sy = sy0; y < height; y++, sy += dy) {
const uint32_t *src = (const uint32_t *)(void *)(from->data[0] +
(src_y + (sy >> 16)) * from->linesize[0]) + src_x;
uint32_t *dst = (uint32_t *)(void *)(to->data[0] + (dst_y + y) * to->linesize[0]) + dst_x;
for (x = 0, sx = sx0; x < width; x++, sx += dx) {
/* XXX: No filtering */
dst[x + 0] = src[sx >> 16];
}
}
#endif
}
return 0;
}
int qe_picture_copy(QEPicture *to, int dst_x, int dst_y, int dst_w, int dst_h,
const QEPicture *from,
int src_x, int src_y, int src_w, int src_h, int flags)
{
const uint32_t *palette;
int x, y, width = src_w, height = src_h;
int res = 0;
if (src_w != dst_w || src_h != dst_h) {
/* Generic scaling */
QEPicture *ip1 = NULL;
if (from->format != QEBITMAP_FORMAT_RGBA32) {
ip1 = qe_create_picture(src_w, src_h, QEBITMAP_FORMAT_RGBA32, 0);
if (!ip1)
return -1;
res = qe_picture_copy(ip1, 0, 0, src_w, src_h,
from, src_x, src_y, src_w, src_h, 0);
from = ip1;
src_x = src_y = 0;
}
if (!res) {
res = qe_picture_scale(to, dst_x, dst_y, dst_w, dst_h,
from, src_x, src_y, src_w, src_h, flags);
}
qe_free_picture(&ip1);
return res;
}
if (from->format == QEBITMAP_FORMAT_8BIT && to->format == QEBITMAP_FORMAT_RGBA32) {
palette = from->palette;
if (palette == NULL) {
palette = xterm_colors;
}
for (y = 0; y < height; y++) {
const unsigned char *src = from->data[0] + (src_y + y) * from->linesize[0] + src_x;
uint32_t *dst = (uint32_t *)(void *)(to->data[0] + (dst_y + y) * to->linesize[0]) + dst_x;
int w4 = width & ~3;
for (x = 0; x < w4; x += 4) {
dst[x + 0] = palette[src[x + 0]];
dst[x + 1] = palette[src[x + 1]];
dst[x + 2] = palette[src[x + 2]];
dst[x + 3] = palette[src[x + 3]];
}
for (; x < width; x++) {
dst[x] = palette[src[x]];
}
}
return 0;
}
if (from->format == QEBITMAP_FORMAT_4BIT && to->format == QEBITMAP_FORMAT_RGBA32) {
palette = from->palette;
if (palette == NULL) {
palette = xterm_colors;
}
for (y = 0; y < height; y++) {
const unsigned char *src = from->data[0] + (src_y + y) * from->linesize[0] + (src_x >> 1);
uint32_t *dst = (uint32_t *)(void *)(to->data[0] + (dst_y + y) * to->linesize[0]) + dst_x;
int shift, w4;
x = 0;
if (src_x & 1) {
/* Convert incomplete left block */
dst[x] = palette[*src++ & 15];
x++;
}
/* Convert middle block */
w4 = x + ((width - x) & ~3);
for (x = 0; x < w4; x += 4, src += 2) {
dst[x + 0] = palette[src[0] >> 4];
dst[x + 1] = palette[src[0] & 15];
dst[x + 2] = palette[src[1] >> 4];
dst[x + 3] = palette[src[1] & 15];
}
/* Convert right block */
shift = 4;
for (; x < width; x++) {
dst[x] = palette[(src[0] >> shift) & 15];
src += (shift ^= 4) >> 2;
}
}
return 0;
}
if (from->format == QEBITMAP_FORMAT_1BIT && to->format == QEBITMAP_FORMAT_RGBA32) {
QEColor bw[2] = { QERGB(0, 0, 0), QERGB(0xff, 0xff, 0xff) };
palette = from->palette;
if (palette == NULL) {
palette = bw;
}
for (y = 0; y < height; y++) {
const unsigned char *src = from->data[0] + (src_y + y) * from->linesize[0] + (src_x >> 3);
uint32_t *dst = (uint32_t *)(void *)(to->data[0] + (dst_y + y) * to->linesize[0]) + dst_x;
int bits, shift, w8;
x = 0;
if (src_x & 7) {
/* Convert incomplete left block */
bits = *src++;
shift = 8 - (src_x & 7);
for (; x < width && shift != 0; x++) {
dst[x] = palette[(bits >> --shift) & 1];
}
}
/* Convert middle block */
w8 = x + ((width - x) & ~7);
for (; x < w8; x += 8) {
bits = *src++;
dst[x + 0] = palette[(bits >> 7) & 1];
dst[x + 1] = palette[(bits >> 6) & 1];
dst[x + 2] = palette[(bits >> 5) & 1];
dst[x + 3] = palette[(bits >> 4) & 1];
dst[x + 4] = palette[(bits >> 3) & 1];
dst[x + 5] = palette[(bits >> 2) & 1];
dst[x + 6] = palette[(bits >> 1) & 1];
dst[x + 7] = palette[(bits >> 0) & 1];
}
if (x < width) {
/* Convert incomplete right block */
bits = *src;
shift = 8;
for (; x < width; x++) {
dst[x] = palette[(bits >> --shift) & 1];
}
}
}
return 0;
}
if (from->format == QEBITMAP_FORMAT_RGB565 && to->format == QEBITMAP_FORMAT_RGBA32) {
/* XXX: deal with endianness */
for (y = 0; y < height; y++) {
const uint16_t *src = (const uint16_t *)(void *)(from->data[0] + (src_y + y) * from->linesize[0]) + src_x;
uint32_t *dst = (uint32_t *)(void *)(to->data[0] + (dst_y + y) * to->linesize[0]) + dst_x;
for (x = 0; x < width; x++, src++) {
unsigned int r = (src[0] >> 8) & 0xF8;
unsigned int g = (src[0] >> 3) & 0xFC;
unsigned int b = (src[0] << 3) & 0xF8;
dst[x] = QERGB(r | (r >> 5), g | (g >> 6), b | (b >> 5));
}
}
return 0;
}
if (from->format == QEBITMAP_FORMAT_RGB555 && to->format == QEBITMAP_FORMAT_RGBA32) {
/* XXX: deal with endianness */
for (y = 0; y < height; y++) {
const uint16_t *src = (const uint16_t *)(void *)(from->data[0] + (src_y + y) * from->linesize[0]) + src_x;
uint32_t *dst = (uint32_t *)(void *)(to->data[0] + (dst_y + y) * to->linesize[0]) + dst_x;
for (x = 0; x < width; x++, src++) {
unsigned int r = (src[0] >> 7) & 0xF8;
unsigned int g = (src[0] >> 2) & 0xF8;
unsigned int b = (src[0] << 3) & 0xF8;
dst[x] = QERGB(r | (r >> 5), g | (g >> 5), b | (b >> 5));
}
}
return 0;
}
if (from->format == QEBITMAP_FORMAT_RGB24 && to->format == QEBITMAP_FORMAT_RGBA32) {
for (y = 0; y < height; y++) {
const unsigned char *src = from->data[0] + (src_y + y) * from->linesize[0] + src_x * 3;
uint32_t *dst = (uint32_t *)(void *)(to->data[0] + (dst_y + y) * to->linesize[0]) + dst_x;
int w4 = width & ~3;
for (x = 0; x < w4; x += 4, src += 12) {
dst[x + 0] = QERGB(src[0], src[1], src[2]);
dst[x + 1] = QERGB(src[3], src[4], src[5]);
dst[x + 2] = QERGB(src[6], src[7], src[8]);
dst[x + 3] = QERGB(src[9], src[10], src[11]);
}
for (; x < width; x++, src += 3) {
dst[x] = QERGB(src[0], src[1], src[2]);
}
}
return 0;
}
if (from->format == QEBITMAP_FORMAT_BGR24 && to->format == QEBITMAP_FORMAT_RGBA32) {
for (y = 0; y < height; y++) {
const unsigned char *src = from->data[0] + (src_y + y) * from->linesize[0] + src_x * 3;
uint32_t *dst = (uint32_t *)(void *)(to->data[0] + (dst_y + y) * to->linesize[0]) + dst_x;
int w4 = width & ~3;
for (x = 0; x < w4; x += 4, src += 12) {
dst[x + 0] = QERGB(src[2], src[1], src[0]);
dst[x + 1] = QERGB(src[5], src[4], src[3]);
dst[x + 2] = QERGB(src[8], src[7], src[6]);
dst[x + 3] = QERGB(src[11], src[10], src[9]);
}
for (; x < width; x++, src += 3) {
dst[x] = QERGB(src[2], src[1], src[0]);
}
}
return 0;
}
if (from->format == QEBITMAP_FORMAT_BGRA32 && to->format == QEBITMAP_FORMAT_RGBA32) {
for (y = 0; y < height; y++) {
const unsigned char *src = from->data[0] + (src_y + y) * from->linesize[0] + src_x * 4;
uint32_t *dst = (uint32_t *)(void *)(to->data[0] + (dst_y + y) * to->linesize[0]) + dst_x;
int w4 = width & ~3;
for (x = 0; x < w4; x += 4, src += 16) {
dst[x + 0] = QERGB(src[0], src[1], src[2]);
dst[x + 1] = QERGB(src[4], src[5], src[6]);
dst[x + 2] = QERGB(src[8], src[9], src[10]);
dst[x + 3] = QERGB(src[12], src[13], src[14]);
}
for (; x < width; x++, src += 4) {
dst[x] = QERGB(src[0], src[1], src[2]);
}
}
return 0;
}
return 1;
}
#endif /* !CONFIG_TINY */