1 /*
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
21 using namespace android;
22
peek(const char tag[],const void * data,size_t length)23 bool NinePatchPeeker::peek(const char tag[], const void* data, size_t length) {
24 if (!strcmp("npTc", tag) && length >= sizeof(Res_png_9patch)) {
25 Res_png_9patch* patch = (Res_png_9patch*) data;
26 size_t patchSize = patch->serializedSize();
27 if (length != patchSize) {
28 return false;
29 }
30 // You have to copy the data because it is owned by the png reader
31 Res_png_9patch* patchNew = (Res_png_9patch*) malloc(patchSize);
32 memcpy(patchNew, patch, patchSize);
33 Res_png_9patch::deserialize(patchNew);
34 patchNew->fileToDevice();
35 free(mPatch);
36 mPatch = patchNew;
37 mPatchSize = patchSize;
38
39 // now update our host to force index or 32bit config
40 // 'cause we don't want 565 predithered, since as a 9patch, we know
41 // we will be stretched, and therefore we want to dither afterwards.
42 SkImageDecoder::PrefConfigTable table;
43 table.fPrefFor_8Index_NoAlpha_src = SkBitmap::kIndex8_Config;
44 table.fPrefFor_8Index_YesAlpha_src = SkBitmap::kIndex8_Config;
45 table.fPrefFor_8Gray_src = SkBitmap::kARGB_8888_Config;
46 table.fPrefFor_8bpc_NoAlpha_src = SkBitmap::kARGB_8888_Config;
47 table.fPrefFor_8bpc_YesAlpha_src = SkBitmap::kARGB_8888_Config;
48
49 mHost->setPrefConfigTable(table);
50 } else if (!strcmp("npLb", tag) && length == sizeof(int32_t) * 4) {
51 mHasInsets = true;
52 memcpy(&mOpticalInsets, data, sizeof(int32_t) * 4);
53 } else if (!strcmp("npOl", tag) && length == 24) { // 4 int32_ts, 1 float, 1 int32_t sized byte
54 mHasInsets = true;
55 memcpy(&mOutlineInsets, data, sizeof(int32_t) * 4);
56 mOutlineRadius = ((const float*)data)[4];
57 mOutlineAlpha = ((const int32_t*)data)[5] & 0xff;
58 }
59 return true; // keep on decoding
60 }
61