]> git.r.bdr.sh - rbdr/captura/blame_incremental - Captura/Core Extensions/CGImage+resize.swift
Format the code
[rbdr/captura] / Captura / Core Extensions / CGImage+resize.swift
... / ...
CommitLineData
1/*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://captura.tranquil.systems.
16 */
17import CoreGraphics
18
19extension CGImage {
20 func resize(by scale: CGFloat) -> CGImage? {
21 let width = Int(CGFloat(self.width) / scale)
22 let height = Int(CGFloat(self.height) / scale)
23
24 let bitsPerComponent = self.bitsPerComponent
25 let colorSpace = self.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)!
26 let bitmapInfo = self.bitmapInfo.rawValue
27
28 guard
29 let context = CGContext(
30 data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: 0,
31 space: colorSpace, bitmapInfo: bitmapInfo)
32 else {
33 return nil
34 }
35
36 context.interpolationQuality = .high
37 context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height))
38
39 return context.makeImage()
40 }
41}