Browse Source

Factor out pixelBits, pixelSize in glitch

June 3 months ago
parent
commit
fc1eb1c8ef
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
1 changed files with 18 additions and 13 deletions
  1. 18
    13
      bin/glitch.c

+ 18
- 13
bin/glitch.c View File

@@ -127,17 +127,25 @@ static struct PACKED {
127 127
 } header;
128 128
 static_assert(13 == sizeof(header), "header size");
129 129
 
130
-static size_t lineSize(void) {
130
+static size_t pixelBits(void) {
131 131
 	switch (header.color) {
132
-		case Grayscale:      return (header.width * 1 * header.depth + 7) / 8;
133
-		case Truecolor:      return (header.width * 3 * header.depth + 7) / 8;
134
-		case Indexed:        return (header.width * 1 * header.depth + 7) / 8;
135
-		case GrayscaleAlpha: return (header.width * 2 * header.depth + 7) / 8;
136
-		case TruecolorAlpha: return (header.width * 4 * header.depth + 7) / 8;
132
+		case Grayscale:      return 1 * header.depth;
133
+		case Truecolor:      return 3 * header.depth;
134
+		case Indexed:        return 1 * header.depth;
135
+		case GrayscaleAlpha: return 2 * header.depth;
136
+		case TruecolorAlpha: return 4 * header.depth;
137 137
 		default: abort();
138 138
 	}
139 139
 }
140 140
 
141
+static size_t pixelSize(void) {
142
+	return (pixelBits() + 7) / 8;
143
+}
144
+
145
+static size_t lineSize(void) {
146
+	return (header.width * pixelBits() + 7) / 8;
147
+}
148
+
141 149
 static size_t dataSize(void) {
142 150
 	return (1 + lineSize()) * header.height;
143 151
 }
@@ -346,14 +354,12 @@ static void scanlines(void) {
346 354
 }
347 355
 
348 356
 static struct Bytes origBytes(uint32_t y, size_t i) {
349
-	size_t pixelSize = lineSize() / header.width;
350
-	if (!pixelSize) pixelSize = 1;
351
-	bool a = (i >= pixelSize), b = (y > 0), c = (a && b);
357
+	bool a = (i >= pixelSize()), b = (y > 0), c = (a && b);
352 358
 	return (struct Bytes) {
353 359
 		.x = lines[y]->data[i],
354
-		.a = a ? lines[y]->data[i - pixelSize] : 0,
360
+		.a = a ? lines[y]->data[i - pixelSize()] : 0,
355 361
 		.b = b ? lines[y - 1]->data[i] : 0,
356
-		.c = c ? lines[y - 1]->data[i - pixelSize] : 0,
362
+		.c = c ? lines[y - 1]->data[i - pixelSize()] : 0,
357 363
 	};
358 364
 }
359 365
 
@@ -421,9 +427,8 @@ static void mirror(void) {
421 427
 }
422 428
 
423 429
 static void zeroX(void) {
424
-	size_t pixelSize = lineSize() / header.width;
425 430
 	for (uint32_t y = 0; y < header.height; ++y) {
426
-		memset(lines[y]->data, 0, pixelSize);
431
+		memset(lines[y]->data, 0, pixelSize());
427 432
 	}
428 433
 }
429 434
 

Loading…
Cancel
Save