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;
205 default:
206 return false;
207 }
208
209 png_set_IHDR(fPngPtr,
210 fInfoPtr,
213 bitDepth,
214 pngColorType,
215 PNG_INTERLACE_NONE,
216 PNG_COMPRESSION_TYPE_BASE,
217 PNG_FILTER_TYPE_BASE);
218 png_set_sBIT(fPngPtr, fInfoPtr, &sigBit);
219
222 png_set_filter(fPngPtr, PNG_FILTER_TYPE_BASE, filters);
223
224 int zlibLevel = std::min(std::max(0,
options.fZLibLevel), 9);
226 png_set_compression_level(fPngPtr, zlibLevel);
227
228
230 if (comments != nullptr) {
231 std::vector<png_text> png_texts(comments->count());
232 std::vector<SkString> clippedKeys;
233 for (int i = 0; i < comments->count() / 2; ++i) {
234 const char* keyword;
235 const char* originalKeyword = comments->atStr(2 * i);
236 const char*
text = comments->atStr(2 * i + 1);
237 if (strlen(originalKeyword) <= PNG_KEYWORD_MAX_LENGTH) {
238 keyword = originalKeyword;
239 } else {
240 SkDEBUGFAILF(
"PNG tEXt keyword should be no longer than %d.",
241 PNG_KEYWORD_MAX_LENGTH);
242 clippedKeys.emplace_back(originalKeyword, PNG_KEYWORD_MAX_LENGTH);
243 keyword = clippedKeys.back().c_str();
244 }
245
246
247
248 png_texts[i].compression = PNG_TEXT_COMPRESSION_NONE;
249 png_texts[i].key = const_cast<png_charp>(keyword);
250 png_texts[i].text =
const_cast<png_charp
>(
text);
251 }
252 png_set_text(fPngPtr, fInfoPtr, png_texts.data(), png_texts.size());
253 }
254
255 return true;
256}
#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
@ 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
Type::kYUV Type::kRGBA() int(0.7 *637)
SkAlphaType alphaType() const
SkColorType colorType() const