xref: /aosp_15_r20/external/skia/example/external_client/src/use_skresources.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/codec/SkCodec.h"
9 #include "include/codec/SkJpegDecoder.h"
10 #include "include/codec/SkPngDecoder.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkString.h"
13 #include "modules/skresources/include/SkResources.h"
14 
15 #include <cstdio>
16 #include <filesystem>
17 
18 // bazel run :use_skresources -- $SKIA_HOME/resources
main(int argc,char ** argv)19 int main(int argc, char** argv) {
20     if (argc != 2) {
21         printf("Usage: %s [folder]", argv[0]);
22         return 1;
23     }
24 
25     SkCodecs::Register(SkJpegDecoder::Decoder());
26     SkCodecs::Register(SkPngDecoder::Decoder());
27     auto frp = skresources::FileResourceProvider::Make(SkString(argv[1]));
28 
29     // Try to load two arbitrary files in //resources/images
30 
31     sk_sp<skresources::ImageAsset> asset = frp->loadImageAsset("images", "baby_tux.png", "");
32     if (!asset) {
33         printf("Could not load baby_tux.png in images subdirectory\n");
34         return 1;
35     }
36     sk_sp<SkImage> tux = asset->getFrameData(0).image;
37     if (!tux) {
38         printf("Could not decode baby_tux.png in images subdirectory\n");
39         return 1;
40     }
41     printf("Baby Tux is %d by %d pixels big\n", tux->width(), tux->height());
42 
43     asset = frp->loadImageAsset("images", "CMYK.jpg", "");
44     if (!asset || !asset->getFrameData(0).image) {
45         printf("Could not load/decode CMYK.jpg in images subdirectory\n");
46         return 1;
47     }
48 
49     sk_sp<SkImage> cmyk = asset->getFrameData(0).image;
50     if (!cmyk) {
51         printf("Could not decode CMYK.jpg in images subdirectory\n");
52         return 1;
53     }
54     printf("CMYK is %d by %d pixels big\n", cmyk->width(), cmyk->height());
55 
56     return 0;
57 }
58