summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrediano Ziglio <fziglio@redhat.com>2017-08-22 12:57:16 +0100
committerFrediano Ziglio <fziglio@redhat.com>2017-08-22 17:41:43 +0100
commit9c85f8d3caf826099d8a1db562e23e5cf4e8b243 (patch)
tree416838991af8fd2f857a046075a4c9645d6b61b0
parentaaeecf129424752b13373a9242bcede58337e047 (diff)
imagetest: Save PNG file using a helper function
This allows to reuse the code to save a DIB to a file. Signed-off-by: Frediano Ziglio <fziglio@redhat.com> Acked-by: Uri Lublin <uril@redhat.com>
-rw-r--r--vdagent/imagetest.cpp28
1 files changed, 18 insertions, 10 deletions
diff --git a/vdagent/imagetest.cpp b/vdagent/imagetest.cpp
index 319b188..3a553a9 100644
--- a/vdagent/imagetest.cpp
+++ b/vdagent/imagetest.cpp
@@ -23,6 +23,23 @@
#include "image.h"
#include "imagepng.h"
+static void
+save_dib_to_file(ImageCoder& coder, const uint8_t *raw_dib, const char *filename)
+{
+ const BITMAPINFO& info(*(BITMAPINFO*) raw_dib);
+ const uint8_t *raw_bits = &raw_dib[sizeof(BITMAPINFOHEADER) + 4 * info.bmiHeader.biClrUsed];
+
+ long size = 0;
+ uint8_t *raw_file = coder.from_bitmap(info, raw_bits, size);
+ assert(raw_file && size > 0);
+
+ FILE *f = fopen(filename, "wb");
+ assert(f);
+ assert(fwrite(raw_file, 1, size, f) == (unsigned long) size);
+ fclose(f);
+ free(raw_file);
+}
+
int main(int argc, char **argv)
{
ImageCoder *coder = create_png_coder();
@@ -66,16 +83,7 @@ int main(int argc, char **argv)
fclose(f);
// convert back to PNG
- long png_size = 0;
- uint8_t *png = coder->from_bitmap(*((BITMAPINFO*)&out[0]), &out[sizeof(BITMAPINFOHEADER) + 4 * info.biClrUsed], png_size);
- assert(png && png_size > 0);
-
- f = fopen(argc > 3 ? argv[3] : "out.png", "wb");
- assert(f);
- assert(fwrite(png, 1, png_size, f) == (unsigned long) png_size);
- fclose(f);
- free(png);
- png = NULL;
+ save_dib_to_file(*coder, &out[0], argc > 3 ? argv[3] : "out.png");
return 0;
}