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.
12 #include "filesystem/File.h"
13 #include "utils/log.h"
17 using namespace XFILE
;
19 CDDSImage::CDDSImage()
22 memset(&m_desc
, 0, sizeof(m_desc
));
25 CDDSImage::CDDSImage(unsigned int width
, unsigned int height
, unsigned int format
)
28 Allocate(width
, height
, format
);
31 CDDSImage::~CDDSImage()
36 unsigned int CDDSImage::GetWidth() const
41 unsigned int CDDSImage::GetHeight() const
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)
54 if (strncmp((const char *)&m_desc
.pixelFormat
.fourcc
, "DXT3", 4) == 0)
56 if (strncmp((const char *)&m_desc
.pixelFormat
.fourcc
, "DXT5", 4) == 0)
58 if (strncmp((const char *)&m_desc
.pixelFormat
.fourcc
, "ARGB", 4) == 0)
59 return XB_FMT_A8R8G8B8
;
64 unsigned int CDDSImage::GetSize() const
66 return m_desc
.linearSize
;
69 unsigned char *CDDSImage::GetData() const
74 bool CDDSImage::ReadFile(const std::string
&inputFile
)
78 if (!file
.Open(inputFile
))
83 if (file
.Read(&magic
, 4) != 4)
85 if (file
.Read(&m_desc
, sizeof(m_desc
)) != sizeof(m_desc
))
88 return false; // not supported
91 m_data
= new unsigned char[m_desc
.linearSize
];
96 if (file
.Read(m_data
, m_desc
.linearSize
) != static_cast<ssize_t
>(m_desc
.linearSize
))
103 unsigned int CDDSImage::GetStorageRequirements(unsigned int width
, unsigned int height
, unsigned int format
)
108 return ((width
+ 3) / 4) * ((height
+ 3) / 4) * 8;
111 return ((width
+ 3) / 4) * ((height
+ 3) / 4) * 16;
112 case XB_FMT_A8R8G8B8
:
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
;
131 m_data
= new unsigned char[m_desc
.linearSize
];
134 const char *CDDSImage::GetFourCC(unsigned int format
)
144 case XB_FMT_A8R8G8B8
: