-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlupng.h
More file actions
187 lines (166 loc) · 6.17 KB
/
lupng.h
File metadata and controls
187 lines (166 loc) · 6.17 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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Jan Solanti
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifdef __cplusplus
extern "C" {
#endif
#pragma once
#if defined(_MSC_VER) && (_MSC_VER < 1600)
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
#else
#include <stdint.h>
#include <stdlib.h>
#endif
typedef struct {
int32_t width;
int32_t height;
uint8_t channels;
uint8_t depth; /* must be 8 or 16 */
size_t dataSize;
uint8_t *data;
} LuImage;
typedef size_t (*PngReadProc)(void *outPtr, size_t size, size_t count,
void *userPtr);
typedef size_t (*PngWriteProc)(const void *inPtr, size_t size, size_t count,
void *userPtr);
typedef void *(*PngAllocProc)(size_t size, void *userPtr);
typedef void (*PngFreeProc)(void *ptr, void *userPtr);
typedef void (*PngWarnProc)(void *userPtr, const char *fmt, ...);
typedef struct {
/* loader */
PngReadProc readProc;
void *readProcUserPtr;
int skipSig;
/* writer */
PngWriteProc writeProc;
void *writeProcUserPtr;
int compressionLevel;
/* memory allocation */
PngAllocProc allocProc;
void *allocProcUserPtr;
PngFreeProc freeProc;
void *freeProcUserPtr;
/* warnings/error output */
PngWarnProc warnProc; /* set to NULL to disable output altogether */
void *warnProcUserPtr;
/* special case: avoid allocating a LuImage when loading or creating
* an image, just use this one */
LuImage *overrideImage;
} LuUserContext;
/**
* Initializes a LuUserContext to use the defaul malloc implementation.
*
* @param userCtx the LuUserContext to initialize
*/
void luUserContextInitDefault(LuUserContext *userCtx);
/**
* Creates a new Image object with the specified attributes.
* The data store of the Image is allocated but its contents are undefined.
* Only 8 and 16 bits deep images with 1-4 channels are supported.
*
* @param buffer pointer to an existing buffer (which may already contain
* the image data), or NULL to internally allocate a new buffer
* @param userCtx the user context (with the memory allocator function
* pointers to use), or NULL to use the default allocator
* (malloc).
*/
LuImage *luImageCreate(size_t width, size_t height, uint8_t channels,
uint8_t depth, uint8_t *buffer,
const LuUserContext *usrCtx);
/**
* Releases the memory associated with the given Image object.
*
* @param userCtx the user context (with the memory deallocator function
* pointers to use), or NULL to use the default deallocator
* (free). The deallocator should match the ones used for
* allocation.
*/
void luImageRelease(LuImage *img, const LuUserContext *usrCtx);
/**
* Extracts the raw image buffer form a LuImage and releases the
* then-orphaned LuImage object. This can be used if you want to use
* the image data in your own structures.
*
* @param userCtx the user context (with the memory deallocator function
* pointers to use), or NULL to use the default deallocator
* (free). The deallocator should match the ones used for
* allocation.
*/
uint8_t *luImageExtractBufAndRelease(LuImage *img,
const LuUserContext *userCtx);
/**
* Decodes a PNG image from a file
*
* @param filename the file name (optionally with full path) to read from.
*/
LuImage *luPngReadFile(const char *filename);
/**
* Decodes a PNG image with the provided read function into a LuImage struct
*
* @param readProc a function pointer to a user-defined function to use for
* reading the PNG data.
* @param userPtr an opaque pointer provided as an argument to readProc
* @param skipSig don't verify PNG signature - the bytes have already been
* removed from the input stream
*/
LuImage *luPngRead(PngReadProc readProc, void *userPtr, int skipSig);
/**
* Decodes a PNG image with the provided user context into a LuImage struct
*
* @param userCtx the LuUserContext to use
*/
LuImage *luPngReadUC(const LuUserContext *userCtx);
/**
* Encodes a LuImage struct to PNG and writes it out to a file.
*
* @param filename the file name (optionally with full path) to write to.
* Existing files will be overwritten!
* @param img the LuImage to encode
*/
int luPngWriteFile(const char *filename, const LuImage *img);
/**
* Encodes a LuImage struct to PNG and writes it out using a user-defined
* write function.
*
* @param writeProc a function pointer to a user-defined function that will
* be used for writing the final PNG data.
* @param userPtr an opaque pointer provided as an argument to writeProc
* @param img the LuImage to encode
*/
int luPngWrite(PngWriteProc writeProc, void *userPtr, const LuImage *img);
/**
* Encodes a LuImage struct to PNG and writes it out with the provided user
* context.
*
* @param userCtx the LuUserContext to use
* @param img the LuImage to encode
*/
int luPngWriteUC(const LuUserContext *userCtx, const LuImage *img);
#ifdef __cplusplus
}
#endif