summaryrefslogtreecommitdiff
path: root/dali/internal/imaging/common/webp-loading.h
blob: 5a232fbfcefd6106fc182b1c84d128bd7d817e03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef DALI_INTERNAL_WEBP_LOADING_H
#define DALI_INTERNAL_WEBP_LOADING_H

/*
 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
// EXTERNAL INCLUDES
#include <dali/public-api/common/dali-vector.h>
#include <dali/public-api/common/vector-wrapper.h>
#include <dali/public-api/math/rect.h>
#include <dali/public-api/math/uint-16-pair.h>
#include <cstdint>
#include <memory>

// INTERNAL INCLUDES
#include <dali/internal/imaging/common/animated-image-loading-impl.h>
#include <dali/public-api/dali-adaptor-common.h>

namespace Dali
{
class PixelData;
typedef Dali::Uint16Pair ImageDimensions;

namespace Internal
{
namespace Adaptor
{
/**
 * Class to manage loading frames of an animated webp in small chunks. Lazy initializes only when
 * data is actually needed.
 * Note, once the WEBP has loaded, the undecoded data will reside in memory until this object
 * is released. (This is to speed up frame loads, which would otherwise have to re-acquire the
 * data from disk)
 */
class WebPLoading : public Internal::Adaptor::AnimatedImageLoading
{
public:
  /**
   * Create a WebPLoading with the given url and resourceType.
   * @param[in] url The url of the webp image to load
   * @param[in] isLocalResource The true or false whether this is a local resource.
   * @return A newly created WebPLoading.
   */
  static AnimatedImageLoadingPtr New(const std::string& url, bool isLocalResource);

  /**
   * Create a WebPLoading with the given url and resourceType.
   * @param[in] fp The file pointer to be load.
   * @return A newly created WebPLoading.
   */
  static AnimatedImageLoadingPtr New(FILE* const fp);

  /**
   * @brief Constructor
   *
   * Construct a Loader with the given URL
   * @param[in] url The url of the webp image to load
   * @param[in] isLocalResource The true or false whether this is a local resource.
   */
  WebPLoading(const std::string& url, bool isLocalResource);

  /**
   * @brief Constructor
   *
   * Construct a Loader with the given URL
   * @param[in] fp The file pointer to be load.
   */
  WebPLoading(FILE* const fp);

  /**
   * @brief Destructor
   */
  ~WebPLoading() override;

  /**
   * @brief Load the next Frame of the animated image.
   *
   * @note This function will load the entire animated image into memory if not already loaded.
   * @param[in] frameIndex The frame counter to load. Will usually be the next frame.
   * @return Dali::Devel::PixelBuffer The loaded PixelBuffer. If loading is fail, return empty handle.
   */
  Dali::Devel::PixelBuffer LoadFrame(uint32_t frameIndex) override;

  /**
   * @brief Get the size of a webp image.
   *
   * @return The width and height in pixels of the webp image.
   */
  ImageDimensions GetImageSize() const override;

  /**
   * @brief Get the number of frames in this webp.
   */
  uint32_t GetImageCount() const override;

  /**
   * @brief Get the frame interval of the frame index
   *
   * @note The frame is needed to be loaded before this function is called.
   *
   * @return The time interval between frameIndex and frameIndex + 1(microsecond).
   */
  uint32_t GetFrameInterval(uint32_t frameIndex) const override;

  /**
   * @brief Get the animated image file URL
   *
   * @return The URL string of the animated image file
   */
  std::string GetUrl() const override;

  /**
   * @brief Return whether the animated image loading is succeeded or not.
   *
   * @return True when the animated image loading is succeeded.
   */
  bool HasLoadingSucceeded() const override;

private:
  /**
   * @brief Decode Frame of the animated image.
   *
   * @param[in] frameIndex The frame counter to load. Will usually be the next frame.
   * @return Dali::Devel::PixelBuffer The loaded PixelBuffer. If loading is fail, return empty handle.
   */
  Dali::Devel::PixelBuffer DecodeFrame(uint32_t frameIndex);

private:
  struct Impl;
  Impl* mImpl;
};

} // namespace Adaptor

} // namespace Internal

} // namespace Dali

#endif // DALI_INTERNAL_WEBP_LOADING_H