imsz
imsz.h
Go to the documentation of this file.
1 
4 #ifndef IMSZ_H
5 #define IMSZ_H
6 #pragma once
7 
8 #ifndef _POSIX_C_SOURCE
9  #define _POSIX_C_SOURCE 1
10 #endif
11 
12 #ifndef _POSIX_SOURCE
13  #define _POSIX_SOURCE
14 #endif
15 
16 #include <stdint.h>
17 #include <stdio.h>
18 
19 #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
20  #include <windows.h>
21 #endif
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
28  #ifdef IMSZ_STATIC
29  #define IMSZ_EXPORT
30  #else
31  #define IMSZ_EXPORT __declspec(dllimport)
32  #endif
33 #else
34  #if (defined(__GNUC__) && __GNUC__ >= 4) || defined(__clang__)
35  #define IMSZ_EXPORT __attribute__ ((visibility ("default")))
36  #else
37  #define IMSZ_EXPORT extern
38  #endif
39 #endif
40 
52 typedef enum ImError {
53  IMSZ_OK = 0,
54  IMSZ_ERR_IO = -1,
57 } ImError;
58 
60 typedef enum ImFormat {
61  IMSZ_GIF = 1u,
62  IMSZ_PNG = 2u,
63  IMSZ_BMP = 3u,
64  IMSZ_JPEG = 4u,
65  IMSZ_WEBP = 5u,
66  IMSZ_QOI = 6u,
67  IMSZ_PSD = 7u,
68  IMSZ_XCF = 8u,
69  IMSZ_ICO = 9u,
70  IMSZ_AVIF = 10u,
71  IMSZ_TIFF = 11u,
72  IMSZ_OpenEXR = 12u,
73  IMSZ_PCX = 13u,
74  IMSZ_TGA = 14u,
75  IMSZ_DDS = 15u,
76  IMSZ_HEIF = 16u,
77  IMSZ_JP2K = 17u,
78  IMSZ_DIB = 18u,
79  IMSZ_VTF = 19u,
80  IMSZ_ILBM = 20u,
81 } ImFormat;
82 
89 #define IMSZ_INIT { .width = (uint64_t)0, .height = (uint64_t)0, .format = 0 }
90 
92 typedef struct ImInfo {
93  uint64_t width;
94  uint64_t height;
95 
104  unsigned int format;
105 } ImInfo;
106 
112 IMSZ_EXPORT int imsz_from_path(const char *path, ImInfo *info_ptr);
113 
120 IMSZ_EXPORT int imsz_from_buffer(const void *buf, size_t len, ImInfo *info_ptr);
121 
127 IMSZ_EXPORT int imsz_from_fd(int fd, ImInfo *info_ptr);
128 
134 IMSZ_EXPORT int imsz_from_file(FILE *stream, ImInfo *info_ptr);
135 
140 IMSZ_EXPORT const char *imsz_format_name(unsigned int format);
141 
142 #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) || defined(__DOXYGEN__)
143  IMSZ_EXPORT int imsz_from_pathw(const wchar_t *path, ImInfo *info_ptr);
149 
155  IMSZ_EXPORT int imsz_from_handle(HANDLE hnd, ImInfo *info_ptr);
156 
161  IMSZ_EXPORT const wchar_t *imsz_format_namew(unsigned int format);
162 
163  #define imsz_2_(arg1, arg2) \
164  _Generic((arg1), \
165  wchar_t*: imsz_from_pathw((const wchar_t*)(arg1), (arg2)), \
166  const wchar_t*: imsz_from_pathw((const wchar_t*)(arg1), (arg2)), \
167  char*: imsz_from_path((const char*)(arg1), (arg2)), \
168  const char*: imsz_from_path((const char*)(arg1), (arg2)), \
169  FILE*: imsz_from_file((FILE*)(arg1), (arg2)), \
170  HANDLE: imsz_from_handle((HANDLE)(arg1), (arg2)), \
171  int: imsz_from_fd((intptr_t)(arg1), (arg2)) \
172  )
173 #else
174  #define imsz_2_(arg1, arg2) \
175  _Generic((arg1), \
176  char*: imsz_from_path((const char*)(arg1), (arg2)), \
177  const char*: imsz_from_path((const char*)(arg1), (arg2)), \
178  FILE*: imsz_from_file((FILE*)(arg1), (arg2)), \
179  int: imsz_from_fd((intptr_t)(arg1), (arg2)) \
180  )
181 #endif
182 
183 #define imsz_3_(arg1, arg2, arg3) \
184  imsz_from_buffer((arg1), (arg2), (arg3))
185 
186 #define imsz_va_dispatch_(arg1, arg2, arg3, arg4, ...) arg4
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 
192 #if defined(__cplusplus) || defined(__DOXYGEN__)
193 
197 inline int imsz(const char *path, ImInfo *info_ptr) {
198  return imsz_from_path(path, info_ptr);
199 }
200 
204 inline int imsz(const void *buf, size_t len, ImInfo *info_ptr) {
205  return imsz_from_buffer(buf, len, info_ptr);
206 }
207 
211 inline int imsz(int fd, ImInfo *info_ptr) {
212  return imsz_from_fd(fd, info_ptr);
213 }
214 
218 inline int imsz(FILE *file, ImInfo *info_ptr) {
219  return imsz_from_file(file, info_ptr);
220 }
221 
222 #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) || defined(__DOXYGEN__)
223 inline int imsz(const wchar_t *path, ImInfo *info_ptr) {
227  return imsz_from_pathw(path, info_ptr);
228 }
229 
233 inline int imsz(HANDLE hnd, ImInfo *info_ptr) {
234  return imsz_from_handle(hnd, info_ptr);
235 }
236 #endif
237 #endif
238 
239 #if !defined(__cplusplus) || defined(__DOXYGEN__)
240 
258  #define imsz(...) imsz_va_dispatch_(__VA_ARGS__, imsz_3_, imsz_2_, imsz_error_)(__VA_ARGS__)
259 
260 #endif
261 
262 
263 #endif
imsz_from_fd
int imsz_from_fd(int fd, ImInfo *info_ptr)
imsz_format_name
const char * imsz_format_name(unsigned int format)
IMSZ_ILBM
@ IMSZ_ILBM
Interleaved Bitmap files, including Planar Bitmap variant.
Definition: imsz.h:80
IMSZ_GIF
@ IMSZ_GIF
Graphics Interchange Format files in version GIF87a or GIF89a.
Definition: imsz.h:61
IMSZ_QOI
@ IMSZ_QOI
Quite OK Image format files.
Definition: imsz.h:66
ImInfo::width
uint64_t width
Width of the image.
Definition: imsz.h:93
imsz
int imsz(const char *path, ImInfo *info_ptr)
Alias of imsz_from_path()
Definition: imsz.h:197
IMSZ_OK
@ IMSZ_OK
No error.
Definition: imsz.h:53
IMSZ_OpenEXR
@ IMSZ_OpenEXR
OpenEXR files.
Definition: imsz.h:72
IMSZ_PNG
@ IMSZ_PNG
Portable Network Graphics files. Requires the first chunk to be IHDR.
Definition: imsz.h:62
IMSZ_VTF
@ IMSZ_VTF
Valve Texture Format.
Definition: imsz.h:79
IMSZ_TIFF
@ IMSZ_TIFF
Tag Image File Format. Supports big endian and little endian TIFF files.
Definition: imsz.h:71
IMSZ_DDS
@ IMSZ_DDS
DirectDraw Surface files.
Definition: imsz.h:75
IMSZ_HEIF
@ IMSZ_HEIF
HEIC/HEIF files.
Definition: imsz.h:76
ImInfo
struct ImInfo ImInfo
The width, height and format of an image.
IMSZ_JP2K
@ IMSZ_JP2K
JPEG 2000 files.
Definition: imsz.h:77
IMSZ_XCF
@ IMSZ_XCF
GIMP files.
Definition: imsz.h:68
IMSZ_DIB
@ IMSZ_DIB
Device-Independent bitmap files.
Definition: imsz.h:78
ImInfo::height
uint64_t height
Height of the image.
Definition: imsz.h:94
imsz_from_buffer
int imsz_from_buffer(const void *buf, size_t len, ImInfo *info_ptr)
IMSZ_ERR_IO
@ IMSZ_ERR_IO
IO error happened, but no OS error (errno or WIndows error code) was reported. (Classic should never ...
Definition: imsz.h:54
ImInfo
The width, height and format of an image.
Definition: imsz.h:92
ImInfo::format
unsigned int format
Values from ImFormat.
Definition: imsz.h:104
IMSZ_JPEG
@ IMSZ_JPEG
Joint Photographic Experts Group files.
Definition: imsz.h:64
IMSZ_PCX
@ IMSZ_PCX
PiCture eXchange files.
Definition: imsz.h:73
IMSZ_PSD
@ IMSZ_PSD
Adobe Photoshop files.
Definition: imsz.h:67
IMSZ_TGA
@ IMSZ_TGA
TARGA (Truevision Advanced Raster Graphics Adapter) files.
Definition: imsz.h:74
ImError
ImError
Error values.
Definition: imsz.h:52
ImFormat
ImFormat
All supported image formats.
Definition: imsz.h:60
imsz_from_path
int imsz_from_path(const char *path, ImInfo *info_ptr)
IMSZ_ERR_PARSER
@ IMSZ_ERR_PARSER
File format was detected, but there was an error parsing the file. ImInfo::format will be set to the ...
Definition: imsz.h:55
imsz_format_namew
const wchar_t * imsz_format_namew(unsigned int format)
IMSZ_ERR_UNSUPPORTED
@ IMSZ_ERR_UNSUPPORTED
File format is not supported.
Definition: imsz.h:56
IMSZ_BMP
@ IMSZ_BMP
Windows Bitmap, both for Windows 2.0 (BITMAPCOREHEADER) and for newer versions (BITMAPINFOHEADER).
Definition: imsz.h:63
imsz_from_pathw
int imsz_from_pathw(const wchar_t *path, ImInfo *info_ptr)
imsz_from_file
int imsz_from_file(FILE *stream, ImInfo *info_ptr)
IMSZ_WEBP
@ IMSZ_WEBP
WebP files. Supported sub-formats: VP8, VP8L, VP8X.
Definition: imsz.h:65
IMSZ_AVIF
@ IMSZ_AVIF
AV1 Image File Format.
Definition: imsz.h:70
IMSZ_ICO
@ IMSZ_ICO
ICO files can contain multiple images. This returns the dimensions of the biggest image in the file.
Definition: imsz.h:69
imsz_from_handle
int imsz_from_handle(HANDLE hnd, ImInfo *info_ptr)