WORKS ON TEST1

This commit is contained in:
Isaac Shoebottom 2023-12-04 20:02:21 -04:00
parent 9f9d7a1925
commit d81bbe423e
2 changed files with 11 additions and 5 deletions

View File

@ -40,7 +40,10 @@ int main(int argc, char *argv[]) {
} }
printf("Chunk size is:%d\n", chunks[i]->length); 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); destroy_chunks(chunks);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -104,19 +104,19 @@ png_chunk **get_png_chunks(char *buffer) {
} }
unsigned int offset = PNG_SIGNATURE_SIZE; unsigned int offset = PNG_SIGNATURE_SIZE;
int i = 0; int i = 0;
while (1) { while (true) {
chunks[i] = get_png_chunk(buffer, offset); chunks[i] = get_png_chunk(buffer, offset);
// 12 = 4 (length) + 4 (type) + 4 (crc) // 12 = 4 (length) + 4 (type) + 4 (crc)
offset += 12 + chunks[i]->length; offset += 12 + chunks[i]->length;
if (memcmp(chunks[i]->type, "IEND", 4) == 0) { if (memcmp(chunks[i]->type, "IEND", 4) == 0) {
break; break;
} }
i++; chunks = realloc(chunks, sizeof(png_chunk *) * (i + 2));
chunks = realloc(chunks, sizeof(png_chunk *) * (i + 1));
if (chunks == NULL) { if (chunks == NULL) {
perror("realloc"); perror("realloc");
exit(1); exit(1);
} }
i++;
} }
return chunks; return chunks;
} }
@ -153,9 +153,12 @@ void write_png_chunks(char *path, png_chunk **chunks) {
exit(1); exit(1);
} }
fwrite(PNG_SIGNATURE, PNG_SIGNATURE_SIZE, 1, fp); 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]->length, 4, 1, fp);
fwrite(chunks[i]->type, 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]->data, chunks[i]->length, 1, fp);
fwrite(&chunks[i]->crc, 4, 1, fp); fwrite(&chunks[i]->crc, 4, 1, fp);
} }