VTFLib
A C and C++ API that, with a few simple functions, can open and save .vtf and .vmt files.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
VTFFile.h
Go to the documentation of this file.
1 /*
2  * VTFLib
3  * Copyright (C) 2005-2010 Neil Jedrzejewski & Ryan Gregg
4 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later
9  * version.
10  */
11 
12 // ============================================================
13 // NOTE: This file is commented for compatibility with Doxygen.
14 // ============================================================
20 #ifndef VTFLIB_VTFFILE_H
21 #define VTFLIB_VTFFILE_H
22 
23 #include "stdafx.h"
24 #include "Readers.h"
25 #include "Writers.h"
26 #include "VTFFormat.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
33 
38 #pragma pack(1)
39 typedef struct tagSVTFImageFormatInfo
40 {
41  const vlChar *lpName;
51 #pragma pack()
52 
54 
60 #pragma pack(1)
61 typedef struct tagSVTFCreateOptions
62 {
63  vlUInt uiVersion[2];
65 
69  vlSingle sReflectivity[3];
70 
74 
77 
84 
88 
91 
102 
105 #pragma pack()
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 namespace VTFLib
112 {
114 
132  class VTFLIB_API CVTFFile
133  {
134  private:
135 
136  SVTFHeader *Header; // VTF header
137 
138  vlUInt uiImageBufferSize; // Size of VTF image data buffer
139  vlByte *lpImageData; // VTF image buffer
140 
141  vlUInt uiThumbnailBufferSize; // Size of VTF thumbnail image data buffer
142  vlByte *lpThumbnailImageData; // VTF thumbnail image buffer
143 
144  public:
145 
146  CVTFFile();
147 
149 
155  CVTFFile(const CVTFFile &VTFFile);
156 
158 
165  CVTFFile(const CVTFFile &VTFFile, VTFImageFormat ImageFormat);
166 
167  ~CVTFFile();
168 
169  public:
170 
172 
188  vlBool Create(vlUInt uiWidth, vlUInt uiHeight, vlUInt uiFrames = 1, vlUInt uiFaces = 1, vlUInt uiSlices = 1, VTFImageFormat ImageFormat = IMAGE_FORMAT_RGBA8888, vlBool bThumbnail = vlTrue, vlBool bMipmaps = vlTrue, vlBool bNullImageData = vlFalse);
189 
191 
202  vlBool Create(vlUInt uiWidth, vlUInt uiHeight, vlByte *lpImageDataRGBA8888, const SVTFCreateOptions &VTFCreateOptions);
203 
205 
220  vlBool Create(vlUInt uiWidth, vlUInt uiHeight, vlUInt uiFrames, vlUInt uiFaces, vlUInt vlSlices, vlByte **lpImageDataRGBA8888, const SVTFCreateOptions &VTFCreateOptions);
221 
223  vlVoid Destroy();
224 
226 
233  vlBool IsLoaded() const;
234 
236 
244  vlBool Load(const vlChar *cFileName, vlBool bHeaderOnly = vlFalse);
245 
247 
257  vlBool Load(const vlVoid *lpData, vlSize uiBufferSize, vlBool bHeaderOnly = vlFalse);
258 
260 
269  vlBool Load(vlVoid *pUserData, vlBool bHeaderOnly = vlFalse);
270 
272 
278  vlBool Save(const vlChar *cFileName) const;
279 
281 
289  vlBool Save(vlVoid *lpData, vlSize uiBufferSize, vlSize &uiSize) const;
290 
292 
298  vlBool Save(vlVoid *pUserData) const;
299 
300  private:
301  vlBool IsPowerOfTwo(vlUInt uiSize);
302  vlUInt NextPowerOfTwo(vlUInt uiSize);
303 
304  vlVoid ComputeResources();
305 
306  // Interface with out reader/writer classes
307  vlBool Load(IO::Readers::IReader *Reader, vlBool bHeaderOnly);
308  vlBool Save(IO::Writers::IWriter *Writer) const;
309 
310  public:
311 
313 
319  vlBool GetHasImage() const;
320 
321  vlUInt GetMajorVersion() const;
322  vlUInt GetMinorVersion() const;
323  vlUInt GetSize() const;
324 
325  vlUInt GetWidth() const;
326  vlUInt GetHeight() const;
327  vlUInt GetDepth() const;
328 
329  vlUInt GetFrameCount() const;
330  vlUInt GetFaceCount() const;
331  vlUInt GetMipmapCount() const;
332 
333  vlUInt GetStartFrame() const;
334  vlVoid SetStartFrame(vlUInt uiStartFrame);
335 
336  vlUInt GetFlags() const;
337  vlVoid SetFlags(vlUInt uiFlags);
338 
340 
346  vlBool GetFlag(VTFImageFlag ImageFlag) const;
347 
349 
355  vlVoid SetFlag(VTFImageFlag ImageFlag, vlBool bState);
356 
357  vlSingle GetBumpmapScale() const;
358 
360 
364  vlVoid SetBumpmapScale(vlSingle sBumpmapScale);
365 
367 
372  vlVoid GetReflectivity(vlSingle &sX, vlSingle &sY, vlSingle &sZ) const;
373 
375 
380  vlVoid SetReflectivity(vlSingle sX, vlSingle sY, vlSingle sZ);
381 
382  VTFImageFormat GetFormat() const;
383 
385 
397  vlByte *GetData(vlUInt uiFrame, vlUInt uiFace, vlUInt uiSlice, vlUInt uiMipmapLevel) const;
398 
400 
415  vlVoid SetData(vlUInt uiFrame, vlUInt uiFace, vlUInt uiSlice, vlUInt uiMipmapLevel, vlByte *lpData);
416 
417  public:
418 
419  vlBool GetHasThumbnail() const;
420 
421  vlUInt GetThumbnailWidth() const;
422  vlUInt GetThumbnailHeight() const;
423 
424  VTFImageFormat GetThumbnailFormat() const;
425 
427 
432  vlByte *GetThumbnailData() const;
433 
435 
443  vlVoid SetThumbnailData(vlByte *lpData);
444 
445  public:
446  vlBool GetSupportsResources() const;
447 
448  vlUInt GetResourceCount() const;
449  vlUInt GetResourceType(vlUInt uiIndex) const;
450  vlBool GetHasResource(vlUInt uiType) const;
451 
453 
460  vlVoid *GetResourceData(vlUInt uiType, vlUInt &uiSize) const;
461 
463 
472  vlVoid *SetResourceData(vlUInt uiType, vlUInt uiSize, vlVoid *lpData);
473 
474  public:
475 
477 
486  vlBool GenerateMipmaps(VTFMipmapFilter MipmapFilter = MIPMAP_FILTER_BOX, VTFSharpenFilter SharpenFilter = SHARPEN_FILTER_NONE);
487 
489 
502  vlBool GenerateMipmaps(vlUInt uiFace, vlUInt uiFrame, VTFMipmapFilter MipmapFilter = MIPMAP_FILTER_BOX, VTFSharpenFilter SharpenFilter = SHARPEN_FILTER_NONE);
503 
505 
512  vlBool GenerateThumbnail();
513 
515 
525  vlBool GenerateNormalMap(VTFKernelFilter KernelFilter = KERNEL_FILTER_3X3, VTFHeightConversionMethod HeightConversionMethod = HEIGHT_CONVERSION_METHOD_AVERAGE_RGB, VTFNormalAlphaResult NormalAlphaResult = NORMAL_ALPHA_RESULT_WHITE);
526 
528 
539  vlBool GenerateNormalMap(vlUInt uiFrame, VTFKernelFilter KernelFilter = KERNEL_FILTER_3X3, VTFHeightConversionMethod HeightConversionMethod = HEIGHT_CONVERSION_METHOD_AVERAGE_RGB, VTFNormalAlphaResult NormalAlphaResult = NORMAL_ALPHA_RESULT_WHITE);
540 
541  vlBool GenerateSphereMap();
542 
543  public:
544 
545  vlBool ComputeReflectivity();
546 
547  public:
548 
550 
556  static SVTFImageFormatInfo const &GetImageFormatInfo(VTFImageFormat ImageFormat);
557 
559 
570  static vlUInt ComputeImageSize(vlUInt uiWidth, vlUInt uiHeight, vlUInt uiDepth, VTFImageFormat ImageFormat);
571 
573 
585  static vlUInt ComputeImageSize(vlUInt uiWidth, vlUInt uiHeight, vlUInt uiDepth, vlUInt uiMipmaps, VTFImageFormat ImageFormat);
586 
588 
597  static vlUInt ComputeMipmapCount(vlUInt uiWidth, vlUInt uiHeight, vlUInt uiDepth);
598 
600 
612  static vlVoid ComputeMipmapDimensions(vlUInt uiWidth, vlUInt uiHeight, vlUInt uiDepth, vlUInt uiMipmapLevel, vlUInt &uiMipmapWidth, vlUInt &uiMipmapHeight, vlUInt &uiMipmapDepth);
613 
615 
626  static vlUInt ComputeMipmapSize(vlUInt uiWidth, vlUInt uiHeight, vlUInt uiDepth, vlUInt uiMipmapLevel, VTFImageFormat ImageFormat);
627 
628  private:
629 
630  // Calculates where in the VTF image the data begins
631  vlUInt ComputeDataOffset(vlUInt uiFrame, vlUInt uiFace, vlUInt uiSlice, vlUInt uiMipmapLevel, VTFImageFormat ImageFormat) const;
632 
633  public:
634 
636 
646  static vlBool ConvertToRGBA8888(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth, vlUInt uiHeight, VTFImageFormat SourceFormat);
647 
649 
659  static vlBool ConvertFromRGBA8888(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth, vlUInt uiHeight, VTFImageFormat DestFormat);
660 
662 
673  static vlBool Convert(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth, vlUInt uiHeight, VTFImageFormat SourceFormat, VTFImageFormat DestFormat);
674 
676 
694  static vlBool ConvertToNormalMap(const vlByte *lpSourceRGBA8888, vlByte *lpDestRGBA8888, vlUInt uiWidth, vlUInt uiHeight, VTFKernelFilter KernelFilter = KERNEL_FILTER_3X3, VTFHeightConversionMethod HeightConversionMethod = HEIGHT_CONVERSION_METHOD_AVERAGE_RGB, VTFNormalAlphaResult NormalAlphaResult = NORMAL_ALPHA_RESULT_WHITE, vlByte bMinimumZ = 0, vlSingle sScale = 2.0f, vlBool bWrap = vlFalse, vlBool bInvertX = vlFalse, vlBool bInvertY = vlFalse, vlBool bInvertZ = vlFalse);
695 
697 
710  static vlBool Resize(const vlByte *lpSourceRGBA8888, vlByte *lpDestRGBA8888, vlUInt uiSourceWidth, vlUInt uiSourceHeight, vlUInt uiDestWidth, vlUInt uiDestHeight, VTFMipmapFilter ResizeFilter = MIPMAP_FILTER_TRIANGLE, VTFSharpenFilter SharpenFilter = SHARPEN_FILTER_NONE);
711 
712  private:
713 
714  // DXTn format decompression functions
715  static vlBool DecompressDXT1(const vlByte *src, vlByte *dst, vlUInt uiWidth, vlUInt uiHeight);
716  static vlBool DecompressDXT3(const vlByte *src, vlByte *dst, vlUInt uiWidth, vlUInt uiHeight);
717  static vlBool DecompressDXT5(const vlByte *src, vlByte *dst, vlUInt uiWidth, vlUInt uiHeight);
718 
719  // DXTn format compression function
720  static vlBool CompressDXTn(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth, vlUInt uiHeight, VTFImageFormat DestFormat);
721 
722  public:
723 
725 
733  static vlVoid CorrectImageGamma(vlByte *lpImageDataRGBA8888, vlUInt uiWidth, vlUInt uiHeight, vlSingle sGammaCorrection);
734 
736 
748  static vlVoid ComputeImageReflectivity(const vlByte *lpImageDataRGBA8888, vlUInt uiWidth, vlUInt uiHeight, vlSingle &sX, vlSingle &sY, vlSingle &sZ);
749 
750  static vlVoid FlipImage(vlByte *lpImageDataRGBA8888, vlUInt uiWidth, vlUInt uiHeight);
751  static vlVoid MirrorImage(vlByte *lpImageDataRGBA8888, vlUInt uiWidth, vlUInt uiHeight);
752  };
753 }
754 
755 #endif
vlUInt uiResizeClampHeight
Maximum height to re-size to.
Definition: VTFFile.h:87
VTFImageFormat
Image data formats VTFLib supports.
Definition: VTFFormat.h:60
unsigned char vlByte
Single unsigned byte.
Definition: stdafx.h:52
vlBool bGammaCorrection
Gamma correct input image.
Definition: VTFFile.h:89
VTFSharpenFilter MipmapSharpenFilter
MIP map sharpen filter.
Definition: VTFFile.h:73
vlBool bNormalMap
Convert input image to a normal map.
Definition: VTFFile.h:92
vlBool bIsCompressed
Format is compressed (DXT).
Definition: VTFFile.h:48
vlBool bResizeClamp
Clamp re-size size.
Definition: VTFFile.h:85
VTFMipmapFilter ResizeFilter
Re-size filter.
Definition: VTFFile.h:80
vlBool bIsSupported
Format is supported by VTFLib.
Definition: VTFFile.h:49
VTFKernelFilter
Normal map creation kernel size indices.
Definition: VTFFormat.h:242
vlSingle sNormalScale
Normal map scale.
Definition: VTFFile.h:97
vlUInt uiAlphaBitsPerPixel
Format alpha bits per pixel. 0 for N/A.
Definition: VTFFile.h:47
VTFResizeMethod
Image re-size handling method indices.
Definition: VTFFormat.h:283
char vlChar
Single signed character.
Definition: stdafx.h:51
Structures and enumerations relating to the VTF file format.
vlBool bMipmaps
Generate MIPmaps. (Space is always allocated.)
Definition: VTFFile.h:71
vlByte bNormalMinimumZ
Minimum normal Z value.
Definition: VTFFile.h:96
vlUInt uiGreenBitsPerPixel
Format green bits per pixel. 0 for N/A.
Definition: VTFFile.h:45
vlSingle sBumpScale
Output image bump scale.
Definition: VTFFile.h:68
vlUInt uiBytesPerPixel
Format bytes per pixel.
Definition: VTFFile.h:43
VTFKernelFilter KernelFilter
Normal map generation kernel.
Definition: VTFFile.h:93
vlBool bNormalInvertZ
Invert the normal Z component.
Definition: VTFFile.h:101
vlUInt uiRedBitsPerPixel
Format red bits per pixel. 0 for N/A.
Definition: VTFFile.h:44
vlUInt uiStartFrame
Output image start frame.
Definition: VTFFile.h:67
unsigned int vlUInt
Unsigned integer value.
Definition: stdafx.h:56
vlBool bNormalInvertX
Invert the normal X component.
Definition: VTFFile.h:99
VTFResizeMethod ResizeMethod
New size compution method.
Definition: VTFFile.h:79
vlBool bThumbnail
Generate thumbnail image.
Definition: VTFFile.h:75
Application framework header plus VTFLib custom data types.
vlUInt uiResizeWidth
New width after re-size if method is RESIZE_SET.
Definition: VTFFile.h:82
VTFImageFormat ImageFormat
Output image output storage format.
Definition: VTFFile.h:64
void vlVoid
Void value.
Definition: stdafx.h:61
VTFMipmapFilter MipmapFilter
MIP map re-size filter.
Definition: VTFFile.h:72
VTF Creation options struct.
Definition: VTFFile.h:61
VTFMipmapFilter
MIP map reduction filter indices.
Definition: VTFFormat.h:184
= Red, Green, Blue, Alpha - 32 bpp
Definition: VTFFormat.h:62
VTF File access/creation class.
Definition: VTFFile.h:132
VTFHeightConversionMethod
Normal map height conversion method indices.
Definition: VTFFormat.h:256
vlSingle sGammaCorrection
Gamma correction to apply.
Definition: VTFFile.h:90
vlBool bNormalWrap
Wrap the normal map.
Definition: VTFFile.h:98
VTFImageFlag
VTF image header flags.
Definition: VTFFormat.h:125
vlUInt uiResizeClampWidth
Maximum width to re-size to.
Definition: VTFFile.h:86
vlUInt uiBitsPerPixel
Format bits per pixel.
Definition: VTFFile.h:42
VTFHeightConversionMethod HeightConversionMethod
Method or determining height from input image during normal map creation.
Definition: VTFFile.h:94
vlUInt uiFlags
Output image header flags.
Definition: VTFFile.h:66
VTFImageFormat info struct.
Definition: VTFFile.h:39
VTFSharpenFilter ResizeSharpenFilter
Sharpen filter.
Definition: VTFFile.h:81
vlBool bSphereMap
Generate a sphere map for six faced environment maps.
Definition: VTFFile.h:103
VTFSharpenFilter
MIP map sharpen filter indices.
Definition: VTFFormat.h:204
vlBool bNormalInvertY
Invert the normal Y component.
Definition: VTFFile.h:100
unsigned char vlBool
Boolean value 0/1.
Definition: stdafx.h:50
VTFNormalAlphaResult
Normal map alpha channel handling indices.
Definition: VTFFormat.h:273
vlUInt uiResizeHeight
New height after re-size if method is RESIZE_SET.
Definition: VTFFile.h:83
vlBool bResize
Resize the input image.
Definition: VTFFile.h:78
float vlSingle
Floating point number.
Definition: stdafx.h:59
VTFNormalAlphaResult NormalAlphaResult
How to handle output image alpha channel, post normal map creation.
Definition: VTFFile.h:95
vlBool bReflectivity
Compute image reflectivity.
Definition: VTFFile.h:76
const vlChar * lpName
Enumeration text equivalent.
Definition: VTFFile.h:41
vlUInt uiBlueBitsPerPixel
Format blue bits per pixel. 0 for N/A.
Definition: VTFFile.h:46