Re: [Spice-devel] [vdagent-win PATCH v8 3/5] Write code to decode PNG format

2017-07-21 Thread Frediano Ziglio
>
> On Wed, Jul 19, 2017 at 09:42:41AM +0100, Frediano Ziglio wrote:
> > Signed-off-by: Frediano Ziglio 
> > ---
> >  Makefile.am  |   6 +-
> >  configure.ac |   3 +
> >  vdagent/image.cpp|   8 +-
> >  vdagent/imagepng.cpp | 237
> >  +++
> >  vdagent/imagepng.h   |  25 ++
> >  5 files changed, 270 insertions(+), 9 deletions(-)
> >  create mode 100644 vdagent/imagepng.cpp
> >  create mode 100644 vdagent/imagepng.h
> > 
> > diff --git a/Makefile.am b/Makefile.am
> > index b35dd57..175d8f7 100644
> > --- a/Makefile.am
> > +++ b/Makefile.am
> > @@ -23,8 +23,8 @@ LIBS = -lversion
> >  
> >  bin_PROGRAMS = vdagent vdservice
> >  
> > -vdagent_LDADD = -lwtsapi32 -lgdi32 vdagent_rc.$(OBJEXT)
> > -vdagent_CXXFLAGS = $(AM_CXXFLAGS)
> > +vdagent_LDADD = $(LIBPNG_LIBS) $(ZLIB_LIBS) -lwtsapi32 -lgdi32
> > vdagent_rc.$(OBJEXT)
> > +vdagent_CXXFLAGS = $(AM_CXXFLAGS) $(LIBPNG_CFLAGS)
> >  vdagent_LDFLAGS = $(AM_LDFLAGS) -Wl,--subsystem,windows
> >  vdagent_SOURCES =  \
> > common/vdcommon.cpp \
> > @@ -44,6 +44,8 @@ vdagent_SOURCES = \
> > vdagent/as_user.h   \
> > vdagent/image.cpp   \
> > vdagent/image.h \
> > +   vdagent/imagepng.cpp\
> > +   vdagent/imagepng.h  \
> > $(NULL)
> >  
> >  vdagent_rc.$(OBJEXT): vdagent/vdagent.rc
> > diff --git a/configure.ac b/configure.ac
> > index 4eac4b4..bb33075 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -101,6 +101,9 @@ dnl
> > ---
> >  dnl - Check library dependencies
> >  dnl
> >  ---
> >  
> > +PKG_CHECK_MODULES(LIBPNG, [libpng])
> > +PKG_CHECK_MODULES(ZLIB, [zlib])
> > +
> >  dnl
> >  ---
> >  dnl - Makefiles, etc.
> >  dnl
> >  ---
> > diff --git a/vdagent/image.cpp b/vdagent/image.cpp
> > index faec18f..82cfb0e 100644
> > --- a/vdagent/image.cpp
> > +++ b/vdagent/image.cpp
> > @@ -21,9 +21,9 @@
> >  
> >  #include "vdcommon.h"
> >  #include "image.h"
> > +#include "imagepng.h"
> >  
> >  ImageCoder *create_bitmap_coder();
> > -ImageCoder *create_png_coder();
> >  
> >  static ImageCoder *get_coder(uint32_t vdagent_type)
> >  {
> > @@ -172,9 +172,3 @@ ImageCoder *create_bitmap_coder()
> >  {
> >  return new BitmapCoder();
> >  }
> > -
> > -// TODO
> > -ImageCoder *create_png_coder()
> > -{
> > -return NULL;
> > -}
> > diff --git a/vdagent/imagepng.cpp b/vdagent/imagepng.cpp
> > new file mode 100644
> > index 000..9530d71
> > --- /dev/null
> > +++ b/vdagent/imagepng.cpp
> > @@ -0,0 +1,237 @@
> > +/*
> > +   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 .
> > +*/
> > +
> > +#include "vdcommon.h"
> > +
> > +#include 
> > +#include 
> > +
> > +#include "imagepng.h"
> > +
> > +class PngCoder: public ImageCoder
> > +{
> > +public:
> > +PngCoder() {};
> > +size_t get_dib_size(const uint8_t *data, size_t size);
> > +void get_dib_data(uint8_t *dib, const uint8_t *data, size_t size);
> > +uint8_t *from_bitmap(const BITMAPINFO& info, const void *bits, long
> > );
> > +private:
> > +size_t convert_to_dib(uint8_t *out_buf, const uint8_t *data, size_t
> > size);
> > +};
> > +
> > +struct BufferIo {
> > +uint8_t *buf;
> > +uint32_t pos, size;
> > +BufferIo(uint8_t *_buf, uint32_t _size):
> > +buf(_buf), pos(0), size(_size)
> > +{}
> > +};
> > +
> > +static void read_from_bufio(png_structp png, png_bytep out, png_size_t
> > size)
> > +{
> > +BufferIo& io(*(BufferIo*)png_get_io_ptr(png));
> > +if (io.pos + size > io.size)
> > +png_error(png, "read past end");
> > +memcpy(out, io.buf+io.pos, size);
> > +io.pos += size;
> > +}
> > +
> > +size_t PngCoder::get_dib_size(const uint8_t *data, size_t size)
> > +{
> > +return convert_to_dib(NULL, data, size);
> > +}
> > +
> > +typedef void line_fixup_t(uint8_t *line, unsigned int width);
> > +
> > +static void line_fixup_none(uint8_t *line, unsigned int width)
> > +{
> > +}
> 

Re: [Spice-devel] [vdagent-win PATCH v8 3/5] Write code to decode PNG format

2017-07-21 Thread Christophe Fergeau
On Wed, Jul 19, 2017 at 09:42:41AM +0100, Frediano Ziglio wrote:
> Signed-off-by: Frediano Ziglio 
> ---
>  Makefile.am  |   6 +-
>  configure.ac |   3 +
>  vdagent/image.cpp|   8 +-
>  vdagent/imagepng.cpp | 237 
> +++
>  vdagent/imagepng.h   |  25 ++
>  5 files changed, 270 insertions(+), 9 deletions(-)
>  create mode 100644 vdagent/imagepng.cpp
>  create mode 100644 vdagent/imagepng.h
> 
> diff --git a/Makefile.am b/Makefile.am
> index b35dd57..175d8f7 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -23,8 +23,8 @@ LIBS = -lversion
>  
>  bin_PROGRAMS = vdagent vdservice
>  
> -vdagent_LDADD = -lwtsapi32 -lgdi32 vdagent_rc.$(OBJEXT)
> -vdagent_CXXFLAGS = $(AM_CXXFLAGS)
> +vdagent_LDADD = $(LIBPNG_LIBS) $(ZLIB_LIBS) -lwtsapi32 -lgdi32 
> vdagent_rc.$(OBJEXT)
> +vdagent_CXXFLAGS = $(AM_CXXFLAGS) $(LIBPNG_CFLAGS)
>  vdagent_LDFLAGS = $(AM_LDFLAGS) -Wl,--subsystem,windows
>  vdagent_SOURCES =\
>   common/vdcommon.cpp \
> @@ -44,6 +44,8 @@ vdagent_SOURCES =   \
>   vdagent/as_user.h   \
>   vdagent/image.cpp   \
>   vdagent/image.h \
> + vdagent/imagepng.cpp\
> + vdagent/imagepng.h  \
>   $(NULL)
>  
>  vdagent_rc.$(OBJEXT): vdagent/vdagent.rc
> diff --git a/configure.ac b/configure.ac
> index 4eac4b4..bb33075 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -101,6 +101,9 @@ dnl 
> ---
>  dnl - Check library dependencies
>  dnl 
> ---
>  
> +PKG_CHECK_MODULES(LIBPNG, [libpng])
> +PKG_CHECK_MODULES(ZLIB, [zlib])
> +
>  dnl 
> ---
>  dnl - Makefiles, etc.
>  dnl 
> ---
> diff --git a/vdagent/image.cpp b/vdagent/image.cpp
> index faec18f..82cfb0e 100644
> --- a/vdagent/image.cpp
> +++ b/vdagent/image.cpp
> @@ -21,9 +21,9 @@
>  
>  #include "vdcommon.h"
>  #include "image.h"
> +#include "imagepng.h"
>  
>  ImageCoder *create_bitmap_coder();
> -ImageCoder *create_png_coder();
>  
>  static ImageCoder *get_coder(uint32_t vdagent_type)
>  {
> @@ -172,9 +172,3 @@ ImageCoder *create_bitmap_coder()
>  {
>  return new BitmapCoder();
>  }
> -
> -// TODO
> -ImageCoder *create_png_coder()
> -{
> -return NULL;
> -}
> diff --git a/vdagent/imagepng.cpp b/vdagent/imagepng.cpp
> new file mode 100644
> index 000..9530d71
> --- /dev/null
> +++ b/vdagent/imagepng.cpp
> @@ -0,0 +1,237 @@
> +/*
> +   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 .
> +*/
> +
> +#include "vdcommon.h"
> +
> +#include 
> +#include 
> +
> +#include "imagepng.h"
> +
> +class PngCoder: public ImageCoder
> +{
> +public:
> +PngCoder() {};
> +size_t get_dib_size(const uint8_t *data, size_t size);
> +void get_dib_data(uint8_t *dib, const uint8_t *data, size_t size);
> +uint8_t *from_bitmap(const BITMAPINFO& info, const void *bits, long 
> );
> +private:
> +size_t convert_to_dib(uint8_t *out_buf, const uint8_t *data, size_t 
> size);
> +};
> +
> +struct BufferIo {
> +uint8_t *buf;
> +uint32_t pos, size;
> +BufferIo(uint8_t *_buf, uint32_t _size):
> +buf(_buf), pos(0), size(_size)
> +{}
> +};
> +
> +static void read_from_bufio(png_structp png, png_bytep out, png_size_t size)
> +{
> +BufferIo& io(*(BufferIo*)png_get_io_ptr(png));
> +if (io.pos + size > io.size)
> +png_error(png, "read past end");
> +memcpy(out, io.buf+io.pos, size);
> +io.pos += size;
> +}
> +
> +size_t PngCoder::get_dib_size(const uint8_t *data, size_t size)
> +{
> +return convert_to_dib(NULL, data, size);
> +}
> +
> +typedef void line_fixup_t(uint8_t *line, unsigned int width);
> +
> +static void line_fixup_none(uint8_t *line, unsigned int width)
> +{
> +}
> +
> +static void line_fixup_2bpp_to_4bpp(uint8_t *line, unsigned int width)
> +{
> +width = (width + 3) / 4u;
> +while (width--) {
> +uint8_t from = line[width];
> +line[width*2+1] = ((from & 0x03) << 0) | ((from & 0x0c) << 2);
> +line[width*2+0] = 

[Spice-devel] [vdagent-win PATCH v8 3/5] Write code to decode PNG format

2017-07-19 Thread Frediano Ziglio
Signed-off-by: Frediano Ziglio 
---
 Makefile.am  |   6 +-
 configure.ac |   3 +
 vdagent/image.cpp|   8 +-
 vdagent/imagepng.cpp | 237 +++
 vdagent/imagepng.h   |  25 ++
 5 files changed, 270 insertions(+), 9 deletions(-)
 create mode 100644 vdagent/imagepng.cpp
 create mode 100644 vdagent/imagepng.h

diff --git a/Makefile.am b/Makefile.am
index b35dd57..175d8f7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,8 +23,8 @@ LIBS = -lversion
 
 bin_PROGRAMS = vdagent vdservice
 
-vdagent_LDADD = -lwtsapi32 -lgdi32 vdagent_rc.$(OBJEXT)
-vdagent_CXXFLAGS = $(AM_CXXFLAGS)
+vdagent_LDADD = $(LIBPNG_LIBS) $(ZLIB_LIBS) -lwtsapi32 -lgdi32 
vdagent_rc.$(OBJEXT)
+vdagent_CXXFLAGS = $(AM_CXXFLAGS) $(LIBPNG_CFLAGS)
 vdagent_LDFLAGS = $(AM_LDFLAGS) -Wl,--subsystem,windows
 vdagent_SOURCES =  \
common/vdcommon.cpp \
@@ -44,6 +44,8 @@ vdagent_SOURCES = \
vdagent/as_user.h   \
vdagent/image.cpp   \
vdagent/image.h \
+   vdagent/imagepng.cpp\
+   vdagent/imagepng.h  \
$(NULL)
 
 vdagent_rc.$(OBJEXT): vdagent/vdagent.rc
diff --git a/configure.ac b/configure.ac
index 4eac4b4..bb33075 100644
--- a/configure.ac
+++ b/configure.ac
@@ -101,6 +101,9 @@ dnl 
---
 dnl - Check library dependencies
 dnl ---
 
+PKG_CHECK_MODULES(LIBPNG, [libpng])
+PKG_CHECK_MODULES(ZLIB, [zlib])
+
 dnl ---
 dnl - Makefiles, etc.
 dnl ---
diff --git a/vdagent/image.cpp b/vdagent/image.cpp
index faec18f..82cfb0e 100644
--- a/vdagent/image.cpp
+++ b/vdagent/image.cpp
@@ -21,9 +21,9 @@
 
 #include "vdcommon.h"
 #include "image.h"
+#include "imagepng.h"
 
 ImageCoder *create_bitmap_coder();
-ImageCoder *create_png_coder();
 
 static ImageCoder *get_coder(uint32_t vdagent_type)
 {
@@ -172,9 +172,3 @@ ImageCoder *create_bitmap_coder()
 {
 return new BitmapCoder();
 }
-
-// TODO
-ImageCoder *create_png_coder()
-{
-return NULL;
-}
diff --git a/vdagent/imagepng.cpp b/vdagent/imagepng.cpp
new file mode 100644
index 000..9530d71
--- /dev/null
+++ b/vdagent/imagepng.cpp
@@ -0,0 +1,237 @@
+/*
+   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 .
+*/
+
+#include "vdcommon.h"
+
+#include 
+#include 
+
+#include "imagepng.h"
+
+class PngCoder: public ImageCoder
+{
+public:
+PngCoder() {};
+size_t get_dib_size(const uint8_t *data, size_t size);
+void get_dib_data(uint8_t *dib, const uint8_t *data, size_t size);
+uint8_t *from_bitmap(const BITMAPINFO& info, const void *bits, long );
+private:
+size_t convert_to_dib(uint8_t *out_buf, const uint8_t *data, size_t size);
+};
+
+struct BufferIo {
+uint8_t *buf;
+uint32_t pos, size;
+BufferIo(uint8_t *_buf, uint32_t _size):
+buf(_buf), pos(0), size(_size)
+{}
+};
+
+static void read_from_bufio(png_structp png, png_bytep out, png_size_t size)
+{
+BufferIo& io(*(BufferIo*)png_get_io_ptr(png));
+if (io.pos + size > io.size)
+png_error(png, "read past end");
+memcpy(out, io.buf+io.pos, size);
+io.pos += size;
+}
+
+size_t PngCoder::get_dib_size(const uint8_t *data, size_t size)
+{
+return convert_to_dib(NULL, data, size);
+}
+
+typedef void line_fixup_t(uint8_t *line, unsigned int width);
+
+static void line_fixup_none(uint8_t *line, unsigned int width)
+{
+}
+
+static void line_fixup_2bpp_to_4bpp(uint8_t *line, unsigned int width)
+{
+width = (width + 3) / 4u;
+while (width--) {
+uint8_t from = line[width];
+line[width*2+1] = ((from & 0x03) << 0) | ((from & 0x0c) << 2);
+line[width*2+0] = ((from & 0x30) >> 4) | ((from & 0xc0) >> 2);
+}
+}
+
+size_t PngCoder::convert_to_dib(uint8_t *out_buf, const uint8_t *data, size_t 
size)
+{
+BufferIo io((uint8_t *)data, size);
+
+png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, 
NULL, NULL);
+if (!png)
+return 0;
+
+png_infop info = png_create_info_struct(png);
+if