aboutsummaryrefslogtreecommitdiffstats
path: root/w3mimg/win/win_w3mimg.cpp
blob: 1864fcb5e13d8cf40a967f49a2a4798e7f1b961a (plain) (blame)
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
/* $Id: win_w3mimg.cpp,v 1.1 2010/12/21 10:13:55 htrb Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
#include <assert.h>
#include <locale.h>

#include <new>
#include <algorithm>

#include "w3mimg/w3mimg.h"
#include <windows.h>
#include <gdiplus.h>
#include <unistd.h>
#include <sys/cygwin.h>
/* GDI+ can handle BMP, GIF, JPEG, PNG and TIFF by itself. */

#define OFFSET_X	2
#define OFFSET_Y	2
#define DEBUG
#define USE_GDIP_CACHED_BITMAP

#ifdef DEBUG
#define THROW_NONE throw()
#else
#define THROW_NONE
#endif

struct win_info {
    HWND window;
    Gdiplus::ARGB background_pixel;
    ULONG_PTR gdiplus_token;
    FILE *logfile;
};

struct window_list {
    HWND *wnd;
    size_t nwnd;
    size_t capacity;
};

#ifdef USE_GDIP_CACHED_BITMAP
typedef Gdiplus::CachedBitmap *cache_handle;
#else
typedef HBITMAP cache_handle;
#endif
class win_image {
private:
    win_image(const win_image &); // decl only
    win_image &operator=(const win_image &); // decl only

    Gdiplus::Bitmap *gpbitmap;
    unsigned int nframe;
    unsigned int current;
    unsigned long tick;
    unsigned long loopcount; // zero = infinite
    unsigned long *delay; // unit: millisecond
    cache_handle *cache;

public:
    win_image() THROW_NONE;
    ~win_image() THROW_NONE;
    int load(w3mimg_op *wop, Gdiplus::Bitmap **p_gpbitmap,
	    int *wreturn, int *hreturn) THROW_NONE;
    int show(w3mimg_op *wop, int sx, int sy, int sw, int sh, int x, int y) THROW_NONE;
    int animate(w3mimg_op *wop) THROW_NONE;
};

static int win_init(w3mimg_op * wop) THROW_NONE;
static int win_finish(w3mimg_op * wop) THROW_NONE;
static int win_active(w3mimg_op * wop) THROW_NONE;
static void win_set_background(w3mimg_op * wop, char *background) THROW_NONE;
static void win_sync(w3mimg_op * wop) THROW_NONE;
static void win_close(w3mimg_op * wop) THROW_NONE;

static int win_load_image(w3mimg_op * wop, W3MImage * img, char *fname,
	int w, int h) THROW_NONE;
static int win_show_image(w3mimg_op * wop, W3MImage * img,
	int sx, int sy, int sw, int sh, int x, int y) THROW_NONE;
static void win_free_image(w3mimg_op * wop, W3MImage * img) THROW_NONE;
static int win_get_image_size(w3mimg_op * wop, W3MImage * img,
	char *fname, int *w, int *h) THROW_NONE;
static int win_clear(w3mimg_op * wop, int x, int y, int w, int h) THROW_NONE;

static int window_alive(w3mimg_op *wop) THROW_NONE;
static Gdiplus::Bitmap *read_image_file(w3mimg_op *wop, const char *fname) THROW_NONE;
static BOOL CALLBACK store_to_window_list(HWND hWnd, LPARAM wndlist) THROW_NONE;
static void clear_window_list(struct window_list *wl) THROW_NONE;
static const char *gdip_strerror(Gdiplus::Status status) THROW_NONE;
static void gdip_perror(w3mimg_op *wop, Gdiplus::Status status, const char *func) THROW_NONE;
static char *win32_strerror_alloc(DWORD status) THROW_NONE;
static void win32_perror(w3mimg_op *wop, DWORD status, const char *func) THROW_NONE;
#if 0 /* unused */
static WCHAR *mb2wstr_alloc(const char *) THROW_NONE;
static char *wstr2mb_alloc(const WCHAR *) THROW_NONE;
#endif

#define PRELUDE(wop, xi) \
    assert(wop); \
    struct win_info *xi = static_cast<struct win_info *>(wop->priv); \
    assert(xi)

win_image::win_image() THROW_NONE
    : gpbitmap(NULL), nframe(0)
{}

win_image::~win_image() THROW_NONE
{
    if (this->cache) {
	for (size_t i = 0; i != this->nframe; ++i) {
#ifdef USE_GDIP_CACHED_BITMAP
	    delete this->cache[i];
#else
	    if (this->cache[i])
		DeleteObject(this->cache[i]);
#endif
	}
	delete[] this->cache;
    }
    delete[] this->delay;
    delete this->gpbitmap;
}

