Browse Source

Use PascalCase for constants in gfx

June 3 months ago
parent
commit
defede1691
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
3 changed files with 95 additions and 82 deletions
  1. 7
    3
      gfx/brot.c
  2. 4
    4
      gfx/cocoa.m
  3. 84
    75
      gfx/gfxx.c

+ 7
- 3
gfx/brot.c View File

@@ -28,8 +28,12 @@
28 28
 
29 29
 #include "gfx.h"
30 30
 
31
-#define RGB(r, g, b) ((uint32_t)(r) << 16 | (uint32_t)(g) << 8 | (uint32_t)(b))
32
-#define GRAY(n) RGB(n, n, n)
31
+static uint32_t rgb(uint8_t r, uint8_t g, uint8_t b) {
32
+	return (uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b;
33
+}
34
+static uint32_t gray(uint8_t n) {
35
+	return rgb(n, n, n);
36
+}
33 37
 
34 38
 static double absSq(double complex z) {
35 39
 	return creal(z) * creal(z) + cimag(z) * cimag(z);
@@ -70,7 +74,7 @@ static void sample(uint32_t *buf, size_t width, size_t height) {
70 74
 
71 75
 static void color(uint32_t *buf, size_t width, size_t height) {
72 76
 	for (size_t i = 0; i < width * height; ++i) {
73
-		buf[i] = GRAY(255 * buf[i] / samples / samples / depth);
77
+		buf[i] = gray(255 * buf[i] / samples / samples / depth);
74 78
 	}
75 79
 }
76 80
 

+ 4
- 4
gfx/cocoa.m View File

@@ -23,8 +23,6 @@
23 23
 
24 24
 #import "gfx.h"
25 25
 
26
-#define UNUSED __attribute__((unused))
27
-
28 26
 @interface BufferView : NSView {
29 27
 	size_t bufSize;
30 28
 	uint32_t *buf;
@@ -61,7 +59,8 @@
61 59
 	[self draw];
62 60
 }
63 61
 
64
-- (void) drawRect: (NSRect) UNUSED dirtyRect {
62
+- (void) drawRect: (NSRect) dirtyRect {
63
+	(void)dirtyRect;
65 64
 	NSSize size = [self frame].size;
66 65
 	CGContextRef ctx = [[NSGraphicsContext currentContext] CGContext];
67 66
 	CGImageRef image = CGImageCreate(
@@ -106,7 +105,8 @@
106 105
 
107 106
 @implementation Delegate
108 107
 - (BOOL) applicationShouldTerminateAfterLastWindowClosed:
109
-	(NSApplication *) UNUSED sender {
108
+	(NSApplication *) sender {
109
+	(void)sender;
110 110
 	return YES;
111 111
 }
112 112
 @end

+ 84
- 75
gfx/gfxx.c View File

@@ -34,27 +34,36 @@
34 34
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
35 35
 #define MASK(b) ((1 << (b)) - 1)
36 36
 
37
-#define RGB(r, g, b) ((uint32_t)(r) << 16 | (uint32_t)(g) << 8 | (uint32_t)(b))
38
-#define GRAY(n) RGB(n, n, n)
37
+static uint32_t rgb(uint8_t r, uint8_t g, uint8_t b) {
38
+	return (uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b;
39
+}
40
+static uint32_t gray(uint8_t n) {
41
+	return rgb(n, n, n);
42
+}
39 43
 
40 44
 static enum {
41
-	COLOR_INDEXED,
42
-	COLOR_GRAYSCALE,
43
-	COLOR_RGB,
44
-	COLOR__COUNT,
45
-} space = COLOR_RGB;
46
-static const char *COLOR__STR[COLOR__COUNT] = { "indexed", "grayscale", "rgb" };
45
+	ColorIndexed,
46
+	ColorGrayscale,
47
+	ColorRGB,
48
+	ColorCount,
49
+} space = ColorRGB;
50
+static const char *ColorStr[ColorCount] = { "indexed", "grayscale", "rgb" };
47 51
 static uint32_t palette[256];
48 52
 
49 53
 static enum {
50
-	ENDIAN_LITTLE,
51
-	ENDIAN_BIG,
54
+	EndianLittle,
55
+	EndianBig,
52 56
 } byteOrder, bitOrder;
53 57
 
54
-enum { PAD, R, G, B };
58
+enum { Pad, R, G, B };
55 59
 static uint8_t bits[4] = { 8, 8, 8, 8 };
56
-#define BITS_COLOR (bits[R] + bits[G] + bits[B])
57
-#define BITS_TOTAL (bits[PAD] + BITS_COLOR)
60
+
61
+static uint8_t bitsColor(void) {
62
+	return bits[R] + bits[G] + bits[B];
63
+}
64
+static uint8_t bitsTotal(void) {
65
+	return bits[Pad] + bitsColor();
66
+}
58 67
 
59 68
 static size_t offset;
60 69
 static size_t width = 16;
@@ -76,22 +85,22 @@ int init(int argc, char *argv[]) {
76 85
 		switch (opt) {
77 86
 			break; case 'c':
78 87
 				switch (optarg[0]) {
79
-					break; case 'i': space = COLOR_INDEXED;
80
-					break; case 'g': space = COLOR_GRAYSCALE;
81
-					break; case 'r': space = COLOR_RGB;
88
+					break; case 'i': space = ColorIndexed;
89
+					break; case 'g': space = ColorGrayscale;
90
+					break; case 'r': space = ColorRGB;
82 91
 					break; default: return EX_USAGE;
83 92
 				}
84 93
 			break; case 'p': pal = optarg;
85 94
 			break; case 'e':
86 95
 				switch (optarg[0]) {
87
-					break; case 'l': byteOrder = ENDIAN_LITTLE;
88
-					break; case 'b': byteOrder = ENDIAN_BIG;
96
+					break; case 'l': byteOrder = EndianLittle;
97
+					break; case 'b': byteOrder = EndianBig;
89 98
 					break; default: return EX_USAGE;
90 99
 				}
91 100
 			break; case 'E':
92 101
 				switch (optarg[0]) {
93
-					break; case 'l': bitOrder = ENDIAN_LITTLE;
94
-					break; case 'b': bitOrder = ENDIAN_BIG;
102
+					break; case 'l': bitOrder = EndianLittle;
103
+					break; case 'b': bitOrder = EndianBig;
95 104
 					break; default: return EX_USAGE;
96 105
 				}
97 106
 			break; case 'b': {
@@ -162,10 +171,10 @@ static void formatOptions(void) {
162 171
 	snprintf(
163 172
 		options, sizeof(options),
164 173
 		"gfxx -c %s -e%c -E%c -b %hhu%hhu%hhu%hhu -n 0x%zX %s%s-w %zu -z %zu",
165
-		COLOR__STR[space],
174
+		ColorStr[space],
166 175
 		"lb"[byteOrder],
167 176
 		"lb"[bitOrder],
168
-		bits[PAD], bits[R], bits[G], bits[B],
177
+		bits[Pad], bits[R], bits[G], bits[B],
169 178
 		offset,
170 179
 		flip ? "-f " : "",
171 180
 		mirror ? "-m " : "",
@@ -247,35 +256,35 @@ static uint8_t interp(uint8_t b, uint32_t n) {
247 256
 	return n * MASK(8) / MASK(b);
248 257
 }
249 258
 
250
-static uint32_t interpolate(uint32_t rgb) {
259
+static uint32_t interpolate(uint32_t orig) {
251 260
 	uint32_t r, g, b;
252
-	if (bitOrder == ENDIAN_LITTLE) {
253
-		b = rgb & MASK(bits[B]);
254
-		g = (rgb >>= bits[B]) & MASK(bits[G]);
255
-		r = (rgb >>= bits[G]) & MASK(bits[R]);
261
+	if (bitOrder == EndianLittle) {
262
+		b = orig & MASK(bits[B]);
263
+		g = (orig >>= bits[B]) & MASK(bits[G]);
264
+		r = (orig >>= bits[G]) & MASK(bits[R]);
256 265
 	} else {
257
-		r = rgb & MASK(bits[R]);
258
-		g = (rgb >>= bits[R]) & MASK(bits[G]);
259
-		b = (rgb >>= bits[G]) & MASK(bits[B]);
266
+		r = orig & MASK(bits[R]);
267
+		g = (orig >>= bits[R]) & MASK(bits[G]);
268
+		b = (orig >>= bits[G]) & MASK(bits[B]);
260 269
 	}
261
-	return RGB(interp(bits[R], r), interp(bits[G], g), interp(bits[B], b));
270
+	return rgb(interp(bits[R], r), interp(bits[G], g), interp(bits[B], b));
262 271
 }
263 272
 
264 273
 static void drawBits(struct Iter *it) {
265 274
 	for (size_t i = offset; i < size; ++i) {
266
-		for (uint8_t b = 0; b < 8; b += BITS_TOTAL) {
275
+		for (uint8_t b = 0; b < 8; b += bitsTotal()) {
267 276
 			uint8_t n;
268
-			if (byteOrder == ENDIAN_BIG) {
269
-				n = data[i] >> (8 - BITS_TOTAL - b) & MASK(BITS_TOTAL);
277
+			if (byteOrder == EndianBig) {
278
+				n = data[i] >> (8 - bitsTotal() - b) & MASK(bitsTotal());
270 279
 			} else {
271
-				n = data[i] >> b & MASK(BITS_TOTAL);
280
+				n = data[i] >> b & MASK(bitsTotal());
272 281
 			}
273 282
 
274
-			if (space == COLOR_INDEXED) {
283
+			if (space == ColorIndexed) {
275 284
 				put(it, palette[n]);
276
-			} else if (space == COLOR_GRAYSCALE) {
277
-				put(it, GRAY(interp(BITS_COLOR, n & MASK(BITS_COLOR))));
278
-			} else if (space == COLOR_RGB) {
285
+			} else if (space == ColorGrayscale) {
286
+				put(it, gray(interp(bitsColor(), n & MASK(bitsColor()))));
287
+			} else if (space == ColorRGB) {
279 288
 				put(it, interpolate(n));
280 289
 			}
281 290
 
@@ -285,19 +294,19 @@ static void drawBits(struct Iter *it) {
285 294
 }
286 295
 
287 296
 static void drawBytes(struct Iter *it) {
288
-	uint8_t bytes = (BITS_TOTAL + 7) / 8;
297
+	uint8_t bytes = (bitsTotal() + 7) / 8;
289 298
 	for (size_t i = offset; i + bytes <= size; i += bytes) {
290 299
 		uint32_t n = 0;
291 300
 		for (size_t b = 0; b < bytes; ++b) {
292 301
 			n <<= 8;
293
-			n |= (byteOrder == ENDIAN_BIG) ? data[i + b] : data[i + bytes - b - 1];
302
+			n |= (byteOrder == EndianBig) ? data[i + b] : data[i + bytes - b - 1];
294 303
 		}
295 304
 
296
-		if (space == COLOR_INDEXED) {
305
+		if (space == ColorIndexed) {
297 306
 			put(it, palette[n & 0xFF]);
298
-		} else if (space == COLOR_GRAYSCALE) {
299
-			put(it, GRAY(interp(BITS_COLOR, n & MASK(BITS_COLOR))));
300
-		} else if (space == COLOR_RGB) {
307
+		} else if (space == ColorGrayscale) {
308
+			put(it, gray(interp(bitsColor(), n & MASK(bitsColor()))));
309
+		} else if (space == ColorRGB) {
301 310
 			put(it, interpolate(n));
302 311
 		}
303 312
 
@@ -359,22 +368,22 @@ static void pngDump(uint32_t *src, size_t srcWidth, size_t srcHeight) {
359 368
 	outOpen("png");
360 369
 	if (!out.file) return;
361 370
 
362
-	const uint8_t SIGNATURE[] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n' };
363
-	const uint8_t HEADER[] = { 8, 2, 0, 0, 0 }; // 8-bit truecolor
364
-	const char SOFTWARE[] = "Software";
371
+	const uint8_t Signature[8] = "\x89PNG\r\n\x1A\n";
372
+	const uint8_t Header[] = { 8, 2, 0, 0, 0 }; // 8-bit truecolor
373
+	const char Software[] = "Software";
365 374
 	formatOptions();
366 375
 	uint8_t sbit[3] = { MAX(bits[R], 1), MAX(bits[G], 1), MAX(bits[B], 1) };
367 376
 
368
-	pngWrite(SIGNATURE, sizeof(SIGNATURE));
377
+	pngWrite(Signature, sizeof(Signature));
369 378
 
370
-	pngChunk("IHDR", 4 + 4 + sizeof(HEADER));
379
+	pngChunk("IHDR", 4 + 4 + sizeof(Header));
371 380
 	pngUint(srcWidth);
372 381
 	pngUint(srcHeight);
373
-	pngWrite(HEADER, sizeof(HEADER));
382
+	pngWrite(Header, sizeof(Header));
374 383
 	pngUint(crc);
375 384
 
376
-	pngChunk("tEXt", sizeof(SOFTWARE) + strlen(options));
377
-	pngWrite(SOFTWARE, sizeof(SOFTWARE));
385
+	pngChunk("tEXt", sizeof(Software) + strlen(options));
386
+	pngWrite(Software, sizeof(Software));
378 387
 	pngWrite(options, strlen(options));
379 388
 	pngUint(crc);
380 389
 
@@ -394,21 +403,21 @@ static void pngDump(uint32_t *src, size_t srcWidth, size_t srcHeight) {
394 403
 }
395 404
 
396 405
 static enum {
397
-	DUMP_NONE,
398
-	DUMP_ONE,
399
-	DUMP_ALL,
406
+	DumpNone,
407
+	DumpOne,
408
+	DumpAll,
400 409
 } dump;
401 410
 
402 411
 void draw(uint32_t *buf, size_t bufWidth, size_t bufHeight) {
403 412
 	memset(buf, 0, 4 * bufWidth * bufHeight);
404 413
 	struct Iter it = iter(buf, bufWidth, bufHeight);
405
-	if (BITS_TOTAL >= 8) {
414
+	if (bitsTotal() >= 8) {
406 415
 		drawBytes(&it);
407 416
 	} else {
408 417
 		drawBits(&it);
409 418
 	}
410 419
 	if (dump) pngDump(buf, bufWidth, bufHeight);
411
-	if (dump == DUMP_ONE) dump = DUMP_NONE;
420
+	if (dump == DumpOne) dump = DumpNone;
412 421
 }
413 422
 
414 423
 static void palSample(void) {
@@ -429,7 +438,7 @@ static void palDump(void) {
429 438
 	if (error) err(EX_IOERR, "%s", out.path);
430 439
 }
431 440
 
432
-static const uint8_t PRESETS[][4] = {
441
+static const uint8_t Presets[][4] = {
433 442
 	{ 0, 0, 1, 0 },
434 443
 	{ 0, 1, 1, 0 },
435 444
 	{ 1, 1, 1, 1 },
@@ -441,14 +450,14 @@ static const uint8_t PRESETS[][4] = {
441 450
 	{ 0, 8, 8, 8 },
442 451
 	{ 8, 8, 8, 8 },
443 452
 };
444
-#define PRESETS_LEN (sizeof(PRESETS) / sizeof(PRESETS[0]))
453
+static const size_t PresetsLen = sizeof(Presets) / sizeof(Presets[0]);
445 454
 
446
-static uint8_t preset = PRESETS_LEN - 1;
455
+static uint8_t preset = PresetsLen - 1;
447 456
 static void setPreset(void) {
448
-	bits[PAD] = PRESETS[preset][PAD];
449
-	bits[R] = PRESETS[preset][R];
450
-	bits[G] = PRESETS[preset][G];
451
-	bits[B] = PRESETS[preset][B];
457
+	bits[Pad] = Presets[preset][Pad];
458
+	bits[R] = Presets[preset][R];
459
+	bits[G] = Presets[preset][G];
460
+	bits[B] = Presets[preset][B];
452 461
 }
453 462
 
454 463
 static void setBit(char in) {
@@ -458,21 +467,21 @@ static void setBit(char in) {
458 467
 }
459 468
 
460 469
 bool input(char in) {
461
-	size_t pixel = (BITS_TOTAL + 7) / 8;
462
-	size_t row = width * BITS_TOTAL / 8;
470
+	size_t pixel = (bitsTotal() + 7) / 8;
471
+	size_t row = width * bitsTotal() / 8;
463 472
 	switch (in) {
464 473
 		break; case 'q': return false;
465
-		break; case 'x': dump = DUMP_ONE;
466
-		break; case 'X': dump ^= DUMP_ALL;
474
+		break; case 'x': dump = DumpOne;
475
+		break; case 'X': dump ^= DumpAll;
467 476
 		break; case 'o': formatOptions(); printf("%s\n", options);
468
-		break; case '[': if (!space--) space = COLOR__COUNT - 1;
469
-		break; case ']': if (++space == COLOR__COUNT) space = 0;
477
+		break; case '[': if (!space--) space = ColorCount - 1;
478
+		break; case ']': if (++space == ColorCount) space = 0;
470 479
 		break; case 'p': palSample();
471 480
 		break; case 'P': palDump();
472
-		break; case '{': if (!preset--) preset = PRESETS_LEN - 1; setPreset();
473
-		break; case '}': if (++preset == PRESETS_LEN) preset = 0; setPreset();
474
-		break; case 'e': byteOrder ^= ENDIAN_BIG;
475
-		break; case 'E': bitOrder ^= ENDIAN_BIG;
481
+		break; case '{': if (!preset--) preset = PresetsLen - 1; setPreset();
482
+		break; case '}': if (++preset == PresetsLen) preset = 0; setPreset();
483
+		break; case 'e': byteOrder ^= EndianBig;
484
+		break; case 'E': bitOrder ^= EndianBig;
476 485
 		break; case 'h': if (offset) offset--;
477 486
 		break; case 'j': offset += pixel;
478 487
 		break; case 'k': if (offset >= pixel) offset -= pixel;

Loading…
Cancel
Save