Created
December 25, 2019 20:04
-
-
Save nicknapoli82/8f4dbf09b1d2f0951968fc295867c6c6 to your computer and use it in GitHub Desktop.
Create Index of Pixels combined rgb values into a unit and quantify number pixels found in an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
struct Pixel { | |
unsigned char r; | |
unsigned char g; | |
unsigned char b; | |
}; | |
struct Pixel_Total { | |
int value; | |
int quantity; | |
}; | |
struct Pixel_Table { | |
struct Pixel_Total* totals; | |
int length; | |
}; | |
void find_or_realloc(struct Pixel_Table*, struct Pixel); | |
int main(void) { | |
// Make some test data | |
struct Pixel pixel1 = {1, 2, 3}; | |
struct Pixel pixel2 = {4, 5, 6}; | |
struct Pixel pixel3 = {1, 2, 3}; | |
struct Pixel pixel4 = {10, 11, 12}; | |
struct Pixel pixel5 = {4, 5, 6}; | |
// get some memory | |
struct Pixel_Table table = {calloc(2, sizeof(struct Pixel_Total)), 2}; | |
find_or_realloc(&table, pixel1); | |
find_or_realloc(&table, pixel2); | |
find_or_realloc(&table, pixel3); | |
find_or_realloc(&table, pixel4); | |
find_or_realloc(&table, pixel5); | |
// Check the data | |
printf("Pixel 1 rbg = {%i, %i, %i} | Unit Value = %i\n", | |
pixel1.r, pixel1.g, pixel1.b, pixel1.r + (pixel1.g << 8) + (pixel1.b << 16)); | |
printf("Pixel 2 rbg = {%i, %i, %i} | Unit Value = %i\n", | |
pixel2.r, pixel2.g, pixel2.b, pixel2.r + (pixel2.g << 8) + (pixel2.b << 16)); | |
printf("Pixel 3 rbg = {%i, %i, %i} | Unit Value = %i\n", | |
pixel3.r, pixel3.g, pixel3.b, pixel3.r + (pixel3.g << 8) + (pixel3.b << 16)); | |
printf("Pixel 4 rbg = {%i, %i, %i} | Unit Value = %i\n", | |
pixel4.r, pixel4.g, pixel4.b, pixel4.r + (pixel4.g << 8) + (pixel4.b << 16)); | |
printf("Pixel 5 rbg = {%i, %i, %i} | Unit Value = %i\n", | |
pixel5.r, pixel5.g, pixel5.b, pixel5.r + (pixel5.g << 8) + (pixel5.b << 16)); | |
printf("\nAnd now the table\n"); | |
printf("table 1 value = %i | table 1 quantity = %i\n", table.totals[0].value, table.totals[0].quantity); | |
printf("table 2 value = %i | table 2 quantity = %i\n", table.totals[1].value, table.totals[1].quantity); | |
printf("table 3 value = %i | table 3 quantity = %i\n", table.totals[2].value, table.totals[2].quantity); | |
printf("table 4 value = %i | table 4 quantity = %i\n", table.totals[3].value, table.totals[3].quantity); | |
// clean up | |
free(table.totals); | |
return 0; | |
} | |
void find_or_realloc(struct Pixel_Table* p_table, struct Pixel p) { | |
char found = 0; | |
struct Pixel_Table t = *p_table; | |
// new_value is the summation of all rgb values stuffed into one unit | |
int new_value = p.r + (p.g << 8) + (p.b << 16); | |
for(int i = 0; i < t.length; i++) { | |
if(t.totals[i].value == new_value || (new_value == 0 && t.totals[i].value == 0)) { | |
t.totals[i].quantity++; | |
found = 1; | |
break; | |
} | |
else if(t.totals[i].value == 0 && new_value != 0) { | |
t.totals[i].value = new_value; | |
t.totals[i].quantity++; | |
found = 1; | |
break; | |
} | |
} | |
// If we didn't find the value, or don't have space for a new pixel value | |
// get more memory | |
if(!found) { | |
int new_length = t.length + 2; | |
struct Pixel_Total* new_totals = calloc(new_length, sizeof(struct Pixel_Total)); | |
// Copy the old array to the new | |
memcpy(new_totals, t.totals, sizeof(struct Pixel_Total) * t.length); | |
free(p_table->totals); | |
p_table->totals = new_totals; | |
p_table->totals[t.length].value = new_value; | |
p_table->totals[t.length].quantity++; | |
p_table->length = new_length; | |
} | |
// this could return a bool so your program knows if this function was successful | |
// or maybe we have an issue with memory that fails. I didn't check if the memory | |
// allocation was successful, but meh. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment