118 {
119 if (setjmp(png_jmpbuf(fPngPtr))) {
120 return false;
121 }
122
123 int pngColorType;
124 png_color_8 sigBit;
125 int bitDepth = 8;
130 sigBit.red = 16;
131 sigBit.green = 16;
132 sigBit.blue = 16;
133 sigBit.alpha = 16;
134 bitDepth = 16;
135 pngColorType = srcInfo.
isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
136 fPngBytesPerPixel = 8;
137 break;
139 sigBit.gray = 8;
140 pngColorType = PNG_COLOR_TYPE_GRAY;
141 fPngBytesPerPixel = 1;
143 break;
146 sigBit.red = 8;
147 sigBit.green = 8;
148 sigBit.blue = 8;
149 sigBit.alpha = 8;
150 pngColorType = srcInfo.
isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
151 fPngBytesPerPixel = srcInfo.
isOpaque() ? 3 : 4;
152 break;
154 sigBit.red = 8;
155 sigBit.green = 8;
156 sigBit.blue = 8;
157 pngColorType = PNG_COLOR_TYPE_RGB;
158 fPngBytesPerPixel = 3;
160 break;
163 return false;
164 }
165
166 sigBit.red = 4;
167 sigBit.green = 4;
168 sigBit.blue = 4;
169 sigBit.alpha = 4;
170 pngColorType = srcInfo.
isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
171 fPngBytesPerPixel = srcInfo.
isOpaque() ? 3 : 4;
172 break;
174 sigBit.red = 5;
175 sigBit.green = 6;
176 sigBit.blue = 5;
177 pngColorType = PNG_COLOR_TYPE_RGB;
178 fPngBytesPerPixel = 3;
180 break;
183 sigBit.alpha = 8;
184 pngColorType = PNG_COLOR_TYPE_GRAY_ALPHA;
185 fPngBytesPerPixel = 2;
186 break;
188 bitDepth = 16;
189 sigBit.red = 10;
190 sigBit.green = 10;
191 sigBit.blue = 10;
192 sigBit.alpha = 2;
193 pngColorType = srcInfo.
isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
194 fPngBytesPerPixel = 8;
195 break;
198 bitDepth = 16;
199 sigBit.red = 10;
200 sigBit.green = 10;
201 sigBit.blue = 10;
202 pngColorType = PNG_COLOR_TYPE_RGB;
203 fPngBytesPerPixel = 6;
204 break;
206 bitDepth = 16;
207 sigBit.red = 10;
208 sigBit.green = 10;
209 sigBit.blue = 10;
210 sigBit.alpha = 10;
211 pngColorType = srcInfo.
isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
212 fPngBytesPerPixel = 8;
213 break;
214 default:
215 return false;
216 }
217
218 png_set_IHDR(fPngPtr,
219 fInfoPtr,
222 bitDepth,
223 pngColorType,
224 PNG_INTERLACE_NONE,
225 PNG_COMPRESSION_TYPE_BASE,
226 PNG_FILTER_TYPE_BASE);
227 png_set_sBIT(fPngPtr, fInfoPtr, &sigBit);
228
231 png_set_filter(fPngPtr, PNG_FILTER_TYPE_BASE, filters);
232
235 png_set_compression_level(fPngPtr, zlibLevel);
236
237
239 if (comments != nullptr) {
240 std::vector<png_text> png_texts(comments->
count());
241 std::vector<SkString> clippedKeys;
242 for (
int i = 0;
i < comments->
count() / 2; ++
i) {
243 const char* keyword;
244 const char* originalKeyword = comments->
atStr(2 *
i);
245 const char*
text = comments->
atStr(2 *
i + 1);
246 if (strlen(originalKeyword) <= PNG_KEYWORD_MAX_LENGTH) {
247 keyword = originalKeyword;
248 } else {
249 SkDEBUGFAILF(
"PNG tEXt keyword should be no longer than %d.",
250 PNG_KEYWORD_MAX_LENGTH);
251 clippedKeys.emplace_back(originalKeyword, PNG_KEYWORD_MAX_LENGTH);
252 keyword = clippedKeys.back().c_str();
253 }
254
255
256
257 png_texts[
i].compression = PNG_TEXT_COMPRESSION_NONE;
258 png_texts[
i].key =
const_cast<png_charp
>(keyword);
259 png_texts[
i].text =
const_cast<png_charp
>(
text);
260 }
261 png_set_text(fPngPtr, fInfoPtr, png_texts.data(), png_texts.size());
262 }
263
264 return true;
265}
#define SkDEBUGFAILF(fmt,...)
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
@ kBGRA_10101010_XR_SkColorType
pixel with 10 bits each for blue, green, red, alpha; in 64-bit word, extended range
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
@ kBGR_101010x_XR_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
static constexpr int kGraySigBit_GrayAlphaIsJustAlpha
const char * atStr(int index) const
static float max(float r, float g, float b)
static float min(float r, float g, float b)
SkAlphaType alphaType() const
SkColorType colorType() const