Merge pull request #22816 from CastagnaIT/fix_tx3g
[xbmc.git] / xbmc / guilib / DDSImage.cpp
blob124352d74f3c7c126bcde38459b32d212a53c4e6
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "DDSImage.h"
11 #include "XBTF.h"
12 #include "filesystem/File.h"
13 #include "utils/log.h"
15 #include <algorithm>
16 #include <string.h>
17 using namespace XFILE;
19 CDDSImage::CDDSImage()
21 m_data = NULL;
22 memset(&m_desc, 0, sizeof(m_desc));
25 CDDSImage::CDDSImage(unsigned int width, unsigned int height, unsigned int format)
27 m_data = NULL;
28 Allocate(width, height, format);
31 CDDSImage::~CDDSImage()
33 delete[] m_data;
36 unsigned int CDDSImage::GetWidth() const
38 return m_desc.width;
41 unsigned int CDDSImage::GetHeight() const
43 return m_desc.height;
46 unsigned int CDDSImage::GetFormat() const
48 if (m_desc.pixelFormat.flags & DDPF_RGB)
49 return 0; // Not supported
50 if (m_desc.pixelFormat.flags & DDPF_FOURCC)
52 if (strncmp((const char *)&m_desc.pixelFormat.fourcc, "DXT1", 4) == 0)
53 return XB_FMT_DXT1;
54 if (strncmp((const char *)&m_desc.pixelFormat.fourcc, "DXT3", 4) == 0)
55 return XB_FMT_DXT3;
56 if (strncmp((const char *)&m_desc.pixelFormat.fourcc, "DXT5", 4) == 0)
57 return XB_FMT_DXT5;
58 if (strncmp((const char *)&m_desc.pixelFormat.fourcc, "ARGB", 4) == 0)
59 return XB_FMT_A8R8G8B8;
61 return 0;
64 unsigned int CDDSImage::GetSize() const
66 return m_desc.linearSize;
69 unsigned char *CDDSImage::GetData() const
71 return m_data;
74 bool CDDSImage::ReadFile(const std::string &inputFile)
76 // open the file
77 CFile file;
78 if (!file.Open(inputFile))
79 return false;
81 // read the header
82 uint32_t magic;
83 if (file.Read(&magic, 4) != 4)
84 return false;
85 if (file.Read(&m_desc, sizeof(m_desc)) != sizeof(m_desc))
86 return false;
87 if (!GetFormat())
88 return false; // not supported
90 // allocate our data
91 m_data = new unsigned char[m_desc.linearSize];
92 if (!m_data)
93 return false;
95 // and read it in
96 if (file.Read(m_data, m_desc.linearSize) != static_cast<ssize_t>(m_desc.linearSize))
97 return false;
99 file.Close();
100 return true;
103 unsigned int CDDSImage::GetStorageRequirements(unsigned int width, unsigned int height, unsigned int format)
105 switch (format)
107 case XB_FMT_DXT1:
108 return ((width + 3) / 4) * ((height + 3) / 4) * 8;
109 case XB_FMT_DXT3:
110 case XB_FMT_DXT5:
111 return ((width + 3) / 4) * ((height + 3) / 4) * 16;
112 case XB_FMT_A8R8G8B8:
113 default:
114 return width * height * 4;
118 void CDDSImage::Allocate(unsigned int width, unsigned int height, unsigned int format)
120 memset(&m_desc, 0, sizeof(m_desc));
121 m_desc.size = sizeof(m_desc);
122 m_desc.flags = ddsd_caps | ddsd_pixelformat | ddsd_width | ddsd_height | ddsd_linearsize;
123 m_desc.height = height;
124 m_desc.width = width;
125 m_desc.linearSize = GetStorageRequirements(width, height, format);
126 m_desc.pixelFormat.size = sizeof(m_desc.pixelFormat);
127 m_desc.pixelFormat.flags = ddpf_fourcc;
128 memcpy(&m_desc.pixelFormat.fourcc, GetFourCC(format), 4);
129 m_desc.caps.flags1 = ddscaps_texture;
130 delete[] m_data;
131 m_data = new unsigned char[m_desc.linearSize];
134 const char *CDDSImage::GetFourCC(unsigned int format)
136 switch (format)
138 case XB_FMT_DXT1:
139 return "DXT1";
140 case XB_FMT_DXT3:
141 return "DXT3";
142 case XB_FMT_DXT5:
143 return "DXT5";
144 case XB_FMT_A8R8G8B8:
145 default:
146 return "ARGB";