-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move histogram calculation to a separate file
- Loading branch information
Showing
4 changed files
with
50 additions
and
36 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "histogram.h" | ||
|
||
void histogram(struct Image in, struct Image out) | ||
{ | ||
size_t pixels_count[MAX_COLOR + 1] = { }; | ||
|
||
for (size_t y = 0; y < in.height; y++) | ||
for (size_t x = 0; x < in.width; x++) | ||
pixels_count[in.pixels[y][x]]++; | ||
|
||
size_t max_count = 0; | ||
for (size_t c = 0; c <= MAX_COLOR; c++) | ||
if (pixels_count[c] > max_count) | ||
max_count = pixels_count[c]; | ||
|
||
double k = HIST_HEIGHT / (double) max_count; | ||
|
||
for (size_t y = 0; y < HIST_HEIGHT; y++) | ||
for (size_t x = 0; x < HIST_WIDTH; x++) | ||
out.pixels[y][x] = 0; | ||
|
||
for (size_t c = 0; c <= MAX_COLOR; c++) | ||
{ | ||
size_t x = c * 2; | ||
size_t h = pixels_count[c] * k + 0.5; | ||
if (h > HIST_HEIGHT) h = HIST_HEIGHT; | ||
|
||
for (size_t y = 0; y < h; y++) | ||
{ | ||
out.pixels[HIST_HEIGHT - y - 1][x] = 255; | ||
out.pixels[HIST_HEIGHT - y - 1][x + 1] = 222; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef HISTOGRAM_H | ||
#define HISTOGRAM_H | ||
|
||
#include <stdlib.h> | ||
#include "png_wrapper.h" | ||
|
||
enum { HIST_WIDTH = 512, HIST_HEIGHT = 256, MAX_COLOR = 255 }; | ||
|
||
void histogram(struct Image in, struct Image out); | ||
|
||
#endif |
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