Skip to content

Commit

Permalink
Merge pull request #726 from ckuethe/histogram_safety
Browse files Browse the repository at this point in the history
make histogram parser more robust
  • Loading branch information
EricClaeys authored Oct 24, 2021
2 parents 9021720 + b60a406 commit 0d0d465
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <sys/time.h>
#include <sys/stat.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
Expand Down Expand Up @@ -356,6 +357,10 @@ int bytesPerPixel(ASI_IMG_TYPE imageType) {
// As a workaround, our histogram code replaces ZWO's code auto-exposure mechanism.
// We look at the mean brightness of an X by X rectangle in image, and adjust exposure based on that.

// FIXME prevent this from misbehaving when unreasonable settings are given,
// eg. box size 0x0, box size WxW, box crosses image edge, ... basically
// anything that would read/write out-of-bounds

void computeHistogram(unsigned char *imageBuffer, int width, int height, ASI_IMG_TYPE imageType, int *histogram)
{
int h, i;
Expand Down Expand Up @@ -1228,15 +1233,29 @@ const char *locale = DEFAULT_LOCALE;
if (argumentsQuoted)
{
sscanf(argv[++i], "%d %d %f %f", &histogramBoxSizeX, &histogramBoxSizeY, &histogramBoxPercentFromLeft, &histogramBoxPercentFromTop);
histogramBoxPercentFromLeft /= 100; // user enters 0-100
histogramBoxPercentFromTop /= 100;
}
else
{
histogramBoxSizeX = atoi(argv[++i]);
histogramBoxSizeY = atoi(argv[++i]);
histogramBoxPercentFromLeft = (float)atoi(argv[++i]) / 100; // user enters 0-100
histogramBoxPercentFromTop = (float)atoi(argv[++i]) / 100; // user enters 0-100
histogramBoxPercentFromLeft = atof(argv[++i]);
histogramBoxPercentFromTop = atof(argv[++i]);
}

// scale user-input 0-100 to 0.0-1.0
histogramBoxPercentFromLeft /= 100;
histogramBoxPercentFromTop /= 100;

// avoid histogram crash if the parser returns weird
if (histogramBoxSizeX < 1 || histogramBoxSizeY < 1 || histogramBoxSizeX > 640 || histogramBoxSizeY > 640) {
histogramBoxSizeX = histogramBoxSizeY = 256;
fprintf(stderr, "weird histogram box dimensions; resetting to 256x256\n");
}
if (isnan(histogramBoxPercentFromLeft) || isnan(histogramBoxPercentFromTop) ||
histogramBoxPercentFromLeft < 0.2 || histogramBoxPercentFromTop < 0.2 ||
histogramBoxPercentFromLeft > 0.8 || histogramBoxPercentFromTop > 0.8) {
histogramBoxPercentFromLeft = histogramBoxPercentFromTop = 0.5;
fprintf(stderr, "weird histogram box position; resetting to (50%%,50%%)\n");
}
}
else if (strcmp(argv[i], "-showhistogrambox") == 0)
Expand Down

0 comments on commit 0d0d465

Please sign in to comment.