Skip to content

Commit

Permalink
add max histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
elijahparker authored May 22, 2017
1 parent 478192f commit 42273f9
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/jpeg-lum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#include <math.h>

unsigned int size;
unsigned int histogram[256];
unsigned int histogram[3][256];
unsigned int width;
unsigned int height;
unsigned long count;
double luminance;
double pixel;
double clipped;
Expand Down Expand Up @@ -98,21 +99,21 @@ int read_jpeg_file(char *filename)
height = cinfo.output_height;
luminance = 0.0;
size = cinfo.output_width*cinfo.output_height*cinfo.num_components*sizeof(unsigned int);
memset(histogram, 0, sizeof(int)*256);
memset(histogram, 0, sizeof(int)*256*3);
row_pointer[0] = (unsigned char *)malloc(cinfo.output_width*cinfo.num_components);
unsigned long count = 0;
count = 0;
while (cinfo.output_scanline < cinfo.image_height) {
jpeg_read_scanlines( &cinfo, row_pointer, 1 );
for (i=0; i<cinfo.image_width;i+=cinfo.num_components) {
pixel = 0.0;
for(component=0;component<cinfo.num_components;component++) {
if(component < 2) {
if(component < 3) {
pixel = (double) row_pointer[0][i + component];
pixel = lum(pixel);
if(pixel > 4) clipped++;
luminance += pixel;
count++;
// histogram[(int)pixel]++;
histogram[component][(int)pixel]++;
}
}
}
Expand Down Expand Up @@ -140,8 +141,21 @@ Handle<Value> CreateObject(const FunctionCallbackInfo<Value>& info) {

Local<Object> obj = Object::New(isolate);
Local<Array> histArray = Array::New(isolate, 256);

unsigned int maxPeak = 0;
for (unsigned int i = 0; i < 256; i++) {
for(unsigned int j = 0; j < 3; j++) {
if(histogram[j][i] > maxPeak) maxPeak = histogram[j][i];
}
}

for (unsigned int i = 0; i < 256; i++) {
histArray->Set(i, Number::New(isolate, histogram[i]));
unsigned int max = 0;
for(unsigned int j = 0; j < 3; j++) {
if(histogram[j][i] > max) max = histogram[j][i];
}
max = (max*16)/((maxPeak*16)/256); // compress to 256 max, integer math for speed
histArray->Set(i, Number::New(isolate, max));
}

obj->Set(String::NewFromUtf8(isolate, "histogram", String::kInternalizedString), histArray);
Expand Down

0 comments on commit 42273f9

Please sign in to comment.