[Spice-commits] 3 commits - vdagent/image.cpp vdagent/image.h vdagent/imagetest.cpp

Frediano Ziglio fziglio at kemper.freedesktop.org
Tue Aug 22 16:42:24 UTC 2017


 vdagent/image.cpp     |   17 ++++++++++++-----
 vdagent/image.h       |    2 ++
 vdagent/imagetest.cpp |   48 ++++++++++++++++++++++++------------------------
 3 files changed, 38 insertions(+), 29 deletions(-)

New commits:
commit 16aee83802ee436cc5216bd55cb8f7760c30f50a
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Aug 22 12:58:25 2017 +0100

    imagetest: Save BMP file using BitmapCoder
    
    This allows to test BitmapCoder::from_bitmap.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Uri Lublin <uril at redhat.com>

diff --git a/vdagent/image.cpp b/vdagent/image.cpp
index 15bd4fa..1b21b53 100644
--- a/vdagent/image.cpp
+++ b/vdagent/image.cpp
@@ -23,8 +23,6 @@
 #include "image.h"
 #include "imagepng.h"
 
-ImageCoder *create_bitmap_coder();
-
 static ImageCoder *get_coder(uint32_t vdagent_type)
 {
     switch (vdagent_type) {
diff --git a/vdagent/image.h b/vdagent/image.h
index da549d3..326d7f9 100644
--- a/vdagent/image.h
+++ b/vdagent/image.h
@@ -39,6 +39,8 @@ static inline size_t compute_dib_stride(unsigned int width, unsigned int bit_cou
     return ((width * bit_count + 31u) & ~31u) / 8u;
 }
 
+ImageCoder *create_bitmap_coder();
+
 /**
  * Returns image to put in the clipboard.
  *
diff --git a/vdagent/imagetest.cpp b/vdagent/imagetest.cpp
index 3a553a9..36b8f6c 100644
--- a/vdagent/imagetest.cpp
+++ b/vdagent/imagetest.cpp
@@ -18,6 +18,7 @@
 #undef NDEBUG
 #include <assert.h>
 #include <vector>
+#include <memory>
 
 #include "vdcommon.h"
 #include "image.h"
@@ -42,7 +43,7 @@ save_dib_to_file(ImageCoder& coder, const uint8_t *raw_dib, const char *filename
 
 int main(int argc, char **argv)
 {
-    ImageCoder *coder = create_png_coder();
+    std::unique_ptr<ImageCoder> coder(create_png_coder());
 
     assert(coder);
     if (argc < 2) {
@@ -68,19 +69,10 @@ int main(int argc, char **argv)
     memset(&out[0], 0xcc, dib_size);
     coder->get_dib_data(&out[0], &data[0], len);
 
-    // looks like many tools wants this header so craft it
-    BITMAPFILEHEADER head;
-    memset(&head, 0, sizeof(head));
-    head.bfType = 'B'+'M'*256u;
-    head.bfSize = sizeof(head) + dib_size;
-    BITMAPINFOHEADER& info(*(BITMAPINFOHEADER*)&out[0]);
-    head.bfOffBits = sizeof(head) + sizeof(BITMAPINFOHEADER) + 4 * info.biClrUsed;
-
-    f = fopen(argc > 2 ? argv[2] : "out.bmp", "wb");
-    assert(f);
-    assert(fwrite(&head, 1, sizeof(head), f) == sizeof(head));
-    assert(fwrite(&out[0], 1, dib_size, f) == dib_size);
-    fclose(f);
+    // write BMP file
+    std::unique_ptr<ImageCoder> bmp_coder(create_bitmap_coder());
+    assert(bmp_coder);
+    save_dib_to_file(*bmp_coder, &out[0], argc > 2 ? argv[2] : "out.bmp");
 
     // convert back to PNG
     save_dib_to_file(*coder, &out[0], argc > 3 ? argv[3] : "out.png");
commit 9c85f8d3caf826099d8a1db562e23e5cf4e8b243
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Aug 22 12:57:16 2017 +0100

    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 at redhat.com>
    Acked-by: Uri Lublin <uril at redhat.com>

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;
 }
commit aaeecf129424752b13373a9242bcede58337e047
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Aug 22 12:51:45 2017 +0100

    Make BitmapCoder::from_bitmap return a BMP file format
    
    The network expect the format of the data to match a file
    format so prepending DIB data with BITMAPFILEHEADER change
    the format from DIB to BMP file.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Uri Lublin <uril at redhat.com>

diff --git a/vdagent/image.cpp b/vdagent/image.cpp
index 82cfb0e..15bd4fa 100644
--- a/vdagent/image.cpp
+++ b/vdagent/image.cpp
@@ -150,6 +150,8 @@ void BitmapCoder::get_dib_data(uint8_t *dib, const uint8_t *data, size_t size)
 
 uint8_t *BitmapCoder::from_bitmap(const BITMAPINFO& info, const void *bits, long &size)
 {
+    BITMAPFILEHEADER file_hdr;
+
     const BITMAPINFOHEADER& head(info.bmiHeader);
 
     const DWORD max_palette_colors = head.biBitCount <= 8 ? 1 << head.biBitCount : 0;
@@ -157,14 +159,21 @@ uint8_t *BitmapCoder::from_bitmap(const BITMAPINFO& info, const void *bits, long
 
     const size_t stride = compute_dib_stride(head.biWidth, head.biBitCount);
     const size_t image_size = stride * head.biHeight;
-    size = sizeof(head) + palette_size + image_size;
+    size = sizeof(file_hdr) + sizeof(head) + palette_size + image_size;
+
+    file_hdr.bfType = 'B' + 'M'*256u;
+    file_hdr.bfSize = size;
+    file_hdr.bfReserved1 = 0;
+    file_hdr.bfReserved2 = 0;
+    file_hdr.bfOffBits = sizeof(file_hdr) + sizeof(head) + palette_size;
 
     uint8_t *data = (uint8_t *) malloc(size);
     if (!data) {
         return NULL;
     }
-    memcpy(data, &info, sizeof(head) + palette_size);
-    memcpy(data + sizeof(head) + palette_size, bits, image_size);
+    memcpy(data, &file_hdr, sizeof(file_hdr));
+    memcpy(data + sizeof(file_hdr), &info, sizeof(head) + palette_size);
+    memcpy(data + sizeof(file_hdr) + sizeof(head) + palette_size, bits, image_size);
     return data;
 }
 


More information about the Spice-commits mailing list