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
, XB_FMT format
)
28 Allocate(width
, height
, format
);
31 CDDSImage::~CDDSImage()
36 unsigned int CDDSImage::GetWidth() const
41 unsigned int CDDSImage::GetHeight() const
46 XB_FMT
CDDSImage::GetFormat() const
48 if (m_desc
.pixelFormat
.flags
& DDPF_RGB
)
49 return XB_FMT_UNKNOWN
; // 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
;
61 return XB_FMT_UNKNOWN
;
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
,
110 return ((width
+ 3) / 4) * ((height
+ 3) / 4) * 8;
113 return ((width
+ 3) / 4) * ((height
+ 3) / 4) * 16;
114 case XB_FMT_A8R8G8B8
:
116 return width
* height
* 4;
120 void CDDSImage::Allocate(unsigned int width
, unsigned int height
, XB_FMT format
)
122 memset(&m_desc
, 0, sizeof(m_desc
));
123 m_desc
.size
= sizeof(m_desc
);
124 m_desc
.flags
= ddsd_caps
| ddsd_pixelformat
| ddsd_width
| ddsd_height
| ddsd_linearsize
;
125 m_desc
.height
= height
;
126 m_desc
.width
= width
;
127 m_desc
.linearSize
= GetStorageRequirements(width
, height
, format
);
128 m_desc
.pixelFormat
.size
= sizeof(m_desc
.pixelFormat
);
129 m_desc
.pixelFormat
.flags
= ddpf_fourcc
;
130 memcpy(&m_desc
.pixelFormat
.fourcc
, GetFourCC(format
), 4);
131 m_desc
.caps
.flags1
= ddscaps_texture
;
133 m_data
= new unsigned char[m_desc
.linearSize
];
136 const char* CDDSImage::GetFourCC(XB_FMT format
)
146 case XB_FMT_A8R8G8B8
: