summaryrefslogtreecommitdiffstats
path: root/xbmc/pictures/Picture.h
blob: 192982aa1adde3cd4785a9b8386d791d3d7fb90e (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
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "pictures/PictureScalingAlgorithm.h"
#include "utils/Job.h"

#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>

extern "C"
{
#include <libavutil/pixfmt.h>
}

class CTexture;

class CPicture
{
public:
  static bool GetThumbnailFromSurface(const unsigned char* buffer, int width, int height, int stride, const std::string &thumbFile, uint8_t* &result, size_t& result_size);
  static bool CreateThumbnailFromSurface(const unsigned char* buffer, int width, int height, int stride, const std::string &thumbFile);

  /*! \brief Create a tiled thumb of the given files
   \param files the files to create the thumb from
   \param thumb the filename of the thumb
   */
  static bool CreateTiledThumb(const std::vector<std::string> &files, const std::string &thumb);

  static bool ResizeTexture(
      const std::string& image,
      CTexture* texture,
      uint32_t& dest_width,
      uint32_t& dest_height,
      uint8_t*& result,
      size_t& result_size,
      CPictureScalingAlgorithm::Algorithm scalingAlgorithm = CPictureScalingAlgorithm::NoAlgorithm);
  static bool ResizeTexture(const std::string &image, uint8_t *pixels, uint32_t width, uint32_t height, uint32_t pitch,
    uint32_t &dest_width, uint32_t &dest_height, uint8_t* &result, size_t& result_size,
    CPictureScalingAlgorithm::Algorithm scalingAlgorithm = CPictureScalingAlgorithm::NoAlgorithm);

  /*! \brief Cache a texture, resizing, rotating and flipping as needed, and saving as a JPG or PNG
   \param texture a pointer to a CTexture
   \param dest_width [in/out] maximum width in pixels of cached version - replaced with actual cached width
   \param dest_height [in/out] maximum height in pixels of cached version - replaced with actual cached height
   \param dest the output cache file
   \return true if successful, false otherwise
   */
  static bool CacheTexture(
      CTexture* texture,
      uint32_t& dest_width,
      uint32_t& dest_height,
      const std::string& dest,
      CPictureScalingAlgorithm::Algorithm scalingAlgorithm = CPictureScalingAlgorithm::NoAlgorithm);
  static bool CacheTexture(uint8_t *pixels, uint32_t width, uint32_t height, uint32_t pitch, int orientation,
    uint32_t &dest_width, uint32_t &dest_height, const std::string &dest,
    CPictureScalingAlgorithm::Algorithm scalingAlgorithm = CPictureScalingAlgorithm::NoAlgorithm);

  static void GetScale(unsigned int width, unsigned int height, unsigned int &out_width, unsigned int &out_height);
  static bool ScaleImage(
      uint8_t* in_pixels,
      unsigned int in_width,
      unsigned int in_height,
      unsigned int in_pitch,
      AVPixelFormat in_format,
      uint8_t* out_pixels,
      unsigned int out_width,
      unsigned int out_height,
      unsigned int out_pitch,
      AVPixelFormat out_format,
      CPictureScalingAlgorithm::Algorithm scalingAlgorithm = CPictureScalingAlgorithm::NoAlgorithm);

private:
  static bool OrientateImage(uint32_t*& pixels,
                             unsigned int& width,
                             unsigned int& height,
                             int orientation,
                             unsigned int& stridePixels);

  static bool FlipHorizontal(uint32_t*& pixels,
                             const unsigned int& width,
                             const unsigned int& height,
                             const unsigned int& stridePixels);
  static bool FlipVertical(uint32_t*& pixels,
                           const unsigned int& width,
                           const unsigned int& height,
                           const unsigned int& stridePixels);
  static bool Rotate90CCW(uint32_t*& pixels,
                          unsigned int& width,
                          unsigned int& height,
                          unsigned int& stridePixels);
  static bool Rotate270CCW(uint32_t*& pixels,
                           unsigned int& width,
                           unsigned int& height,
                           unsigned int& stridePixels);
  static bool Rotate180CCW(uint32_t*& pixels,
                           const unsigned int& width,
                           const unsigned int& height,
                           const unsigned int& stridePixels);
  static bool Transpose(uint32_t*& pixels,
                        unsigned int& width,
                        unsigned int& height,
                        unsigned int& width_aligned);
  static bool TransposeOffAxis(uint32_t*& pixels,
                               unsigned int& width,
                               unsigned int& height,
                               unsigned int& stridePixels);
};

//this class calls CreateThumbnailFromSurface in a CJob, so a png file can be written without halting the render thread
class CThumbnailWriter : public CJob
{
  public:
    //WARNING: buffer is deleted from DoWork()
    CThumbnailWriter(unsigned char* buffer, int width, int height, int stride, const std::string& thumbFile);
    ~CThumbnailWriter() override;
    bool DoWork() override;

  private:
    unsigned char* m_buffer;
    int            m_width;
    int            m_height;
    int            m_stride;
    std::string    m_thumbFile;
};