Graphviz 13.0.0~dev.20250121.0651
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 <limits.h>
9#include <stdbool.h>
10#include <stddef.h>
11#include <string.h>
12
14static inline int fcmp(double a, double b) {
15 if (a < b) {
16 return -1;
17 }
18 if (a > b) {
19 return 1;
20 }
21 return 0;
22}
23
25static inline int imax(int a, int b) { return a > b ? a : b; }
26
28static inline int imin(int a, int b) { return a < b ? a : b; }
29
44static inline bool is_exactly_equal(double a, double b) {
45 return memcmp(&a, &b, sizeof(a)) == 0;
46}
47
63static inline bool is_exactly_zero(double v) { return is_exactly_equal(v, 0); }
64
72static inline int scale_clamp(int original, double scale) {
73 assert(original >= 0);
74
75 if (scale < 0) {
76 return 0;
77 }
78
79 if (scale > 1 && original > INT_MAX / scale) {
80 return INT_MAX;
81 }
82
83 return (int)(original * scale);
84}
85
87enum { BYTES_PER_PIXEL = 4 };
88
105static inline void argb2rgba(size_t width, size_t height, unsigned char *data) {
106 assert(data != NULL || (width == 0 && height == 0));
107
108 // indices to color bytes in each format
109 enum { Ba = 0, Ga = 1, Ra = 2, Aa = 3 };
110 enum { Rb = 0, Gb = 1, Bb = 2, Ab = 3 };
111
112 for (size_t y = 0; y < height; ++y) {
113 for (size_t x = 0; x < width; ++x) {
114 const unsigned char red = data[Ra];
115 const unsigned char blue = data[Ba];
116 data[Rb] = red;
117 data[Bb] = blue;
119 }
120 }
121}
static pointf scale(double c, pointf p)
Definition geomprocs.h:155
node NULL
Definition grammar.y:163
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:72
static bool is_exactly_zero(double v)
is a value precisely 0.0?
Definition gv_math.h:63
static bool is_exactly_equal(double a, double b)
are two values precisely the same?
Definition gv_math.h:44
static int imin(int a, int b)
minimum of two integers
Definition gv_math.h:28
static int fcmp(double a, double b)
comparator for doubles
Definition gv_math.h:14
@ BYTES_PER_PIXEL
Definition gv_math.h:87
static void argb2rgba(size_t width, size_t height, unsigned char *data)
Definition gv_math.h:105
static int imax(int a, int b)
maximum of two integers
Definition gv_math.h:25
Definition legal.c:50