1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <skia/salbmp.hxx>
21 #include <skia/zone.hxx>
26 #include <quartz/cgutils.h>
28 #include <osx/saldata.hxx>
32 #include <skia/quartz/cgutils.h>
34 CGImageRef SkiaSalBitmap::CreateWithMask(const SalBitmap& rMask, int nX, int nY, int nWidth,
37 return CreateWithSalBitmapAndMask( *this, rMask, nX, nY, nWidth, nHeight );
40 /** creates an image from the given rectangle, replacing all black pixels
41 with nMaskColor and make all other full transparent */
42 CGImageRef SkiaSalBitmap::CreateColorMask(int nX, int nY, int nWidth, int nHeight,
43 Color nMaskColor) const
46 SkBitmap targetBitmap;
47 if (!targetBitmap.tryAllocPixels(SkImageInfo::Make(nWidth, nHeight, kN32_SkColorType, kPremul_SkAlphaType)))
51 paint.setBlendMode(SkBlendMode::kSrc); // set as is
52 SkMatrix matrix; // The image is needed upside-down.
53 matrix.preTranslate(0, targetBitmap.height());
54 matrix.setConcat(matrix, SkMatrix::Scale(1, -1));
56 SkCanvas canvas(targetBitmap);
57 canvas.concat(matrix);
58 canvas.drawImageRect(GetSkImage(), SkRect::MakeXYWH(nX, nY, nWidth, nHeight), SkRect::MakeXYWH(0, 0, nWidth, nHeight), SkSamplingOptions(), &paint, SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint);
61 const SkColor maskColor = SkiaHelper::toSkColor(nMaskColor);
62 for (int y = 0; y < targetBitmap.height(); y++)
64 SkColor *pPixels = targetBitmap.getAddr32(0, y);
65 for (int x = 0; x < targetBitmap.width(); x++)
66 *pPixels++ = (*pPixels == SK_ColorBLACK ? maskColor : SK_ColorTRANSPARENT);
69 targetBitmap.setImmutable();
71 CGContextRef xContext = CGBitmapContextCreate(targetBitmap.getAddr32(0, 0), targetBitmap.width(), targetBitmap.height(), 8, targetBitmap.rowBytes(), GetSalData()->mxRGBSpace, SkiaToCGBitmapType(targetBitmap.colorType(), targetBitmap.alphaType()));
75 CGImageRef xCroppedImage = CGBitmapContextCreateImage(xContext);
76 CGContextRelease(xContext);
81 CGImageRef SkiaSalBitmap::CreateCroppedImage(int nX, int nY, int nWidth, int nHeight) const
84 SkBitmap targetBitmap;
85 if (!targetBitmap.tryAllocPixels(SkImageInfo::Make(nWidth, nHeight, kN32_SkColorType, kPremul_SkAlphaType)))
89 paint.setBlendMode(SkBlendMode::kSrc); // set as is
90 SkMatrix matrix; // The image is needed upside-down.
91 matrix.preTranslate(0, targetBitmap.height());
92 matrix.setConcat(matrix, SkMatrix::Scale(1, -1));
94 SkCanvas canvas(targetBitmap);
95 canvas.concat(matrix);
96 canvas.drawImageRect(GetSkImage(), SkRect::MakeXYWH(nX, nY, nWidth, nHeight), SkRect::MakeXYWH(0, 0, nWidth, nHeight), SkSamplingOptions(), &paint, SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint);
98 targetBitmap.setImmutable();
100 CGContextRef xContext = CGBitmapContextCreate(targetBitmap.getAddr32(0, 0), targetBitmap.width(), targetBitmap.height(), 8, targetBitmap.rowBytes(), GetSalData()->mxRGBSpace, SkiaToCGBitmapType(targetBitmap.colorType(), targetBitmap.alphaType()));
104 CGImageRef xCroppedImage = CGBitmapContextCreateImage(xContext);
105 CGContextRelease(xContext);
106 return xCroppedImage;