Selaa lähdekoodia

Switched to using the Heap because of a stack overflow from doing long inputs

Dan Hintz - Arch 5 vuotta sitten
vanhempi
sitoutus
77811b3219
3 muutettua tiedostoa jossa 13 lisäystä ja 9 poistoa
  1. 1 1
      Makefile
  2. BIN
      examples/harrypotter1.png
  3. 12 8
      frames2img.c

+ 1 - 1
Makefile

@@ -1,7 +1,7 @@
 all: compile
 
 compile:
-	gcc frames2img.c -o frames2img -lpng -fopenmp -std=c99
+	gcc frames2img.c -o frames2img -lpng -std=c99 -fopenmp
 
 clean:
 	-rm frames2img

BIN
examples/harrypotter1.png


+ 12 - 8
frames2img.c

@@ -6,7 +6,7 @@
 typedef uint8_t uint8;
 typedef uint32_t uint32;
 
-const int bytesPerPixel = 3; /// red, green, blue
+const int bytesPerPixel = 3;
 
 void usage();
 uint32 averagergb(png_image *, uint8 *);
@@ -14,11 +14,11 @@ uint32 averagergb(png_image *, uint8 *);
 int main(int argc, char **argv){
 	if(argc == 1) usage();
 	char *imageFileName = "output.png";
-	int height = 720;
+	int height = 480;
 	int width = argc-1;
 	int percent = 0;
 
-	uint32 frames[width];
+	uint32 *frames = malloc(width*sizeof(uint32));
 
 #pragma omp parallel for
 	for(int i =1;i<argc;i++) {
@@ -38,13 +38,14 @@ int main(int argc, char **argv){
 		fprintf(stdout, "%d%% Complete\r", ++percent * 100 / argc);
 		fflush(stdout);
 	}
-	uint8 image[height][width][bytesPerPixel];
+	uint8 *imgPointer = malloc(height*width*3*sizeof(uint8 *));
 
 	for(int i=0; i<height; i++){
 		for(int j=0; j<width; j++){
-			image[i][j][2] = ((frames[j] >> 16)&0xff);
-			image[i][j][1] = ((frames[j] >> 8)&0xff);
-			image[i][j][0] = (frames[j] & 0xff);
+			int index = i+j*height;
+			imgPointer[j*3+i*width*3+2] = ((frames[j] >> 16)&0xff);
+			imgPointer[j*3+i*width*3+1] = ((frames[j] >> 8)&0xff);
+			imgPointer[j*3+i*width*3+0] = ((frames[j])&0xff);
 		}
 	}
 
@@ -54,7 +55,10 @@ int main(int argc, char **argv){
 	output.width = width;
 	output.height = height;
 
-	png_image_write_to_file(&output, imageFileName, 0, image, 0, NULL);
+	png_image_write_to_file(&output, imageFileName, 0, imgPointer, 0, NULL);
+
+	free(imgPointer);
+	free(frames);
 }
 
 void