dlbeer/quirc

Remove C99 features: vla for Visual Studio and for loop initial declarations for gcc < 5

Closed this issue · 3 comments

In function otsu in identify.c file:

  • unsigned int histogram[HISTOGRAM_SIZE] is a vla for Visual Studio, so it can't compile.
  • there are two 'for loop with initial declarations'. It fails to compile with gcc 4.9 in Debug mode if -std=c99 is not explicitly added to the command line.

This simple patch fix these issues:

--- a/lib/identify.c
+++ b/lib/identify.c
@@ -180,7 +180,7 @@ static uint8_t otsu(const struct quirc *q)
 	int numPixels = q->w * q->h;
 
 	// Calculate histogram
-	const int HISTOGRAM_SIZE = 256;
+	#define HISTOGRAM_SIZE 256
 	unsigned int histogram[HISTOGRAM_SIZE];
 	memset(histogram, 0, (HISTOGRAM_SIZE) * sizeof(unsigned int));
 	uint8_t* ptr = q->image;
@@ -192,7 +192,8 @@ static uint8_t otsu(const struct quirc *q)
 
 	// Calculate weighted sum of histogram values
 	int sum = 0;
-	for (int i = 0; i < HISTOGRAM_SIZE; ++i) {
+	int i;
+	for (i = 0; i < HISTOGRAM_SIZE; ++i) {
 		sum += i * histogram[i];
 	}
 
@@ -201,7 +202,7 @@ static uint8_t otsu(const struct quirc *q)
 	int q1 = 0;
 	double max = 0;
 	uint8_t threshold = 0;
-	for (int i = 0; i < HISTOGRAM_SIZE; ++i) {
+	for (i = 0; i < HISTOGRAM_SIZE; ++i) {
 		// Weighted background
 		q1 += histogram[i];
 		if (q1 == 0)

Hi @SpaceIm,

Thank you for your help. Could you test #69 please?

Hi @kaworu.

Tested, and it works fine in Debug and Release mode, as a static and shared lib, on Linux (gcc 4.9 to 9, clang 3.9 to 9), Macos (apple clang 9.4, 10.3, 11.3), Windows (Visual Studio 2015, 2017, 2019).

Thanks for your extensive tests! <stdint.h> is c99 but is widely supported so I guess it's OK.