Graphviz 14.0.0~dev.20250919.1031
Loading...
Searching...
No Matches
gv_math.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <assert.h>
8#include <float.h>
9#include <limits.h>
10#include <stdbool.h>
11#include <stddef.h>
12#include <string.h>
13
15static inline int fcmp(double a, double b) {
16 if (a < b) {
17 return -1;
18 }
19 if (a > b) {
20 return 1;
21 }
22 return 0;
23}
24
26static inline int imax(int a, int b) { return a > b ? a : b; }
27
29static inline size_t zmax(size_t a, size_t b) { return a > b ? a : b; }
30
32static inline int imin(int a, int b) { return a < b ? a : b; }
33
48static inline bool is_exactly_equal(double a, double b) {
49 return memcmp(&a, &b, sizeof(a)) == 0;
50}
51
67static inline bool is_exactly_zero(double v) { return is_exactly_equal(v, 0); }
68
76static inline int scale_clamp(int original, double scale) {
77 assert(original >= 0);
78
79 if (scale < 0) {
80 return 0;
81 }
82
83 if (scale > 1 && original > INT_MAX / scale) {
84 return INT_MAX;
85 }
86
87 return (int)(original * scale);
88}
89
91enum { BYTES_PER_PIXEL = 4 };
92
109static inline void argb2rgba(size_t width, size_t height, unsigned char *data) {
110 assert(data != NULL || (width == 0 && height == 0));
111
112 // indices to color bytes in each format
113 enum { Ba = 0, Ga = 1, Ra = 2, Aa = 3 };
114 enum { Rb = 0, Gb = 1, Bb = 2, Ab = 3 };
115
116 for (size_t y = 0; y < height; ++y) {
117 for (size_t x = 0; x < width; ++x) {
118 const unsigned char red = data[Ra];
119 const unsigned char blue = data[Ba];
120 data[Rb] = red;
121 data[Bb] = blue;
122 data += BYTES_PER_PIXEL;
123 }
124 }
125}
126
134#define SWAP(a, b) \
135 do { \
136 /* trigger a -Wcompare-distinct-pointer-types compiler warning if `a` */ \
137 /* and `b` have differing types */ \
138 (void)((a) == (b)); \
139 \
140 /* Swap their targets. Contemporary compilers will optimize the `memcpy`s \
141 * into direct writes for primitive types. \
142 */ \
143 char tmp_[sizeof(*(a))]; \
144 memcpy(tmp_, (a), sizeof(*(a))); \
145 *(a) = *(b); \
146 memcpy((b), tmp_, sizeof(*(b))); \
147 } while (0)
148
158static inline float d2f(double v) {
159 if (v > FLT_MAX) {
160 return FLT_MAX;
161 }
162 if (v < -FLT_MAX) {
163 return -FLT_MAX;
164 }
165 return (float)v;
166}
static pointf scale(double c, pointf p)
Definition geomprocs.h:155
node NULL
Definition grammar.y:181
static int scale_clamp(int original, double scale)
scale up or down a non-negative integer, clamping to [0, INT_MAX]
Definition gv_math.h:76
static bool is_exactly_zero(double v)
is a value precisely 0.0?
Definition gv_math.h:67
static bool is_exactly_equal(double a, double b)
are two values precisely the same?
Definition gv_math.h:48
static int imin(int a, int b)
minimum of two integers
Definition gv_math.h:32
static int fcmp(double a, double b)
comparator for doubles
Definition gv_math.h:15
static size_t zmax(size_t a, size_t b)
maximum of two sizes
Definition gv_math.h:29
@ BYTES_PER_PIXEL
Definition gv_math.h:91
static void argb2rgba(size_t width, size_t height, unsigned char *data)
Definition gv_math.h:109
static int imax(int a, int b)
maximum of two integers
Definition gv_math.h:26
static float d2f(double v)
Definition gv_math.h:156