summaryrefslogtreecommitdiff
path: root/cocoa/thumbnail.m
blob: 3c1b8cb9e087837138a2991a4a8865a0d26726ff (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
/*
 * Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
 *
 * 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/>.
 */

#import <Cocoa/Cocoa.h>

#import "desktop/browser.h"
#import "desktop/plotters.h"
#import "desktop/thumbnail.h"
#import "content/urldb.h"
#import "cocoa/plotter.h"
#import "image/bitmap.h"

/* In platform specific thumbnail.c. */
bool thumbnail_create(struct hlcache_handle *content, struct bitmap *bitmap,
					  const char *url)
{
	int bwidth = bitmap_get_width( bitmap );
	int bheight = bitmap_get_height( bitmap );

	struct redraw_context ctx = {
		.interactive = false,
		.plot = &cocoa_plotters
	};

	CGColorSpaceRef cspace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
	CGContextRef bitmapContext = CGBitmapContextCreate( bitmap_get_buffer( bitmap ), 
													   bwidth, bheight, 
													   bitmap_get_bpp( bitmap ) * 8 / 4, 
													   bitmap_get_rowstride( bitmap ), 
													   cspace, kCGImageAlphaNoneSkipLast );
	CGColorSpaceRelease( cspace );

	size_t width = MIN( content_get_width( content ), 1024 );
	size_t height = ((width * bheight) + bwidth / 2) / bwidth;
	
	CGContextTranslateCTM( bitmapContext, 0, bheight );
	CGContextScaleCTM( bitmapContext, (CGFloat)bwidth / width, -(CGFloat)bheight / height );

	[NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithGraphicsPort: bitmapContext flipped: YES]];

	thumbnail_redraw( content, width, height, &ctx );

	[NSGraphicsContext setCurrentContext: nil];
	CGContextRelease( bitmapContext );

	bitmap_modified( bitmap );

	if (NULL != url) urldb_set_thumbnail( url, bitmap );

	return true;
}