Skip to content

Commit

Permalink
Move histogram calculation to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumix committed Mar 21, 2017
1 parent 30e4651 commit 2462516
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ OBJS = $(addsuffix .o,${BINS})
.PHONY: all
all: $(OUT_DIR) ${BINS}

${BINS}: %: $(OUT_DIR)/png_wrapper.o %.o
${BINS}: %: $(OUT_DIR)/png_wrapper.o $(OUT_DIR)/histogram.o %.o
$(CC) -o $@ $^ -L $(LDIR) $(LIBS)

.SECONDEXPANSION:
Expand All @@ -21,6 +21,9 @@ ${OBJS}: $(OUT_DIR)/%.o: %/$$*.c
$(OUT_DIR)/png_wrapper.o: png_wrapper.c
$(CC) -c -o $@ $< -I . -I $(IDIR) $(CFLAGS)

$(OUT_DIR)/histogram.o: histogram.c
$(CC) -c -o $@ $< -I . -I $(IDIR) $(CFLAGS)

$(OUT_DIR):
mkdir -p $(OUT_DIR)

Expand Down
34 changes: 34 additions & 0 deletions histogram.c
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;
}
}
}
11 changes: 11 additions & 0 deletions histogram.h
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
36 changes: 1 addition & 35 deletions lab4/lab4.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
#include <stdlib.h>
#include "png_wrapper.h"

enum { HIST_WIDTH = 512, HIST_HEIGHT = 256, MAX_COLOR = 255 };

static 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;
}
}
}
#include "histogram.h"

void precalculate(unsigned char *lut, size_t s1, size_t s2)
{
Expand Down

0 comments on commit 2462516

Please sign in to comment.