blob: d3482da34fa29d4c2025b89227ea32b935944298 [file] [log] [blame]
Leon Scrogginsa06d86a2011-03-02 16:56:54 -05001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "NinePatchPeeker.h"
18
19#include "SkBitmap.h"
20
21using namespace android;
22
23bool NinePatchPeeker::peek(const char tag[], const void* data, size_t length) {
24 if (strcmp("npTc", tag) == 0 && length >= sizeof(Res_png_9patch)) {
25 Res_png_9patch* patch = (Res_png_9patch*) data;
26 size_t patchSize = patch->serializedSize();
27 assert(length == patchSize);
28 // You have to copy the data because it is owned by the png reader
29 Res_png_9patch* patchNew = (Res_png_9patch*) malloc(patchSize);
30 memcpy(patchNew, patch, patchSize);
31 // this relies on deserialization being done in place
32 Res_png_9patch::deserialize(patchNew);
33 patchNew->fileToDevice();
Amith Yamasaniec4a5042012-04-04 10:27:15 -070034 free(fPatch);
Leon Scrogginsa06d86a2011-03-02 16:56:54 -050035 fPatch = patchNew;
36 //printf("9patch: (%d,%d)-(%d,%d)\n",
37 // fPatch.sizeLeft, fPatch.sizeTop,
38 // fPatch.sizeRight, fPatch.sizeBottom);
Leon Scrogginsa06d86a2011-03-02 16:56:54 -050039
40 // now update our host to force index or 32bit config
41 // 'cause we don't want 565 predithered, since as a 9patch, we know
42 // we will be stretched, and therefore we want to dither afterwards.
Leon Scroggins III82b7b0b2013-12-02 15:10:48 -050043 SkImageDecoder::PrefConfigTable table;
44 table.fPrefFor_8Index_NoAlpha_src = SkBitmap::kIndex8_Config;
45 table.fPrefFor_8Index_YesAlpha_src = SkBitmap::kIndex8_Config;
46 table.fPrefFor_8Gray_src = SkBitmap::kARGB_8888_Config;
47 table.fPrefFor_8bpc_NoAlpha_src = SkBitmap::kARGB_8888_Config;
48 table.fPrefFor_8bpc_YesAlpha_src = SkBitmap::kARGB_8888_Config;
49
50 fHost->setPrefConfigTable(table);
Amith Yamasaniec4a5042012-04-04 10:27:15 -070051 } else if (strcmp("npLb", tag) == 0 && length == sizeof(int) * 4) {
52 fLayoutBounds = new int[4];
53 memcpy(fLayoutBounds, data, sizeof(int) * 4);
Leon Scrogginsa06d86a2011-03-02 16:56:54 -050054 }
55 return true; // keep on decoding
56}