int
win_image::load(w3mimg_op *wop, Gdiplus::Bitmap **p_gpbitmap, int *wreturn, int *hreturn) THROW_NONE
{
    PRELUDE(wop, xi);
    Gdiplus::Bitmap *gpbitmap = *p_gpbitmap;
    assert(gpbitmap);
    Gdiplus::Status status = Gdiplus::Ok;
    int retval = 0;

    Gdiplus::PropertyItem *loopcountbuf = NULL;
    Gdiplus::PropertyItem *delaybuf = NULL;
    unsigned long *delay = NULL;
    cache_handle *cache = NULL;

    if (xi->logfile) {
	fprintf(xi->logfile, "win_image::load(%p, %p, %p, %p) start\n",
		wop, gpbitmap, wreturn, hreturn);
    }
    {
	unsigned int width = gpbitmap->GetWidth();
	unsigned int height = gpbitmap->GetHeight();
	unsigned int nframe = gpbitmap->GetFrameCount(&Gdiplus::FrameDimensionTime);
	unsigned long loopcount = 0;

	if (xi->logfile)
	    fprintf(xi->logfile, "win_image::load(): size[0]=%ux%u\n", width, height);
	if (nframe == 0) {
	    // Not an animated picture
	    if (xi->logfile)
		fprintf(xi->logfile, "win_image::load(): zero frame count\n");
	    nframe = 1;
	    delay = new(std::nothrow) unsigned long[1];
	    if (delay == NULL)
		goto last;
	    delay[0] = 0;
	} else {
	    unsigned int loopcountsize = gpbitmap->GetPropertyItemSize(PropertyTagLoopCount);
	    unsigned int delaysize = gpbitmap->GetPropertyItemSize(PropertyTagFrameDelay);

	    // Get loop count
	    if (loopcountsize != 0) {
		loopcountbuf = (Gdiplus::PropertyItem *)malloc(loopcountsize);
		if (loopcountbuf == NULL)
		    goto last;
		status = gpbitmap->GetPropertyItem(PropertyTagLoopCount, loopcountsize, loopcountbuf);
		if (status != Gdiplus::Ok)
		    goto gdip_error;
		if (loopcountbuf->type == PropertyTagTypeShort &&
			loopcountbuf->length >= sizeof(unsigned short)) {
		    loopcount = *(unsigned short *)loopcountbuf->value;
		} else if (loopcountbuf->type == PropertyTagTypeLong &&
			loopcountbuf->length >= sizeof(unsigned long)) {
		    loopcount = *(unsigned long *)loopcountbuf->value;
		}
	    }
	    if (xi->logfile)
		fprintf(xi->logfile, "win_image::load(): loopcount=%lu\n", loopcount);
	    // Get delay times
	    if (delaysize != 0) {
		delaybuf = (Gdiplus::PropertyItem *)malloc(delaysize);
		if (delaybuf == NULL)
		    goto last;
		status = gpbitmap->GetPropertyItem(PropertyTagFrameDelay, delaysize, delaybuf);
		if (status != Gdiplus::Ok)
		    goto gdip_error;
		delay = new(std::nothrow) unsigned long[nframe];
		if (delay == NULL)
		    goto last;
		std::fill(delay, delay + nframe, 0);
		if (delaybuf->type == PropertyTagTypeShort) {
		    unsigned int count = delaybuf->length / sizeof(unsigned short);
		    for (unsigned int i = 0; i != count; ++i)
			delay[i] = ((unsigned short *)delaybuf->value)[i] * 10;
		} else if (delaybuf->type == PropertyTagTypeLong) {
		    unsigned int count = delaybuf->length / sizeof(unsigned long);
		    for (unsigned int i = 0; i != count; ++i)
			delay[i] = ((unsigned long *)delaybuf->value)[i] * 10;
		}
	    }
	    if (xi->logfile) {
		for (unsigned int i = 0; i != nframe; ++i)
		    fprintf(xi->logfile, "win_image::load(): delay[%u]=%lu\n", i, delay[i]);
	    }
	    // Get dimensions
	    for (unsigned int nextframe = 1; nextframe != nframe; ++nextframe) {
		status = gpbitmap->SelectActiveFrame(&Gdiplus::FrameDimensionTime, nextframe);
		if (status != Gdiplus::Ok) {
		    if (xi->logfile)
			fprintf(xi->logfile, "win_image::load(): SelectActiveFrame() to %u failed = %d: %s\n",
				nextframe, (int)status, gdip_strerror(status));
		    goto last;
		}
		unsigned int iw = gpbitmap->GetWidth();
		unsigned int ih = gpbitmap->GetHeight();
		if (iw > width)
		    width = iw;
		if (ih > height)
		    height = ih;
		if (xi->logfile)
		    fprintf(xi->logfile, "win_image::load(): size[%u]=%ux%u\n", nextframe, iw, ih);
	    }
	    // Back to the top
	    status = gpbitmap->SelectActiveFrame(&Gdiplus::FrameDimensionTime, 0);
	    if (status != Gdiplus::Ok) {
		if (xi->logfile)
		    fprintf(xi->logfile, "win_image::load(): SelectActiveFrame() to %u frame = %d: %s\n",
			    1U, (int)status, gdip_strerror(status));
		goto last;
	    }
	}
	// Allocate cache array
	cache = new(std::nothrow) cache_handle[nframe];
	if (cache == NULL)
	    goto last;
	std::fill(cache, cache + nframe, (cache_handle)NULL);
	// Sanity check
	if (width > SHRT_MAX || height > SHRT_MAX) {
	    if (xi->logfile)
		fprintf(xi->logfile, "win_image::load(): too big image: %ux%u\n", width, height);
	    goto last;
	}
	// Store the results
	if (wreturn)
	    *wreturn = (int)width;
	if (hreturn)
	    *hreturn = (int)height;
	this->gpbitmap = gpbitmap;
	*p_gpbitmap = NULL; // ownership transfer
	this->nframe = nframe;
	this->current = 0;
	this->tick = 0;
	this->loopcount = loopcount;
	this->delay = delay;
	delay = NULL; // ownership transfer
	this->cache = cache;
	cache = NULL; // ownership transfer
	retval = 1;
    }
    goto last;
    
gdip_error:
    gdip_perror(wop, status, "win_image::load");
    goto last;
last:
    delete[] cache;
    delete[] delay;
    free(delaybuf);
    free(loopcountbuf);
    if (xi->logfile)
	fprintf(xi->logfile, "win_image::load() = %d\n", retval);
    return retval;
}

