-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTr2ImageHandler.cpp
More file actions
192 lines (169 loc) · 5.27 KB
/
Tr2ImageHandler.cpp
File metadata and controls
192 lines (169 loc) · 5.27 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright © 2014 CCP ehf.
#include "StdAfx.h"
#include "Tr2ImageHandler.h"
#include "Tr2PngHandler.h"
#include "Tr2DdsHandler.h"
#include "Tr2JpgHandler.h"
#include "Tr2TgaHandler.h"
#include "Tr2BmpHandler.h"
#include "PsdHandler.h"
#include "VtaHandler.h"
namespace
{
CcpMutex s_registerMutex( "ImageIO", "s_registerMutex", 50 );
TrackableStdVector<ImageIO::ImageFormatFunctions>& GetImageHandlers()
{
static TrackableStdVector<ImageIO::ImageFormatFunctions> s_imageHandlers( "ImageIO::GetImageHandlers" );
return s_imageHandlers;
}
void RegisterImageIOHandlers()
{
CcpAutoMutex lock( s_registerMutex );
static bool s_registered = false;
if( !s_registered )
{
ImageIO::Bmp::RegisterHandler();
ImageIO::Dds::RegisterHandler();
ImageIO::Jpeg::RegisterHandler();
ImageIO::Png::RegisterHandler();
ImageIO::Psd::RegisterHandler();
ImageIO::Tga::RegisterHandler();
ImageIO::Vta::RegisterHandler();
s_registered = true;
}
}
const wchar_t* GetExtension( const wchar_t* filename )
{
auto dot = wcsrchr( filename, L'.' );
return dot ? dot + 1 : filename + wcslen( filename );
}
const ImageIO::ImageFormatFunctions* GetImageHandler( const wchar_t* extension )
{
RegisterImageIOHandlers();
auto& handlers = GetImageHandlers();
for( size_t i = 0; i < handlers.size(); ++i )
{
if( ( *handlers[i].checkExtension )( extension ) )
{
return &handlers[i];
}
}
return nullptr;
}
}
namespace ImageIO
{
// --------------------------------------------------------------------------------------
// Description:
// Registers a new image format handler.
// Arguments:
// imageHandler - Pointers to image handling functions
// --------------------------------------------------------------------------------------
void RegisterImageHandler( const ImageFormatFunctions& imageHandler )
{
GetImageHandlers().push_back( imageHandler );
}
// --------------------------------------------------------------------------------------
// Description:
// Loads an image from stream into a bitmap.
// Arguments:
// src - Stream used for reading
// loadParameters - various loading parameters
// bitmap - (out) Destination bitmap
// metadata - (out) Optional image metadata
// Return Value:
// Result of the operation
// --------------------------------------------------------------------------------------
Result ReadImage( ICcpStream& src, const LoadParameters& loadParameters, HostBitmap& bitmap, ImageIO::Metadata* metadata )
{
auto handler = GetImageHandler( GetExtension( loadParameters.m_filename.c_str() ) );
if( !handler )
{
return Result::UNRECOGNIZED_IMAGE_TYPE;
}
if( !handler->readImage )
{
return Result::METHOD_NOT_SUPPORTED;
}
return ( *handler->readImage )( src, loadParameters, bitmap, metadata );
}
// --------------------------------------------------------------------------------------
// Description:
// Checks if saving an image into particular format is supported.
// Arguments:
// filename - Destination file name (used to determine image format)
// dimensions - Image dimensions/type/format
// Return Value:
// Result of the operation (OK if image saving is supported)
// --------------------------------------------------------------------------------------
Result IsSaveSupported( const wchar_t* filename, const BitmapDimensions& dimensions )
{
auto handler = GetImageHandler( GetExtension( filename ) );
if( !handler )
{
return Result::UNRECOGNIZED_IMAGE_TYPE;
}
if( !handler->isSaveSupported )
{
return Result::METHOD_NOT_SUPPORTED;
}
return ( *handler->isSaveSupported )( dimensions );
}
// --------------------------------------------------------------------------------------
// Description:
// Save a bitmap into a stream.
// Arguments:
// filename - Destination file name (used to determine image format)
// bitmap - Bitmap to save
// dest - Destination stream
// metadata - Optional image metadata
// Return Value:
// Result of the operation
// --------------------------------------------------------------------------------------
Result SaveImage( const wchar_t* filename, const HostBitmap& bitmap, ICcpStream& dest, const Metadata* metadata )
{
auto handler = GetImageHandler( GetExtension( filename ) );
if( !handler )
{
return Result::UNRECOGNIZED_IMAGE_TYPE;
}
if( !handler->save )
{
return Result::METHOD_NOT_SUPPORTED;
}
return ( *handler->save )( bitmap, dest, metadata );
}
}
ImageIO::LoadParameters::LoadParameters( const wchar_t* filename, uint32_t mipLevelSkipCount, uint32_t mipLevelMaxCount )
:m_filename( filename ),
m_mipLevelSkipCount( mipLevelSkipCount ),
m_mipLevelMaxCount( mipLevelMaxCount )
{
}
void ImageIO::LoadParameters::GetMipLevelRange( uint32_t width, uint32_t height, uint32_t& skipCount, uint32_t& mipCount ) const
{
skipCount = 0;
if( width > 8 && height > 8 )
{
if( mipCount > m_mipLevelSkipCount )
{
skipCount = m_mipLevelSkipCount;
}
else if( mipCount )
{
// limit total mipmap count to three!
skipCount = mipCount >= 3 ? mipCount - 3 : 0;
}
else
{
// mipCount == 0 is a special case where the driver is asked to autogenerate the mip maps.
// We cannot skip the first n in the chain because we don't have a chain to load!
}
}
mipCount -= skipCount;
if( mipCount > m_mipLevelMaxCount )
{
skipCount += (mipCount - m_mipLevelMaxCount);
mipCount = m_mipLevelMaxCount;
}
}