Graphviz 13.1.2~dev.20250807.2324
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 int imin(int a, int b) { return a < b ? a : b; }
30
45static inline bool is_exactly_equal(double a, double b) {
46 return memcmp(&a, &b, sizeof(a)) == 0;
47}
48
64static inline bool is_exactly_zero(double v) { return is_exactly_equal(v, 0); }
65
73static inline int scale_clamp(int original, double scale) {
74 assert(original >= 0);
75
76 if (scale < 0) {
77 return 0;
78 }
79
80 if (scale > 1 && original > INT_MAX / scale) {
81 return INT_MAX;
82 }
83
84 return (int)(original * scale);
85}
86
88enum { BYTES_PER_PIXEL = 4 };
89
106static inline void argb2rgba(size_t width, size_t height, unsigned char *data) {
107 assert(data != NULL || (width == 0 && height == 0));
108
109 // indices to color bytes in each format
110 enum { Ba = 0, Ga = 1, Ra = 2, Aa = 3 };
111 enum { Rb = 0, Gb = 1, Bb = 2, Ab = 3 };
112
113 for (size_t y = 0; y < height; ++y) {
114 for (size_t x = 0; x < width; ++x) {
115 const unsigned char red = data[Ra];
116 const unsigned char blue = data[Ba];
117 data[Rb] = red;
118 data[Bb] = blue;
120 }
121 }
122}
123
131#define SWAP(a, b) \
132 do { \
133 /* trigger a -Wcompare-distinct-pointer-types compiler warning if `a` */ \
134 /* and `b` have differing types */ \
135 (void)((a) == (b)); \
136 \
137 /* Swap their targets. Contemporary compilers will optimize the `memcpy`s \
138 * into direct writes for primitive types. \
139 */ \
140 char tmp_[sizeof(*(a))]; \
141 memcpy(tmp_, (a), sizeof(*(a))); \
142 *(a) = *(b); \
143 memcpy((b), tmp_, sizeof(*(b))); \
144 } while (0)
145
155static inline float d2f(double v) {
156 if (v > FLT_MAX) {
157 return FLT_MAX;
158 }
159 if (v < -FLT_MAX) {
160 return -FLT_MAX;
161 }
162 return (float)v;
163}
static pointf scale(double c, pointf p)
Definition geomprocs.h:155
node NULL
Definition grammar.y:180
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:73
static bool is_exactly_zero(double v)
is a value precisely 0.0?
Definition gv_math.h:64
@ BYTES_PER_PIXEL
Definition gv_math.h:88
static bool is_exactly_equal(double a, double b)
are two values precisely the same?
Definition gv_math.h:45
static int imin(int a, int b)
minimum of two integers
Definition gv_math.h:29
static int fcmp(double a, double b)
comparator for doubles
Definition gv_math.h:15
static void argb2rgba(size_t width, size_t height, unsigned char *data)
Definition gv_math.h:106
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:153
Definition legal.c:50