int
win_image::show(w3mimg_op *wop, int sx, int sy, int sw, int sh, int x, int y) THROW_NONE
{
    PRELUDE(wop, xi);
    int retval = 0;
    Gdiplus::Status status = Gdiplus::Ok;
    cache_handle newcache = NULL;
#ifndef USE_GDIP_CACHED_BITMAP
    HDC windc = NULL;
    HDC memdc = NULL;
#endif

    if (xi->logfile)
	fprintf(xi->logfile, "win_image::show(%p, %d, %d, %d, %d, %d, %d) start\n",
		wop, sx, sy, sw, sh, x, y);
    if (!window_alive(wop))
	goto last;
    {
	int xx = x + wop->offset_x;
	int yy = y + wop->offset_y;

#ifdef USE_GDIP_CACHED_BITMAP
	// Prepare the Graphics object for painting
	Gdiplus::Graphics graphics(xi->window);
	if ((status = graphics.GetLastStatus()) != Gdiplus::Ok)
	    goto gdip_error;
	Gdiplus::Rect clip(xx, yy, sw, sh);
	status = graphics.SetClip(clip);
	if (status != Gdiplus::Ok)
	    goto gdip_error;

	// Clear the area first as the picture may be transparent
	status = graphics.Clear(Gdiplus::Color(xi->background_pixel));
	if (status != Gdiplus::Ok)
	    goto gdip_error;

	unsigned int retry_count = 2;
	do {
	    if (this->cache[this->current] == NULL) {
		// Cache the image
		Gdiplus::CachedBitmap *newcache = new Gdiplus::CachedBitmap(this->gpbitmap, &graphics);
		if (newcache == NULL)
		    goto last;
		if ((status = newcache->GetLastStatus()) != Gdiplus::Ok)
		    goto gdip_error;
		this->cache[this->current] = newcache;
		newcache = NULL; // ownership transfer
		--retry_count;
	    }
	    // Draw it
	    status = graphics.DrawCachedBitmap(this->cache[this->current], xx - sx, yy - sy);
	    if (status == Gdiplus::Ok)
		break;
	    // maybe the user altered the display configuration
	    if (xi->logfile)
		fprintf(xi->logfile, "win_image::show(): stale cache = %d: %s\n",
			(int)status, gdip_strerror(status));
	    delete this->cache[this->current];
	    this->cache[this->current] = NULL;
	    if (retry_count == 0)
		goto last;
	} while (1);
#else /* !USE_GDIP_CACHED_BITMAP */
	// Prepare the GC for painting
	windc = GetDC(xi->window);
	if (windc == NULL)
	    goto win32_error;
	// Prepare the GC for read/write access to the cached bitmap
	memdc = CreateCompatibleDC(windc);
	if (memdc == NULL)
	    goto win32_error;
	unsigned int retry_count = 2;
	do {
	    HGDIOBJ hbitmap_save;
	    if (this->cache[this->current] == NULL) {
		unsigned int ow = gpbitmap->GetWidth();
		unsigned int oh = gpbitmap->GetHeight();
		// Cache the image
		newcache = CreateCompatibleBitmap(windc, ow, oh);
		if (newcache == NULL)
		    goto win32_error;
		SetLastError(ERROR_SUCCESS); // SelectObject sometimes leave the error unchanged
		hbitmap_save = SelectObject(memdc, newcache);
		if (hbitmap_save == NULL || hbitmap_save == HGDI_ERROR)
		    goto win32_error;
		Gdiplus::Graphics tmp_graphics(memdc);
		if ((status = tmp_graphics.GetLastStatus()) != Gdiplus::Ok)
		    goto gdip_error;
		status = tmp_graphics.Clear(Gdiplus::Color(xi->background_pixel));
		if (status != Gdiplus::Ok)
		    goto gdip_error;
		status = tmp_graphics.DrawImage(this->gpbitmap, 0, 0, ow, oh);
		if (status != Gdiplus::Ok)
		    goto gdip_error;
		this->cache[this->current] = newcache;
		newcache = NULL; // ownership transfer
		--retry_count;
	    } else {
		SetLastError(ERROR_SUCCESS); // SelectObject sometimes leave the error unchanged
		hbitmap_save = SelectObject(memdc, this->cache[this->current]);
		if (hbitmap_save == NULL || hbitmap_save == HGDI_ERROR)
		    goto stale_cache;
	    }
	    // Draw it
	    if (BitBlt(windc, xx, yy, sw, sh, memdc, sx, sy, SRCCOPY))
		break;
	    // maybe the user altered the display configuration
stale_cache:
	    if (xi->logfile) {
		DWORD ecode = GetLastError();
		char *msg = win32_strerror_alloc(ecode);
		fprintf(xi->logfile, "win_image::show(): stale cache = %u: %s\n",
			(unsigned int)ecode, msg ? msg : "(unknown)");
		LocalFree(msg);
	    }
	    SetLastError(ERROR_SUCCESS); // SelectObject sometimes leave the error unchanged
	    hbitmap_save = SelectObject(memdc, hbitmap_save);
	    if (hbitmap_save == NULL || hbitmap_save == HGDI_ERROR)
		goto win32_error;
	    DeleteObject(this->cache[this->current]);
	    this->cache[this->current] = NULL;
	    if (retry_count == 0)
		goto last;
	} while (1);
#endif /* !USE_GDIP_CACHED_BITMAP */

	retval = 1;
    }
    goto last;
#ifndef USE_GDIP_CACHED_BITMAP
win32_error:
    win32_perror(wop, GetLastError(), "win_image::show");
    goto last;
#endif
gdip_error:
    gdip_perror(wop, status, "win_image::show");
    goto last;
last:
#ifdef USE_GDIP_CACHED_BITMAP
    delete newcache;
#else
    if (memdc)
	DeleteDC(memdc);
    if (windc)
	ReleaseDC(xi->window, windc);
    if (newcache)
	DeleteObject(newcache);
#endif
    if (xi->logfile)
	fprintf(xi->logfile, "win_image::show() = %d\n", retval);
    return retval;
}

