[Spice-devel] [vdagent-win PATCH v10 5/6] Add test for PNG files

Frediano Ziglio fziglio at redhat.com
Fri Jul 21 13:51:40 UTC 2017


Test various image and formats.
The idea is to decode and encode again an image and
check for differences. ImageMagick is used to create
some test image and compare results.
Wine is used to execute a test helper.

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

diff --git a/Makefile.am b/Makefile.am
index 175d8f7..411bf0d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -68,6 +68,26 @@ vdservice_rc.$(OBJEXT): vdservice/vdservice.rc
 
 MAINTAINERCLEANFILES += vdservice_rc.$(OBJEXT)
 
+check_PROGRAMS = imagetest
+
+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
+
 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..ee9d86e
--- /dev/null
+++ b/test-png
@@ -0,0 +1,58 @@
+#!/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
+}
+
+compare_images() {
+    DIFF=$(compare -metric AE $1 $2 - 2>&1 > /dev/null || true)
+    if [ "$DIFF" != "0" ]; then
+        error "Images $1 and $2 are too different, diff $DIFF"
+    fi
+}
+
+do_test() {
+    echo "Running image $IMAGE with '$*'..."
+    convert $IMAGE "$@" $IN
+    wine imagetest.exe $IN $OUT_BMP $OUT
+    verbose ls -lh $IN
+    verbose identify $IN
+    verbose identify $OUT_BMP
+    compare_images $IN $OUT
+    compare_images $IN $OUT_BMP
+    rm -f $IN $OUT $OUT_BMP
+}
+
+do_test_all() {
+    IMAGE="$1"
+    do_test
+    do_test -type Palette
+    do_test -type Palette -colors 14
+    do_test -type Palette -colors 3
+    do_test -type TrueColor
+    do_test -type Grayscale -depth 8
+    do_test -type Grayscale -depth 2
+    do_test -type Grayscale -depth 4
+}
+
+set -e
+# test with small image
+do_test_all rose:
+# test with larger image
+do_test_all logo:
+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..319b188
--- /dev/null
+++ b/vdagent/imagetest.cpp
@@ -0,0 +1,82 @@
+/*
+   Copyright (C) 2017 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 <assert.h>
+#include <vector>
+
+#include "vdcommon.h"
+#include "image.h"
+#include "imagepng.h"
+
+int main(int argc, char **argv)
+{
+    ImageCoder *coder = create_png_coder();
+
+    assert(coder);
+    if (argc < 2) {
+        fprintf(stderr, "Usage: %s <in-image> [<out-bmp> [<out-png>]]\n", argv[0]);
+        return 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.13.3



More information about the Spice-devel mailing list