1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "png_ninepatch_res.h"
17 #ifdef _WIN32
18 #include <winsock2.h>
19 #else
20 #include <arpa/inet.h>
21 #endif
22 
23 namespace OHOS {
24 namespace ImagePlugin {
Fill9patchOffsets(PngNinePatchRes * patch)25 static void Fill9patchOffsets(PngNinePatchRes *patch)
26 {
27     if (patch == nullptr) {
28         return;
29     }
30     patch->xDivsOffset = sizeof(PngNinePatchRes);
31     patch->yDivsOffset = patch->xDivsOffset + (patch->numXDivs * sizeof(int32_t));
32     patch->colorsOffset = patch->yDivsOffset + (patch->numYDivs * sizeof(int32_t));
33 }
34 
Deserialize(void * inData)35 PngNinePatchRes *PngNinePatchRes::Deserialize(void *inData)
36 {
37     PngNinePatchRes *patch = static_cast<PngNinePatchRes *>(inData);
38     patch->wasDeserialized = true;
39     Fill9patchOffsets(patch);
40 
41     return patch;
42 }
43 
DeviceToFile()44 void PngNinePatchRes::DeviceToFile()
45 {
46     int32_t *xDivs = GetXDivs();
47     for (int i = 0; i < numXDivs; i++) {
48         xDivs[i] = htonl(xDivs[i]);
49     }
50     int32_t *yDivs = GetYDivs();
51     for (int i = 0; i < numYDivs; i++) {
52         yDivs[i] = htonl(yDivs[i]);
53     }
54     paddingTop = htonl(paddingTop);
55     paddingBottom = htonl(paddingBottom);
56     paddingLeft = htonl(paddingLeft);
57     paddingRight = htonl(paddingRight);
58     uint32_t *colors = GetColors();
59     for (int i = 0; i < numColors; i++) {
60         colors[i] = htonl(colors[i]);
61     }
62 }
63 
FileToDevice()64 void PngNinePatchRes::FileToDevice()
65 {
66     int32_t *xDivs = GetXDivs();
67     for (int i = 0; i < numXDivs; i++) {
68         xDivs[i] = ntohl(xDivs[i]);
69     }
70     int32_t *yDivs = GetYDivs();
71     for (int i = 0; i < numYDivs; i++) {
72         yDivs[i] = ntohl(yDivs[i]);
73     }
74     paddingTop = ntohl(paddingTop);
75     paddingBottom = ntohl(paddingBottom);
76     paddingLeft = ntohl(paddingLeft);
77     paddingRight = ntohl(paddingRight);
78     uint32_t *colors = GetColors();
79     for (int i = 0; i < numColors; i++) {
80         colors[i] = ntohl(colors[i]);
81     }
82 }
83 
SerializedSize() const84 size_t PngNinePatchRes::SerializedSize() const
85 {
86     // The size of this struct is 32 bytes on the 32-bit target system
87     return 32 + numXDivs * sizeof(int32_t) + numYDivs * sizeof(int32_t) + numColors * sizeof(uint32_t);
88 }
89 } // namespace ImagePlugin
90 } // namespace OHOS
91