int
win_image::animate(w3mimg_op * wop) THROW_NONE
{
    PRELUDE(wop, xi);
    int retval = 0;
    Gdiplus::Status status = Gdiplus::Ok;

    if (xi->logfile)
	fprintf(xi->logfile, "win_image::animate(%p) start\n", wop);
    {
	if (this->nframe <= 1)
	    goto animation_end;
#define UNIT_TICK 50
#define MIN_DELAY (UNIT_TICK*2)
	this->tick += UNIT_TICK;
	if (this->tick >= MIN_DELAY && this->tick >= this->delay[this->current]) {
	    this->tick = 0;
	    unsigned int nextframe = this->current + 1;
	    if (nextframe >= this->nframe) {
		if (this->loopcount == 1) // end of the loop
		    goto animation_end;
		nextframe = 0;
	    }
	    status = this->gpbitmap->SelectActiveFrame(&Gdiplus::FrameDimensionTime, nextframe);
	    if (status != Gdiplus::Ok)
		goto gdip_error;
	    this->current = nextframe;
	    if (nextframe == 0 && this->loopcount > 1)
		--this->loopcount;
	}
animation_end:
	retval = 1;
    }
    goto last;
gdip_error:
    gdip_perror(wop, status, "win_image::animate");
    goto last;
last:
    if (xi->logfile)
	fprintf(xi->logfile, "win_image::animate() = %d\n", retval);
    return retval;
}

static int
window_alive(w3mimg_op *wop) THROW_NONE
{
    PRELUDE(wop, xi);
    if (xi->window == NULL)
	return 0;
    if (IsWindow(xi->window))
	return 1;
    xi->window = NULL;
    fputs("w3mimgdisplay: target window disappeared\n", stderr);
    if (xi->logfile)
	fputs("w3mimgdisplay: target window disappeared\n", xi->logfile);
    return 0;
}

