WORKS ON TEST1
This commit is contained in:
		| @@ -40,7 +40,10 @@ int main(int argc, char *argv[]) { | ||||
| 		} | ||||
| 		printf("Chunk size is:%d\n", chunks[i]->length); | ||||
| 	} | ||||
| 	// write_png_chunks(path, chunks); | ||||
| 	char new_path[PATH_MAX]; | ||||
| 	getcwd(new_path, sizeof(new_path)); | ||||
| 	strcat(new_path, "/output.png"); | ||||
| 	write_png_chunks(new_path, chunks); | ||||
| 	destroy_chunks(chunks); | ||||
| 	return EXIT_SUCCESS; | ||||
| } | ||||
| @@ -104,19 +104,19 @@ png_chunk **get_png_chunks(char *buffer) { | ||||
| 	} | ||||
| 	unsigned int offset = PNG_SIGNATURE_SIZE; | ||||
| 	int i = 0; | ||||
| 	while (1) { | ||||
| 	while (true) { | ||||
| 		chunks[i] = get_png_chunk(buffer, offset); | ||||
| 		// 12 = 4 (length) + 4 (type) + 4 (crc) | ||||
| 		offset += 12 + chunks[i]->length; | ||||
| 		if (memcmp(chunks[i]->type, "IEND", 4) == 0) { | ||||
| 			break; | ||||
| 		} | ||||
| 		i++; | ||||
| 		chunks = realloc(chunks, sizeof(png_chunk *) * (i + 1)); | ||||
| 		chunks = realloc(chunks, sizeof(png_chunk *) * (i + 2)); | ||||
| 		if (chunks == NULL) { | ||||
| 			perror("realloc"); | ||||
| 			exit(1); | ||||
| 		} | ||||
| 		i++; | ||||
| 	} | ||||
| 	return chunks; | ||||
| } | ||||
| @@ -153,9 +153,12 @@ void write_png_chunks(char *path, png_chunk **chunks) { | ||||
| 		exit(1); | ||||
| 	} | ||||
| 	fwrite(PNG_SIGNATURE, PNG_SIGNATURE_SIZE, 1, fp); | ||||
| 	for (int i = 0; chunks[i] != NULL; i++) { | ||||
| 	unsigned int size = get_number_of_chunks(chunks); | ||||
| 	for (int i = 0; i < size; i++) { | ||||
| 		chunks[i]->length = htonl(chunks[i]->length); | ||||
| 		fwrite(&chunks[i]->length, 4, 1, fp); | ||||
| 		fwrite(chunks[i]->type, 4, 1, fp); | ||||
| 		chunks[i]->length = ntohl(chunks[i]->length); | ||||
| 		fwrite(chunks[i]->data, chunks[i]->length, 1, fp); | ||||
| 		fwrite(&chunks[i]->crc, 4, 1, fp); | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user