summaryrefslogtreecommitdiff
path: root/windows/thumbnail.c
blob: e96c322011ed15e59a18ac0649e099c15981528e (plain)
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
/*
 * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "content/urldb.h"
#include "desktop/browser.h"
#include "utils/log.h"

#include "windows/bitmap.h"
#include "windows/gui.h"
#include "windows/plot.h"
#include "content/hlcache.h"


bool 
thumbnail_create(hlcache_handle *content, 
		 struct bitmap *bitmap,
		 const char *url)
{
	int width = content_get_width(content);
	int height = content_get_height(content);
	int i;
	uint8_t *pixdata;
	HDC hdc, minidc;
	HBITMAP bufferbm, minibm, minibm2;
	BITMAPINFO *bmi; 
	BITMAPINFOHEADER bmih;

	LOG(("creating thumbnail %p for url %s content %p", bitmap, url, content));

	bmi = malloc(sizeof(BITMAPINFOHEADER) + (bitmap->width * bitmap->height * 4));
	if (bmi == NULL) {
		return false;
	}

	bmih.biSize = sizeof(bmih);
	bmih.biWidth = bitmap->width;
	bmih.biHeight = - bitmap->height;
	bmih.biPlanes = 1;
	bmih.biBitCount = 32;
	bmih.biCompression = BI_RGB;
	bmih.biSizeImage = 4 * bitmap->height * bitmap->width;
	bmih.biXPelsPerMeter = 3600; /* 100 dpi */
	bmih.biYPelsPerMeter = 3600;
	bmih.biClrUsed = 0;
	bmih.biClrImportant = 0;
	bmi->bmiHeader = bmih;
	
	doublebuffering = true;

	if (bufferdc != NULL)
		DeleteDC(bufferdc);
	hdc = GetDC(current_hwnd);
	
	bufferdc = CreateCompatibleDC(hdc);
	if ((bufferdc == NULL) || (bmi == NULL)) {
		doublebuffering = false;
		ReleaseDC(current_hwnd, hdc);
		return false;
	}

	bufferbm = CreateCompatibleBitmap(hdc, width, height);
	if (bufferbm == NULL) {
		doublebuffering = false;
		ReleaseDC(current_hwnd, hdc);
		free(bmi);
		return false;
	}
	SelectObject(bufferdc, bufferbm);
	thumbnail = true;
	content_redraw(content, 0, 0, width, height, 0, 0,
			width, height, 1.0, 0xFFFFFF);
	thumbnail = false;
	
/*	scale bufferbm to minibm */

	minidc = CreateCompatibleDC(hdc);
	if (minidc == NULL) {
		doublebuffering = false;
		DeleteObject(bufferbm);
		ReleaseDC(current_hwnd, hdc);
		free(bmi);
		return false;
	}
	
	minibm = CreateCompatibleBitmap(hdc, bitmap->width, bitmap->height);
	if (minibm == NULL) {
		doublebuffering = false;
		DeleteObject(bufferbm);
		DeleteDC(minidc);
		ReleaseDC(current_hwnd, hdc);
		free(bmi);
		return false;
	}
	ReleaseDC(current_hwnd, hdc);
	
	SelectObject(minidc, minibm);
	
	StretchBlt(minidc, 0, 0, bitmap->width, bitmap->height, bufferdc, 0, 0,
			width, height, SRCCOPY);
	minibm2 = CreateCompatibleBitmap(minidc, bitmap->width, 
			bitmap->height);
	if (minibm2 == NULL) {
		doublebuffering = false;
		DeleteObject(bufferbm);
		DeleteObject(minibm);
		DeleteDC(minidc);
		free(bmi);
		return false;
	}
	SelectObject(minidc, minibm2);
	
/*	save data from minibm bmi */
	GetDIBits(minidc, minibm, 0, 1 - bitmap->height,
			bmi->bmiColors, bmi, DIB_RGB_COLORS);

	pixdata = (uint8_t *)(bitmap->pixdata);
	for (i = 0; i < bitmap->width * bitmap->height; i++) {
		pixdata[4 * i] = bmi->bmiColors[i].rgbRed;
		pixdata[4 * i + 1] = bmi->bmiColors[i].rgbGreen;
		pixdata[4 * i + 2] = bmi->bmiColors[i].rgbBlue;
		pixdata[4 * i + 3] = 0xFF;
	}
	doublebuffering = false;
		   
	DeleteObject(bufferbm);
	DeleteObject(minibm);
	DeleteObject(minibm2);
	DeleteDC(minidc);
	free(bmi);
	if (url)
		urldb_set_thumbnail(url, bitmap);
			
	doublebuffering = false;
	return true;
}