static int
win_init(w3mimg_op *) THROW_NONE
{
    // nothing to do
    return 1;
}

static int
win_finish(w3mimg_op *) THROW_NONE
{
    // nothing to do
    return 1;
}

static int
win_clear(w3mimg_op *wop, int x, int y, int w, int h) THROW_NONE
{
    PRELUDE(wop, xi);
    Gdiplus::Status status = Gdiplus::Ok;
    int retval = 0;

    if (xi->logfile)
	fprintf(xi->logfile, "win_clear(%p, %d, %d, %d, %d) start\n",
		wop, x, y, w, h);
    if (!window_alive(wop))
	goto last;
    {
	if (x < 0)
	    x = 0;
	if (y < 0)
	    y = 0;
	Gdiplus::SolidBrush brush(Gdiplus::Color(xi->background_pixel));
	if ((status = brush.GetLastStatus()) != Gdiplus::Ok)
	    goto gdip_error;
	Gdiplus::Graphics graphics(xi->window);
	if ((status = graphics.GetLastStatus()) != Gdiplus::Ok)
	    goto gdip_error;
	status = graphics.FillRectangle(&brush, x + wop->offset_x, y + wop->offset_y, w, h);
	if (status != Gdiplus::Ok)
	    goto gdip_error;
	retval = 1;
    }
    goto last;
gdip_error:
    gdip_perror(wop, status, "win_clear");
    goto last;
last:
    if (xi->logfile)
	fprintf(xi->logfile, "win_clear() = %d\n", retval);
    return retval;
}

static int
win_active(w3mimg_op * wop) THROW_NONE
{
    return window_alive(wop);
}

static void
win_set_background(w3mimg_op * wop, char *background) THROW_NONE
{
    PRELUDE(wop, xi);

    HDC windc = NULL;

    if (xi->logfile)
	fprintf(xi->logfile, "win_set_background(%p, \"%s\")\n", wop, background ? background : "(auto)");
    {
	// Fallback value
	// xi->background_pixel = Gdiplus::Color::White;
	xi->background_pixel = Gdiplus::Color::Black;

	// Explicit
	if (background) {
	    unsigned int r, g, b;
	    if (sscanf(background, "#%02x%02x%02x", &r, &g, &b) == 3) {
		xi->background_pixel = Gdiplus::Color::MakeARGB((BYTE)255, (BYTE)r, (BYTE)g, (BYTE)b);
		goto last;
	    }
	}

	// Auto detect
	if (xi->window == NULL || !IsWindow(xi->window))
	    goto last;
	windc = GetDC(xi->window);
	if (windc == NULL)
	    goto win32_error;
	COLORREF c = GetPixel(windc,
		    (wop->offset_x >= 1) ? (wop->offset_x - 1) : 0,
		    (wop->offset_y >= 1) ? (wop->offset_y - 1) : 0);
	xi->background_pixel = Gdiplus::Color::MakeARGB(
		(BYTE)255, GetRValue(c), GetGValue(c), GetBValue(c));
    }
    goto last;
win32_error:
    win32_perror(wop, GetLastError(), "win_set_background");
    goto last;
last:
    if (xi->logfile)
	fprintf(xi->logfile, "win_set_background() result = #%06x\n",
		(unsigned int)xi->background_pixel);
    if (windc)
	ReleaseDC(xi->window, windc);
}

static void
win_sync(w3mimg_op *) THROW_NONE
{
    // nothing to do
    return;
}

static void
win_close(w3mimg_op * wop) THROW_NONE
{
    PRELUDE(wop, xi);

    if (xi->gdiplus_token)
	Gdiplus::GdiplusShutdown(xi->gdiplus_token);
    if (xi->logfile) {
	fprintf(xi->logfile, "win_close(%p)\n", wop);
	fclose(xi->logfile);
    }
    delete xi;
    delete wop;
}

static Gdiplus::Bitmap *
read_image_file(w3mimg_op *wop, const char *fname) THROW_NONE
{
    PRELUDE(wop, xi);
    Gdiplus::Status status = Gdiplus::Ok;
    Gdiplus::Bitmap *retval = NULL;

    WCHAR *wfname = NULL;
    Gdiplus::Bitmap *gpbitmap = NULL;

    if (xi->logfile)
	fprintf(xi->logfile, "read_image_file(%p, \"%s\") start\n", wop, fname);
    {
	wfname = (WCHAR *)cygwin_create_path(CCP_POSIX_TO_WIN_W, fname);
	if (wfname == NULL)
	    goto last;
	gpbitmap = new Gdiplus::Bitmap(wfname);
	if (gpbitmap == NULL)
	    goto last;
	status = gpbitmap->GetLastStatus();
	switch (status) {
	    case Gdiplus::Ok:
		break;
	    case Gdiplus::UnknownImageFormat:
	    case Gdiplus::FileNotFound:
		goto last; // fail silently
	    default:
		goto gdip_error;
	}
	retval = gpbitmap;
	gpbitmap = NULL; // ownership transfer
    }
    goto last;
gdip_error:
    gdip_perror(wop, status, "read_image_file");
last:
    delete gpbitmap;
    free(wfname);
    if (xi->logfile)
	fprintf(xi->logfile, "read_image_file() = %p\n", retval);
    return retval;
}

