[Spice-devel] [vdagent-win PATCH v2] RFC: Add test for PNG files

Frediano Ziglio fziglio at redhat.com
Fri Jul 7 08:40:44 UTC 2017


Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 Makefile.am           | 20 +++++++++++++
 test-png              | 41 +++++++++++++++++++++++++++
 vdagent/imagetest.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 139 insertions(+)
 create mode 100755 test-png
 create mode 100644 vdagent/imagetest.cpp

Changes since v2:
- add a script to do a proper "make check" (wine required);
- do not install test utility;
- fix argument count check on test utility.

diff --git a/Makefile.am b/Makefile.am
index 175d8f7..ba1ab61 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -22,6 +22,7 @@ endif
 LIBS = -lversion
 
 bin_PROGRAMS = vdagent vdservice
+noinst_PROGRAMS = imagetest
 
 vdagent_LDADD = $(LIBPNG_LIBS) $(ZLIB_LIBS) -lwtsapi32 -lgdi32 vdagent_rc.$(OBJEXT)
 vdagent_CXXFLAGS = $(AM_CXXFLAGS) $(LIBPNG_CFLAGS)
@@ -68,6 +69,25 @@ vdservice_rc.$(OBJEXT): vdservice/vdservice.rc
 
 MAINTAINERCLEANFILES += vdservice_rc.$(OBJEXT)
 
+imagetest_LDADD = $(LIBPNG_LIBS) $(ZLIB_LIBS) -lwtsapi32 -lgdi32
+imagetest_CXXFLAGS = $(AM_CXXFLAGS) $(LIBPNG_CFLAGS)
+imagetest_LDFLAGS = $(AM_LDFLAGS) -Wl,--subsystem,console
+imagetest_SOURCES =			\
+	common/vdcommon.cpp             \
+	common/vdcommon.h		\
+	common/vdlog.cpp		\
+	common/vdlog.h			\
+	vdagent/imagetest.cpp		\
+	vdagent/image.cpp		\
+	vdagent/image.h			\
+	vdagent/imagepng.cpp		\
+	vdagent/imagepng.h		\
+	$(NULL)
+
+TESTS = test-png
+EXTRA_DIST += test-png
+test-png: imagetest$(EXEEXT)
+
 deps.txt:
 	$(AM_V_GEN)rpm -qa | grep $(host_os) | sort | unix2dos > $@
 
diff --git a/test-png b/test-png
new file mode 100755
index 0000000..48e1ea9
--- /dev/null
+++ b/test-png
@@ -0,0 +1,41 @@
+#!/bin/bash
+
+VERBOSE=${VERBOSE:-0}
+
+IN=in.png
+OUT=out.png
+OUT_BMP=out.bmp
+
+error() {
+    echo "$*" >&2
+    exit 1
+}
+
+verbose() {
+    if [ $VERBOSE != 0 ]; then
+        "$@"
+    fi
+}
+
+do_test() {
+    echo "Running test with $*..."
+    convert rose: "$@" $IN
+    wine imagetest.exe $IN $OUT_BMP $OUT
+    verbose ls -lh $IN
+    verbose identify $IN
+    verbose identify $OUT_BMP
+    DIFF=$(compare -metric AE $IN $OUT - 2>&1 > /dev/null)
+    if [ "$DIFF" != "0" ]; then
+        error "Images are too different, diff $DIFF"
+    fi
+}
+
+set -e
+do_test
+do_test -type Palette
+do_test -type Palette -colors 14
+do_test -type TrueColor
+do_test -type Grayscale
+rm -f $IN $OUT $OUT_BMP
+
+verbose echo Finish
diff --git a/vdagent/imagetest.cpp b/vdagent/imagetest.cpp
new file mode 100644
index 0000000..3ea244e
--- /dev/null
+++ b/vdagent/imagetest.cpp
@@ -0,0 +1,78 @@
+/*
+   Copyright (C) 2016 Red Hat, Inc.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of
+   the License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#undef NDEBUG
+#include "vdcommon.h"
+#include "image.h"
+#include "imagepng.h"
+#include <assert.h>
+#include <vector>
+
+int main(int argc, char **argv)
+{
+    ImageCoder *coder = create_png_coder();
+
+    assert(coder);
+    assert(argc > 1);
+
+    // read all file into memory
+    FILE *f = fopen(argv[1], "rb");
+    assert(f);
+    assert(fseek(f, 0, SEEK_END) == 0);
+    long len = ftell(f);
+    assert(len > 0);
+    assert(fseek(f, 0, SEEK_SET) == 0);
+
+    std::vector<uint8_t> data(len);
+    assert(fread(&data[0], 1, len, f) == (unsigned long) len);
+    fclose(f);
+
+    size_t dib_size = coder->get_dib_size(&data[0], len);
+    assert(dib_size);
+    std::vector<uint8_t> out(dib_size);
+    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);
+
+    // 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;
+
+    return 0;
+}
+
-- 
2.9.4



More information about the Spice-devel mailing list