1 /*
2  * Copyright (C) 2020 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 "SkAndroidCodec.h"
18 #include "SkBitmap.h"
19 #include "SkCanvas.h"
20 #include "SkData.h"
21 #include "SkSurface.h"
22 
23 #include "../includes/memutils.h"
24 #include <fstream>
25 #include <iostream>
26 
27 #define SAMPLE_SIZE 6
28 char enable_selective_overload = ENABLE_NONE;
29 
decode(sk_sp<SkData> bytes,uint8_t sampleSize)30 int decode(sk_sp<SkData> bytes, uint8_t sampleSize) {
31   auto codec = SkAndroidCodec::MakeFromData(bytes);
32   if (!codec) {
33     return EXIT_FAILURE;
34   }
35 
36   auto size = codec->getSampledDimensions(sampleSize);
37   auto info = SkImageInfo::MakeN32Premul(size);
38   SkBitmap bm;
39   if (!bm.tryAllocPixels(info)) {
40     return EXIT_FAILURE;
41   }
42 
43   SkAndroidCodec::AndroidOptions options;
44   options.fSampleSize = sampleSize;
45 
46   codec->getAndroidPixels(bm.info(), bm.getPixels(), bm.rowBytes(), &options);
47   return EXIT_SUCCESS;
48 }
49 
main(int argc,char ** argv)50 int main(int argc, char **argv) {
51   if (argc != 2) {
52     return EXIT_FAILURE;
53   }
54   std::ifstream inFile(argv[1]);
55   if (!inFile) {
56     return EXIT_FAILURE;
57   }
58   inFile.seekg(0, inFile.end);
59   size_t size = inFile.tellg();
60   if (size < 1) {
61     inFile.close();
62     return EXIT_FAILURE;
63   }
64   inFile.seekg(0, inFile.beg);
65   uint8_t *data = (uint8_t *)malloc(size);
66   if (!data) {
67     return EXIT_FAILURE;
68   }
69   inFile.read(reinterpret_cast<char *>(data), size);
70   auto bytes = SkData::MakeWithoutCopy(data, size);
71   bytes = SkData::MakeSubset(bytes.get(), 1, size - 1);
72   enable_selective_overload = ENABLE_ALL;
73   int ret = decode(bytes, SAMPLE_SIZE);
74   enable_selective_overload = ENABLE_NONE;
75   inFile.close();
76   free(data);
77   return ret;
78 }
79