static int
win_load_image(w3mimg_op * wop, W3MImage * img, char *fname, int w, int h) THROW_NONE
{
    PRELUDE(wop, xi);
    int retval = 0;
    Gdiplus::Bitmap *gpbitmap = NULL;
    win_image *wimg = NULL;

    assert(img);
    if (xi->logfile) {
	fprintf(xi->logfile, "win_load_image(%p, %p, \"%s\", %d, %d) start\n",
		wop, img, fname, w, h);
    }
    {
	gpbitmap = read_image_file(wop, fname);
	if (gpbitmap == NULL)
	    goto last;
	int iw, ih;
	wimg = new(std::nothrow) win_image;
	if (!wimg->load(wop, &gpbitmap, &iw, &ih))
	    goto last;
	img->pixmap = wimg;
	wimg = NULL; // ownership transfer
	img->width = (0 <= w && w < iw) ? w : iw;
	img->height = (0 <= h && h < ih) ? h : ih;
	retval = 1;
    }
    goto last;
last:
    delete wimg;
    delete gpbitmap;
    if (xi->logfile)
	fprintf(xi->logfile, "win_load_image() = %d\n", retval);
    return retval;
}

static int
win_show_image(w3mimg_op * wop, W3MImage * img, int sx, int sy, int sw,
	       int sh, int x, int y) THROW_NONE
{
    PRELUDE(wop, xi);
    int retval = 0;

    assert(img);
    win_image *wimg = static_cast<win_image *>(img->pixmap);
    assert(wimg);
    
    if (xi->logfile)
	fprintf(xi->logfile, "win_show_image(%p, %p, %d, %d, %d, %d, %d, %d) start\n",
		wop, img, sx, sy, sw, sh, x, y);
    int sww = sw ? sw : img->width;
    int shh = sh ? sh : img->height;
    retval = wimg->show(wop, sx, sy, sww, shh, x, y)
	&& wimg->animate(wop);
    if (xi->logfile)
	fprintf(xi->logfile, "win_show_image() = %d\n", retval);
    return retval;
}

static void
win_free_image(w3mimg_op * wop, W3MImage * img) THROW_NONE
{
    PRELUDE(wop, xi);

    assert(img);
    if (xi->logfile)
	fprintf(xi->logfile, "win_free_image(%p, %p) pixmap=%p\n", wop, img, img->pixmap);
    delete static_cast<win_image *>(img->pixmap);
    img->pixmap = NULL;
    img->width = 0;
    img->height = 0;
}

static int
win_get_image_size(w3mimg_op * wop, W3MImage *img_unused, char *fname, int *w, int *h) THROW_NONE
{
    PRELUDE(wop, xi);
    int retval = 0;
    Gdiplus::Bitmap *gpbitmap = NULL;
    win_image *wimg = NULL;

    if (xi->logfile) {
	fprintf(xi->logfile, "win_get_image_size(%p, %p, \"%s\", %p, %p) start\n",
		wop, img_unused, fname, w, h);
    }
    {
	gpbitmap = read_image_file(wop, fname);
	if (gpbitmap == NULL)
	    goto last;
	wimg = new(std::nothrow) win_image;
	if (wimg == NULL)
	    goto last;
	retval = wimg->load(wop, &gpbitmap, w, h);;
    }
    goto last;
last:
    delete wimg;
    delete gpbitmap;
    if (xi->logfile)
	fprintf(xi->logfile, "win_get_image_size() = %d\n", retval);
    return retval;
}

