Skip to content

Commit 78929f8

Browse files
authored
Make ktxStream public (KhronosGroup#438)
... to enable wrappers for languages with their own i/o models. * Move ktxStream definitions to ktx.h * Expose or implement ktxStream functions * Add eStreamTypeCustom * Add basic ktxStream <-> C++ integration tests * Add a test for ktxTexture1_WriteKTX2ToStream * Add a KTX2->KTX2 writing test
1 parent 535c883 commit 78929f8

21 files changed

+886
-166
lines changed

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ set(KTX_MAIN_SRC
160160
lib/ktxint.h
161161
lib/memstream.c
162162
lib/memstream.h
163-
lib/stream.h
164163
lib/strings.c
165164
lib/swap.c
166165
lib/texture.c

GNUmakefile

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ gypfiles=ktxtests.gyp \
9797
tests/texturetests/texturetests.gypi \
9898
tests/testimages/testimages.gypi \
9999
tests/transcodetests/transcodetests.gypi \
100+
tests/streamtests/streamtests.gypi \
100101
tests/unittests/unittests.gypi \
101102
tools/tools.gypi \
102103
tools/ktx2ktx2/ktx2ktx2.gypi \

include/ktx.h

+146-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <stdio.h>
2929
#include <stdbool.h>
30+
#include <sys/types.h>
3031

3132
/*
3233
* Don't use khrplatform.h in order not to break apps existing
@@ -202,6 +203,8 @@ typedef enum ktx_error_code_e {
202203
*/
203204
typedef struct ktxKVListEntry* ktxHashList;
204205

206+
typedef struct ktxStream ktxStream;
207+
205208
#define KTX_APIENTRYP KTX_APIENTRY *
206209
/**
207210
* @class ktxHashListEntry
@@ -477,6 +480,9 @@ typedef KTX_error_code
477480
typedef KTX_error_code
478481
(KTX_APIENTRY* PFNKTEXWRITETOMEMORY)(ktxTexture* This,
479482
ktx_uint8_t** bytes, ktx_size_t* size);
483+
typedef KTX_error_code
484+
(KTX_APIENTRY* PFNKTEXWRITETOSTREAM)(ktxTexture* This,
485+
ktxStream* dststr);
480486

481487
/**
482488
* @memberof ktxTexture
@@ -497,6 +503,7 @@ typedef KTX_error_code
497503
PFNKTEXWRITETOSTDIOSTREAM WriteToStdioStream;
498504
PFNKTEXWRITETONAMEDFILE WriteToNamedFile;
499505
PFNKTEXWRITETOMEMORY WriteToMemory;
506+
PFNKTEXWRITETOSTREAM WriteToStream;
500507
};
501508

502509
/****************************************************************
@@ -617,6 +624,14 @@ typedef KTX_error_code
617624
#define ktxTexture_WriteToMemory(This, ppDstBytes, pSize) \
618625
(This)->vtbl->WriteToMemory(This, ppDstBytes, pSize)
619626

627+
/**
628+
* @~English
629+
* @brief Helper for calling the WriteToStream virtual method of a ktxTexture.
630+
* @copydoc ktxTexture2_WriteToStream
631+
*/
632+
#define ktxTexture_WriteToStream(This, dststr) \
633+
(This)->vtbl->WriteToStream(This, dststr)
634+
620635

621636
/**
622637
* @class ktxTexture1
@@ -755,13 +770,120 @@ enum ktxTextureCreateFlagBits {
755770
*/
756771
typedef ktx_uint32_t ktxTextureCreateFlags;
757772

773+
/*===========================================================*
774+
* ktxStream
775+
*===========================================================*/
776+
777+
/*
778+
* This is unsigned to allow ktxmemstreams to use the
779+
* full amount of memory available. Platforms will
780+
* limit the size of ktxfilestreams to, e.g, MAX_LONG
781+
* on 32-bit and ktxfilestreams raises errors if
782+
* offset values exceed the limits. This choice may
783+
* need to be revisited if we ever start needing -ve
784+
* offsets.
785+
*
786+
* Should the 2GB file size handling limit on 32-bit
787+
* platforms become a problem, ktxfilestream will have
788+
* to be changed to explicitly handle large files by
789+
* using the 64-bit stream functions.
790+
*/
791+
#if defined(_MSC_VER) && defined(_WIN64)
792+
typedef unsigned __int64 ktx_off_t;
793+
#else
794+
typedef off_t ktx_off_t;
795+
#endif
796+
typedef struct ktxMem ktxMem;
797+
typedef struct ktxStream ktxStream;
798+
799+
enum streamType { eStreamTypeFile = 1, eStreamTypeMemory = 2, eStreamTypeCustom = 3 };
800+
801+
/**
802+
* @~English
803+
* @brief type for a pointer to a stream reading function
804+
*/
805+
typedef KTX_error_code (*ktxStream_read)(ktxStream* str, void* dst,
806+
const ktx_size_t count);
807+
/**
808+
* @~English
809+
* @brief type for a pointer to a stream skipping function
810+
*/
811+
typedef KTX_error_code (*ktxStream_skip)(ktxStream* str,
812+
const ktx_size_t count);
813+
814+
/**
815+
* @~English
816+
* @brief type for a pointer to a stream reading function
817+
*/
818+
typedef KTX_error_code (*ktxStream_write)(ktxStream* str, const void *src,
819+
const ktx_size_t size,
820+
const ktx_size_t count);
821+
822+
/**
823+
* @~English
824+
* @brief type for a pointer to a stream position query function
825+
*/
826+
typedef KTX_error_code (*ktxStream_getpos)(ktxStream* str, ktx_off_t* const offset);
827+
828+
/**
829+
* @~English
830+
* @brief type for a pointer to a stream position query function
831+
*/
832+
typedef KTX_error_code (*ktxStream_setpos)(ktxStream* str, const ktx_off_t offset);
833+
834+
/**
835+
* @~English
836+
* @brief type for a pointer to a stream size query function
837+
*/
838+
typedef KTX_error_code (*ktxStream_getsize)(ktxStream* str, ktx_size_t* const size);
839+
840+
/**
841+
* @~English
842+
* @brief Destruct a stream
843+
*/
844+
typedef void (*ktxStream_destruct)(ktxStream* str);
845+
846+
/**
847+
* @~English
848+
*
849+
* @brief Interface of ktxStream.
850+
*
851+
* @author Maksim Kolesin
852+
* @author Georg Kolling, Imagination Technology
853+
* @author Mark Callow, HI Corporation
854+
*/
855+
struct ktxStream
856+
{
857+
ktxStream_read read; /*!< @internal pointer to function for reading bytes. */
858+
ktxStream_skip skip; /*!< @internal pointer to function for skipping bytes. */
859+
ktxStream_write write; /*!< @internal pointer to function for writing bytes. */
860+
ktxStream_getpos getpos; /*!< @internal pointer to function for getting current position in stream. */
861+
ktxStream_setpos setpos; /*!< @internal pointer to function for setting current position in stream. */
862+
ktxStream_getsize getsize; /*!< @internal pointer to function for querying size. */
863+
ktxStream_destruct destruct; /*!< @internal destruct the stream. */
864+
865+
enum streamType type;
866+
union {
867+
FILE* file;
868+
ktxMem* mem;
869+
struct
870+
{
871+
void* address;
872+
void* allocatorAddress;
873+
ktx_size_t size;
874+
} custom_ptr;
875+
} data; /**< @internal pointer to the stream data. */
876+
ktx_off_t readpos; /**< @internal used by FileStream for stdin. */
877+
ktx_bool_t closeOnDestruct; /**< @internal Close FILE* or dispose of memory on destruct. */
878+
};
879+
758880
/*
759881
* See the implementation files for the full documentation of the following
760882
* functions.
761883
*/
762884

763885
/*
764-
* These three create a ktxTexture1 or ktxTexture2 according to the data
886+
* These four create a ktxTexture1 or ktxTexture2 according to the data
765887
* header, and return a pointer to the base ktxTexture class.
766888
*/
767889
KTX_API KTX_error_code KTX_APIENTRY
@@ -779,6 +901,11 @@ ktxTexture_CreateFromMemory(const ktx_uint8_t* bytes, ktx_size_t size,
779901
ktxTextureCreateFlags createFlags,
780902
ktxTexture** newTex);
781903

904+
KTX_API KTX_error_code KTX_APIENTRY
905+
ktxTexture_CreateFromStream(ktxStream* stream,
906+
ktxTextureCreateFlags createFlags,
907+
ktxTexture** newTex);
908+
782909
/*
783910
* Returns a pointer to the image data of a ktxTexture object.
784911
*/
@@ -824,7 +951,7 @@ ktxTexture1_Create(ktxTextureCreateInfo* createInfo,
824951
ktxTexture1** newTex);
825952

826953
/*
827-
* These three create a ktxTexture1 provided the data is in KTX format.
954+
* These four create a ktxTexture1 provided the data is in KTX format.
828955
*/
829956
KTX_API KTX_error_code KTX_APIENTRY
830957
ktxTexture1_CreateFromStdioStream(FILE* stdioStream,
@@ -841,6 +968,11 @@ ktxTexture1_CreateFromMemory(const ktx_uint8_t* bytes, ktx_size_t size,
841968
ktxTextureCreateFlags createFlags,
842969
ktxTexture1** newTex);
843970

971+
KTX_API KTX_error_code KTX_APIENTRY
972+
ktxTexture1_CreateFromStream(ktxStream* stream,
973+
ktxTextureCreateFlags createFlags,
974+
ktxTexture1** newTex);
975+
844976
KTX_API ktx_bool_t KTX_APIENTRY
845977
ktxTexture1_NeedsTranscoding(ktxTexture1* This);
846978

@@ -863,6 +995,12 @@ KTX_API KTX_error_code KTX_APIENTRY
863995
ktxTexture1_WriteKTX2ToMemory(ktxTexture1* This,
864996
ktx_uint8_t** bytes, ktx_size_t* size);
865997

998+
/*
999+
* Write a ktxTexture object to a ktxStream in KTX format.
1000+
*/
1001+
KTX_API KTX_error_code KTX_APIENTRY
1002+
ktxTexture1_WriteKTX2ToStream(ktxTexture1* This, ktxStream *dststr);
1003+
8661004
/*
8671005
* Create a new ktxTexture2.
8681006
*/
@@ -878,7 +1016,7 @@ KTX_API KTX_error_code KTX_APIENTRY
8781016
ktxTexture2_CreateCopy(ktxTexture2* orig, ktxTexture2** newTex);
8791017

8801018
/*
881-
* These three create a ktxTexture2 provided the data is in KTX2 format.
1019+
* These four create a ktxTexture2 provided the data is in KTX2 format.
8821020
*/
8831021
KTX_API KTX_error_code KTX_APIENTRY
8841022
ktxTexture2_CreateFromStdioStream(FILE* stdioStream,
@@ -895,6 +1033,11 @@ ktxTexture2_CreateFromMemory(const ktx_uint8_t* bytes, ktx_size_t size,
8951033
ktxTextureCreateFlags createFlags,
8961034
ktxTexture2** newTex);
8971035

1036+
KTX_API KTX_error_code KTX_APIENTRY
1037+
ktxTexture2_CreateFromStream(ktxStream* stream,
1038+
ktxTextureCreateFlags createFlags,
1039+
ktxTexture2** newTex);
1040+
8981041
KTX_API KTX_error_code KTX_APIENTRY
8991042
ktxTexture2_CompressBasis(ktxTexture2* This, ktx_uint32_t quality);
9001043

lib/filestream.h

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#define FILESTREAM_H
1616

1717
#include "ktx.h"
18-
#include "stream.h"
1918

2019
/*
2120
* ktxFileInit: Initialize a ktxStream to a ktxFileStream with a FILE object

lib/info.c

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <ktx.h>
3232

3333
#include "dfdutils/dfd.h"
34-
#include "stream.h"
3534
#include "filestream.h"
3635
#include "memstream.h"
3736
#include "ktxint.h"

lib/ktxint.h

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#define KTXINT_H
1818

1919
#include <math.h>
20-
#include "stream.h"
2120

2221
/* Define this to include the ETC unpack software in the library. */
2322
#ifndef SUPPORT_SOFTWARE_ETC_UNPACK

lib/libktx.gypi

-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
'ktxint.h',
8989
'memstream.c',
9090
'memstream.h',
91-
'stream.h',
9291
'strings.c',
9392
'swap.c',
9493
'texture.c',

lib/memstream.h

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#define MEMSTREAM_H
2323

2424
#include "ktx.h"
25-
#include "stream.h"
2625

2726
/*
2827
* Initialize a ktxStream to a ktxMemStream with internally

0 commit comments

Comments
 (0)