+ strm.avail_in = sizeof(screen_conv);
+ strm.next_in = screen_conv;
+
+ toalloc = 0;
+ strm.avail_out = 0;
+ screen_deflated = NULL;
+ flush = Z_NO_FLUSH;
+
+ do {
+ if (strm.avail_out == 0) {
+ toalloc += CHUNK;
+ screen_deflated = realloc(screen_deflated, toalloc);
+ if (screen_deflated == NULL) {
+ perror("realloc");
+ exit(EXIT_FAILURE);
+ }
+
+ strm.avail_out = CHUNK;
+ strm.next_out = screen_deflated + (toalloc - CHUNK);
+ }
+
+ ret = deflate(&strm, flush);
+ if (ret == Z_STREAM_ERROR) {
+ perror("deflate");
+ exit(EXIT_FAILURE);
+ }
+
+ if(strm.avail_in == 0) {
+ flush = Z_FINISH;
+ }
+ } while(ret != Z_STREAM_END);
+ deflated_size = toalloc - strm.avail_out;
+
+ deflateEnd(&strm);
+
+ image = malloc(sizeof(png) +
+ sizeof(ihdr) + 8 + /* 8 = length, csum */
+ sizeof(idat) + deflated_size + 8 +
+ sizeof(iend) + 8);
+
+ if (image == NULL) {
+ perror("malloc");
+ return NULL;
+ }
+