extern "C" w3mimg_op *
w3mimg_winopen()
{
    w3mimg_op *retval = NULL;
    Gdiplus::Status status = Gdiplus::Ok;

    w3mimg_op *wop = NULL;
    struct win_info *xi = NULL;
    struct window_list children = { NULL, 0, 0 };

    {
	// Quit if running on X
	const char *display_name;
	if ((display_name = getenv("DISPLAY")) != NULL &&
		display_name[0] && strcmp(display_name, ":0") != 0)
	    return NULL;

	// Allocate the context objects
	wop = new(std::nothrow) w3mimg_op(); // call the default ctor instead of "new w3mimg_op;"
	if (wop == NULL)
	    return NULL;
	wop->priv = xi = new(std::nothrow) win_info();
	if (xi == NULL)
	    goto last;

	// Debug logging
	const char *logging_dir;
	if ((logging_dir = getenv("W3MIMG_LOGDIR")) != NULL &&
		logging_dir[0]) {
	    size_t l = strlen(logging_dir) + sizeof "/w3mimgXXXXXXXXXX.log";
	    char *fname = (char *)malloc(l);
	    snprintf(fname, l, "%s/w3mimg%d.log", logging_dir, (int)getpid());
	    xi->logfile = fopen(fname, "a");
	    if (xi->logfile) {
		setvbuf(xi->logfile, NULL, _IONBF, 0);
		fprintf(xi->logfile, "\nw3mimg_winopen() start pid=%d\n", (int)getpid());
	    }
	}

	// Look for the window to draw the image
	xi->window = NULL;
	const char *windowid;
	if ((windowid = getenv("WINDOWID")) != NULL)
	    xi->window = FindWindowA(windowid, NULL);
	if (!xi->window)
	    xi->window = GetForegroundWindow();
	if (!xi->window)
	    goto win32_error;

	WINDOWINFO winfo = WINDOWINFO();
	winfo.cbSize = sizeof winfo;
	GetWindowInfo(xi->window, &winfo);
	wop->width = (int)(winfo.rcClient.right - winfo.rcClient.left);
	wop->height = (int)(winfo.rcClient.bottom - winfo.rcClient.top);

	// Search decendant windows and find out which is the text window
	while (1) {
	    HWND p_window = xi->window;

	    clear_window_list(&children);
	    EnumChildWindows(xi->window, &store_to_window_list, (LPARAM)&children);
	    for (unsigned int i = 0; i < children.nwnd; i++) {
		int width, height;

		GetWindowInfo(children.wnd[i], &winfo);
		width = (int)(winfo.rcClient.right - winfo.rcClient.left);
		height = (int)(winfo.rcClient.bottom - winfo.rcClient.top);
		if (width > wop->width * 0.7 &&
			height > wop->height * 0.7) {
		    /* maybe text window */
		    wop->width = width;
		    wop->height = height;
		    xi->window = children.wnd[i];
		}
	    }
	    if (p_window == xi->window)
		break;
	}

	// Terminal may leave some border pixels
	wop->offset_x = OFFSET_X;
	wop->offset_y = OFFSET_Y;

	// Start up the GDI+
	Gdiplus::GdiplusStartupInput startup_input; /// default ctor
	status = Gdiplus::GdiplusStartup(&xi->gdiplus_token, &startup_input, NULL);
	if (status != Gdiplus::Ok)
	    goto gdip_error;

	// Fill the context object
	wop->init = win_init;
	wop->finish = win_finish;
	wop->active = win_active;
	wop->set_background = win_set_background;
	wop->sync = win_sync;
	wop->close = win_close;
	wop->clear = win_clear;

	wop->load_image = win_load_image;
	wop->show_image = win_show_image;
	wop->free_image = win_free_image;
	wop->get_image_size = win_get_image_size;

	retval = wop; // take care of the object lifetime
    }
    goto last;
win32_error:
    win32_perror(wop, GetLastError(), "w3mimg_winopen");
    goto last;
gdip_error:
    gdip_perror(wop, status, "w3mimg_winopen");
    goto last;
last:
    if (xi && xi->logfile)
	fprintf(xi->logfile, "w3mimg_winopen() = %p\n", retval);
    clear_window_list(&children);
    if (!retval) {
	if (xi) {
	    if (xi->gdiplus_token)
		Gdiplus::GdiplusShutdown(xi->gdiplus_token);
	    if (xi->logfile)
		fclose(xi->logfile);
	    delete xi;
	}
	delete wop;
    }
    return retval;
}

static BOOL CALLBACK
store_to_window_list(HWND hWnd, LPARAM wndlist) THROW_NONE
{
    struct window_list *wl = (struct window_list *)wndlist;

    if (wl->nwnd >= wl->capacity) {
	size_t newsize = (wl->capacity < 4 ) ? 4 : (wl->capacity * 2);
	HWND *newbuf = (HWND *)realloc(wl->wnd, newsize * sizeof newbuf[0]);
	if (newbuf == NULL) {
	    clear_window_list(wl);
	    return FALSE;
	}
	wl->wnd = newbuf;
	wl->capacity = newsize;
    }
    wl->wnd[wl->nwnd++] = hWnd;
    return TRUE;
}

static void
clear_window_list(struct window_list *wl) THROW_NONE
{
    free(wl->wnd);
    wl->wnd = NULL;
    wl->nwnd = 0;
    wl->capacity = 0;
}

