summaryrefslogtreecommitdiff
path: root/frontends/cocoa/bitmap.m
blob: 089870e8a86dfcd507c53f72a42127d3289bb8f7 (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
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
/*
 * 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/>.
 */

/**
 * \file
 * Cocoa implementation of bitmap operations.
 */

#import <Cocoa/Cocoa.h>

#import "netsurf/browser_window.h"
#import "netsurf/plotters.h"
#import "netsurf/bitmap.h"
#import "netsurf/content.h"

#import "cocoa/plotter.h"
#import "cocoa/bitmap.h"

#import "cocoa/arc.h"

#define BITS_PER_SAMPLE (8)
#define SAMPLES_PER_PIXEL (4)
#define BITS_PER_PIXEL (BITS_PER_SAMPLE * SAMPLES_PER_PIXEL)
#define BYTES_PER_PIXEL (BITS_PER_PIXEL / 8)
#define RED_OFFSET (0)
#define GREEN_OFFSET (1)
#define BLUE_OFFSET (2)
#define ALPHA_OFFSET (3)

static CGImageRef cocoa_prepare_bitmap(void *bitmap);

static inline NSMapTable *cocoa_get_bitmap_cache(void)
{
    static NSMapTable *cache = nil;
    if (cache == nil) {
        cache = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, 0);
    }
    return cache;
}

static inline NSBitmapImageRep *get_bitmap(void *bitmap) {
    NSCParameterAssert(bitmap);
    return (__bridge NSBitmapImageRep *)bitmap;
}

static int bitmap_get_width(void *bitmap)
{
    return (int)get_bitmap(bitmap).pixelsWide;
}

static int bitmap_get_height(void *bitmap)
{
    return (int)get_bitmap(bitmap).pixelsHigh;
}

static bool bitmap_get_opaque(void *bitmap)
{
    return get_bitmap(bitmap).isOpaque;
}

static void bitmap_destroy(void *bitmap)
{
    NSCParameterAssert(NULL != bitmap);

    cocoa_bitmap_modified(bitmap);
    arc_release(bitmap);
}

static void *bitmap_create(int width, int height, unsigned int state)
{
    NSBitmapImageRep *bmp = [[NSBitmapImageRep alloc]
        initWithBitmapDataPlanes:NULL
                      pixelsWide:width
                      pixelsHigh:height
                   bitsPerSample:BITS_PER_SAMPLE
                 samplesPerPixel:SAMPLES_PER_PIXEL
                        hasAlpha:YES
                        isPlanar:NO
                  colorSpaceName:NSDeviceRGBColorSpace
                    bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
                     bytesPerRow:BYTES_PER_PIXEL * width
                    bitsPerPixel:BITS_PER_PIXEL];

    return arc_retain(bmp);
}

static void bitmap_set_opaque(void *bitmap, bool opaque)
{
    get_bitmap(bitmap).opaque = opaque ? YES : NO;
}

static unsigned char *bitmap_get_buffer(void *bitmap)
{
    return get_bitmap(bitmap).bitmapData;
}

static size_t bitmap_get_rowstride(void *bitmap)
{
    return get_bitmap(bitmap).bytesPerRow;
}

static size_t bitmap_get_bpp(void *bitmap)
{
    return get_bitmap(bitmap).bitsPerPixel / 8;
}

static bool bitmap_test_opaque(void *bitmap)
{
    NSCParameterAssert(bitmap_get_bpp(bitmap) == BYTES_PER_PIXEL);

    unsigned char *buf = bitmap_get_buffer(bitmap);

    const size_t height = bitmap_get_height(bitmap);
    const size_t width = bitmap_get_width(bitmap);

    const size_t line_step = bitmap_get_rowstride(bitmap) - BYTES_PER_PIXEL * width;

    for (size_t y = 0; y < height; y++) {
        for (size_t x = 0; x < height; x++) {
            if (buf[ALPHA_OFFSET] != 0xFF)
                return false;
            buf += BYTES_PER_PIXEL;
        }
        buf += line_step;
    }

    return true;
}

static bool bitmap_save(void *bitmap, const char *path, unsigned flags)
{
    NSData *tiff = get_bitmap(bitmap).TIFFRepresentation;
    return [tiff writeToFile:@(path) atomically:YES];
}

void cocoa_bitmap_modified(void *bitmap)
{
    NSMapTable *cache = cocoa_get_bitmap_cache();
    CGImageRef image = NSMapGet(cache, bitmap);
    if (NULL != image) {
        CGImageRelease(image);
        NSMapRemove(cache, bitmap);
    }
}

CGImageRef cocoa_get_cgimage(void *bitmap)
{
    NSMapTable *cache = cocoa_get_bitmap_cache();

    CGImageRef result = NSMapGet(cache, bitmap);
    if (NULL == result) {
        result = cocoa_prepare_bitmap(bitmap);
        NSMapInsertKnownAbsent(cache, bitmap, result);
    }

    return result;
}

static CGImageRef cocoa_prepare_bitmap(void *bitmap)
{
    NSBitmapImageRep *bmp = get_bitmap(bitmap);

    size_t w = bmp.pixelsWide;
    size_t h = bmp.pixelsHigh;

    CGImageRef original = bmp.CGImage;

    if (h <= 1) {
        return CGImageRetain(original);
    }

    void *data = malloc(4 * w * h);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(data, w, h, BITS_PER_SAMPLE,
        BYTES_PER_PIXEL * w, colorSpace,
        [bmp isOpaque] ? kCGImageAlphaNoneSkipLast
                       : kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colorSpace);

    CGContextTranslateCTM(context, 0.0, h);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGRect rect = CGRectMake(0, 0, w, h);
    CGContextClearRect(context, rect);
    CGContextDrawImage(context, rect, original);

    CGImageRef result = CGBitmapContextCreateImage(context);

    CGContextRelease(context);
    free(data);

    return result;
}

static nserror bitmap_render(struct bitmap *bitmap, struct hlcache_handle *content)
{
    int bwidth = bitmap_get_width(bitmap);
    int bheight = bitmap_get_height(bitmap);

    struct redraw_context ctx = {
        .interactive = false,
        .background_images = true,
        .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);

    int width = MIN(content_get_width(content), 1024);
    int 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]];

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

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

    cocoa_bitmap_modified(bitmap);

    return NSERROR_OK;
}

static struct gui_bitmap_table bitmap_table = {
    .create = bitmap_create,
    .destroy = bitmap_destroy,
    .set_opaque = bitmap_set_opaque,
    .get_opaque = bitmap_get_opaque,
    .test_opaque = bitmap_test_opaque,
    .get_buffer = bitmap_get_buffer,
    .get_rowstride = bitmap_get_rowstride,
    .get_width = bitmap_get_width,
    .get_height = bitmap_get_height,
    .get_bpp = bitmap_get_bpp,
    .save = bitmap_save,
    .modified = cocoa_bitmap_modified,
    .render = bitmap_render,
};

struct gui_bitmap_table *cocoa_bitmap_table = &bitmap_table;