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 } ImFormat;
81 
88 #define IMSZ_INIT { .width = (uint64_t)0, .height = (uint64_t)0, .format = 0 }
89 
91 typedef struct ImInfo {
92  uint64_t width;
93  uint64_t height;
94 
103  unsigned int format;
104 } ImInfo;
105 
111 IMSZ_EXPORT int imsz_from_path(const char *path, ImInfo *info_ptr);
112 
119 IMSZ_EXPORT int imsz_from_buffer(const void *buf, size_t len, ImInfo *info_ptr);
120 
126 IMSZ_EXPORT int imsz_from_fd(int fd, ImInfo *info_ptr);
127 
132 IMSZ_EXPORT const char *imsz_format_name(unsigned int format);
133 
134 #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) || defined(__DOXYGEN__)
135  IMSZ_EXPORT int imsz_from_pathw(const wchar_t *path, ImInfo *info_ptr);
141 
147  IMSZ_EXPORT int imsz_from_handle(HANDLE hnd, ImInfo *info_ptr);
148 
153  IMSZ_EXPORT const wchar_t *imsz_format_namew(unsigned int format);
154 
155  #define imsz_from_file(fp, info_ptr) imsz_from_fd(_fileno((fp)), (info_ptr))
156 
157  #define imsz_2_(arg1, arg2) \
158  _Generic((arg1), \
159  wchar_t*: imsz_from_pathw((const wchar_t*)(arg1), (arg2)), \
160  const wchar_t*: imsz_from_pathw((const wchar_t*)(arg1), (arg2)), \
161  char*: imsz_from_path((const char*)(arg1), (arg2)), \
162  const char*: imsz_from_path((const char*)(arg1), (arg2)), \
163  FILE*: imsz_from_file((FILE*)(arg1), (arg2)), \
164  HANDLE: imsz_from_handle((HANDLE)(arg1), (arg2)), \
165  int: imsz_from_fd((intptr_t)(arg1), (arg2)) \
166  )
167 #else
168  #define imsz_from_file(fp, info_ptr) imsz_from_fd(fileno((fp)), (info_ptr))
169 
170  #define imsz_2_(arg1, arg2) \
171  _Generic((arg1), \
172  char*: imsz_from_path((const char*)(arg1), (arg2)), \
173  const char*: imsz_from_path((const char*)(arg1), (arg2)), \
174  FILE*: imsz_from_file((FILE*)(arg1), (arg2)), \
175  int: imsz_from_fd((intptr_t)(arg1), (arg2)) \
176  )
177 #endif
178 
185 
186 #define imsz_3_(arg1, arg2, arg3) \
187  imsz_from_buffer((arg1), (arg2), (arg3))
188 
189 #define imsz_va_dispatch_(arg1, arg2, arg3, arg4, ...) arg4
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 #if defined(__cplusplus) || defined(__DOXYGEN__)
196 
200 inline int imsz(const char *path, ImInfo *info_ptr) {
201  return imsz_from_path(path, info_ptr);
202 }
203 
207 inline int imsz(const void *buf, size_t len, ImInfo *info_ptr) {
208  return imsz_from_buffer(buf, len, info_ptr);
209 }
210 
214 inline int imsz(int fd, ImInfo *info_ptr) {
215  return imsz_from_fd(fd, info_ptr);
216 }
217 
221 inline int imsz(FILE *file, ImInfo *info_ptr) {
222  return imsz_from_file(file, info_ptr);
223 }
224 
225 #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) || defined(__DOXYGEN__)
226 inline int imsz(const wchar_t *path, ImInfo *info_ptr) {
230  return imsz_from_pathw(path, info_ptr);
231 }
232 
236 inline int imsz(HANDLE hnd, ImInfo *info_ptr) {
237  return imsz_from_handle(hnd, info_ptr);
238 }
239 #endif
240 #endif
241 
242 #if !defined(__cplusplus) || defined(__DOXYGEN__)
243 
261  #define imsz(...) imsz_va_dispatch_(__VA_ARGS__, imsz_3_, imsz_2_, imsz_error_)(__VA_ARGS__)
262 
263 #endif
264 
265 
266 #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_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:92
imsz
int imsz(const char *path, ImInfo *info_ptr)
Alias of imsz_from_path()
Definition: imsz.h:200
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_from_file
#define imsz_from_file(fp, info_ptr)
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:93
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:91
ImInfo::format
unsigned int format
Values from ImFormat.
Definition: imsz.h:103
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_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)