static const char *
gdip_strerror(Gdiplus::Status status) THROW_NONE
{
    size_t i;
    struct status_rec {
	Gdiplus::Status code;
	const char *str;
    };
    static const struct status_rec table[] = {
#define ERRITEM(s) { Gdiplus::s, #s }
	ERRITEM(Ok),
	ERRITEM(GenericError),
	ERRITEM(InvalidParameter),
	ERRITEM(OutOfMemory),
	ERRITEM(ObjectBusy),
	ERRITEM(InsufficientBuffer),
	ERRITEM(NotImplemented),
	ERRITEM(Win32Error),
	ERRITEM(WrongState),
	ERRITEM(Aborted),
	ERRITEM(FileNotFound),
	ERRITEM(ValueOverflow),
	ERRITEM(AccessDenied),
	ERRITEM(UnknownImageFormat),
	ERRITEM(FontFamilyNotFound),
	ERRITEM(FontStyleNotFound),
	ERRITEM(NotTrueTypeFont),
	ERRITEM(UnsupportedGdiplusVersion),
	ERRITEM(GdiplusNotInitialized),
	ERRITEM(PropertyNotFound),
	ERRITEM(PropertyNotSupported),
	ERRITEM(ProfileNotFound),
#undef ERRITEM
    };
    for (i = 0; i != sizeof table / sizeof table[0]; ++i)
	if (table[i].code == status)
	    return table[i].str;
    return "unknown";
}

static void
gdip_perror(w3mimg_op *wop, Gdiplus::Status status, const char *func) THROW_NONE
{
    const char *s = gdip_strerror(status);
    fprintf(stderr, "w3mimgdisplay: GDI+ error %d: %s\n", (int)status, s);
    if (wop && wop->priv) {
	struct win_info *xi = (struct win_info *)wop->priv;
	if (xi->logfile) {
	    fprintf(xi->logfile, "%s(): GDI+ error %d: %s\n", func, (int)status, s);
	}
    }
}

// Don't free() the result; use LocalFree() instead
static char *
win32_strerror_alloc(DWORD status) THROW_NONE
{
    char *errbuf = NULL;

    FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
	    FORMAT_MESSAGE_FROM_SYSTEM |
	    FORMAT_MESSAGE_IGNORE_INSERTS,
	    NULL, status, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
	    (LPSTR)&errbuf, 0, NULL);
    if (errbuf) {
	size_t len = strlen(errbuf);
	if (len > 0 && errbuf[len - 1] == '\n')
	    errbuf[len - 1] = '\0';
    }
    return errbuf;
}

static void
win32_perror(w3mimg_op *wop, DWORD status, const char *func) THROW_NONE
{
    char *errbuf = win32_strerror_alloc(status);
    const char *s = errbuf ? errbuf : "(unknown)";

    fprintf(stderr, "w3mimgdisplay: Win32 error %u: %s\n", (unsigned int)status, s);
    if (wop && wop->priv) {
	struct win_info *xi = (struct win_info *)wop->priv;
	if (xi->logfile) {
	    fprintf(xi->logfile, "%s(): Win32 error %u: %s\n",
		    func, (unsigned int)status, s);
	}
    }
    LocalFree(errbuf);
}

#if 0 /* unused */
static WCHAR *
mb2wstr_alloc(const char *s) THROW_NONE
{
    int len;
    WCHAR *buf = NULL;

    len = MultiByteToWideChar(CP_OEMCP, MB_PRECOMPOSED, s, -1, NULL, 0);
    if (len <= 0) {
	fprintf(stderr, "w3mimgdisplay: unable to convert string ecode=%u\n",
		(unsigned int)GetLastError());
	goto error;
    }
    buf = (WCHAR *)malloc(len * sizeof(WCHAR)); /* including L'\0' */
    if (!buf)
	goto error;
    len = MultiByteToWideChar(CP_OEMCP, MB_PRECOMPOSED, s, -1, buf, len);
    if (len <= 0) {
	fprintf(stderr, "w3mimgdisplay: unable to convert string ecode=%u\n",
		(unsigned int)GetLastError());
	goto error;
    }
    return buf;
error:
    free(buf);
    return NULL;
}

static char *
wstr2mb_alloc(const WCHAR *ws) THROW_NONE
{
    int len;
    char *buf = NULL;

    len = WideCharToMultiByte(CP_OEMCP, WC_COMPOSITECHECK, ws, -1, NULL, 0, NULL, NULL);
    if (len <= 0) {
	fprintf(stderr, "w3mimgdisplay: unable to convert string ecode=%u\n",
		(unsigned int)GetLastError());
	goto error;
    }
    buf = (char *)malloc(len); /* including '\0' */
    if (!buf)
	goto error;
    len = WideCharToMultiByte(CP_OEMCP, WC_COMPOSITECHECK, ws, -1, buf, len, NULL, NULL);
    if (len <= 0) {
	fprintf(stderr, "w3mimgdisplay: unable to convert string ecode=%u\n",
		(unsigned int)GetLastError());
	goto error;
    }
    return buf;
error:
    free(buf);
    return NULL;
}
#endif /* unused */