Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[mms-lib] Added support for Qt-based resizing
That allows to eliminate dependency on ImageMagick.
Qt/ImageMagick switch is located in mms-lib/Config.mak
  • Loading branch information
monich committed Mar 5, 2014
1 parent ea4fd87 commit 740f598
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 39 deletions.
15 changes: 12 additions & 3 deletions mms-engine/Makefile
Expand Up @@ -5,9 +5,18 @@
.PHONY: mms_ofono_debug_lib mms_ofono_release_lib
.PHONY: mms_handler_debug_lib mms_handler_release_lib

#
# Pull in mms-lib configuration
#

include ../mms-lib/Config.mak

#
# Required packages
#

PKGS = gio-unix-2.0 gio-2.0 glib-2.0
LIB_PKGS = libwspcodec libsoup-2.4 ImageMagick $(PKGS)
LIB_PKGS = libwspcodec libsoup-2.4 $(RESIZE_PKG) $(PKGS)

#
# Default target
Expand Down Expand Up @@ -74,7 +83,7 @@ RELEASE_FLAGS = -O2
DEBUG_DEFS = -DDEBUG
RELEASE_DEFS =
WARN = -Wall
CFLAGS = $(shell pkg-config --cflags $(PKGS)) -I. -I$(GEN_DIR) \
CFLAGS = -fPIC $(shell pkg-config --cflags $(PKGS)) -I. -I$(GEN_DIR) \
-I$(MMS_LIB_DIR)/include -I$(MMS_OFONO_DIR)/include \
-I$(MMS_HANDLER_DIR)/include -MMD

Expand All @@ -90,7 +99,7 @@ endif
DEBUG_CFLAGS = $(DEBUG_FLAGS) $(DEBUG_DEFS) $(CFLAGS)
RELEASE_CFLAGS = $(RELEASE_FLAGS) $(RELEASE_DEFS) $(CFLAGS)

LIBS = $(shell pkg-config --libs $(LIB_PKGS)) -lmagic -ljpeg
LIBS = $(shell pkg-config --libs $(LIB_PKGS)) -lmagic -ljpeg $(RESIZE_LIBS)
DEBUG_LIBS = \
$(MMS_OFONO_DEBUG_LIB) \
$(MMS_HANDLER_DEBUG_LIB) \
Expand Down
12 changes: 11 additions & 1 deletion mms-engine/mms-engine.pro
@@ -1,5 +1,4 @@
TEMPLATE = app
CONFIG -= qt
CONFIG += link_pkgconfig
PKGCONFIG += gio-unix-2.0 gio-2.0 glib-2.0 libsoup-2.4 libwspcodec ImageMagick
DBUS_INTERFACE_DIR = $$_PRO_FILE_PWD_
Expand All @@ -11,6 +10,17 @@ INCLUDEPATH += $$MMS_LIB_DIR/include
INCLUDEPATH += $$MMS_HANDLER_DIR/include
QMAKE_CFLAGS += -Wno-unused

include(../mms-lib/mms-lib-config.pri)

ResizeImageMagick {
CONFIG -= qt
PKGCONFIG += ImageMagick
} else {
ResizeQt {
} else {
}
}

SOURCES += \
main.c \
mms_engine.c
Expand Down
3 changes: 2 additions & 1 deletion mms-handler-dbus/Makefile
Expand Up @@ -44,7 +44,8 @@ DEBUG_DEFS = -DDEBUG
RELEASE_DEFS =
WARNINGS = -Wall
INCLUDES = -I$(SRC_DIR) -I$(INCLUDE_DIR) -I$(MMS_LIB_INCLUDE) -I$(GEN_DIR) -I.
CFLAGS += $(WARNINGS) $(INCLUDES) $(shell pkg-config --cflags $(PKGS)) -MMD
CFLAGS += $(WARNINGS) $(INCLUDES) $(shell pkg-config --cflags $(PKGS)) \
-fPIC -MMD

ifndef KEEP_SYMBOLS
KEEP_SYMBOLS = 0
Expand Down
24 changes: 24 additions & 0 deletions mms-lib/Config.mak
@@ -0,0 +1,24 @@
# -*- Mode: makefile-gmake -*-

MMS_RESIZE = ImageMagick
#MMS_RESIZE = Qt

#
# ImageMagick support
#

ifeq ($(MMS_RESIZE),ImageMagick)
RESIZE_DEFINES = -DMMS_RESIZE_IMAGEMAGICK
RESIZE_PKG = ImageMagick
else
ifeq ($(MMS_RESIZE),Qt)
RESIZE_LIBS = -lstdc++
RESIZE_DEFINES = -DMMS_RESIZE_QT
ifeq ($(shell qmake --version | grep "Using Qt version 5"),)
RESIZE_PKG = QtGui
else
RESIZE_PKG = Qt5Gui
endif
endif
endif

40 changes: 29 additions & 11 deletions mms-lib/Makefile
Expand Up @@ -2,8 +2,14 @@

.PHONY: clean all debug release

#
# Pull in mms-lib configuration
#

include Config.mak

# Required packages
PKGS = glib-2.0 libsoup-2.4 libwspcodec
PKGS = glib-2.0 libsoup-2.4 libwspcodec $(RESIZE_PKG)

#
# Default target
Expand All @@ -23,6 +29,10 @@ SRC = mms_attachment.c mms_attachment_image.c mms_attachment_jpeg.c \
mms_task_publish.c mms_task_read.c mms_task_retrieve.c mms_task_send.c \
mms_util.c

ifeq ($(MMS_RESIZE),Qt)
SRC_CPP += mms_attachment_qt.cpp
endif

#
# Directories
#
Expand All @@ -45,13 +55,6 @@ ifneq ($(GCOV),0)
CFLAGS += --coverage
endif

#
# ImageMagick support
#

DEFINES += -DHAVE_IMAGEMAGICK
PKGS += ImageMagick

#
# Tools and flags
#
Expand All @@ -64,8 +67,9 @@ RELEASE_FLAGS = -O2
DEBUG_DEFS = -DDEBUG
RELEASE_DEFS =
WARNINGS = -Wall
DEFINES = $(RESIZE_DEFINES)
INCLUDES = -I$(SRC_DIR) -I$(INCLUDE_DIR)
CFLAGS += $(WARNINGS) $(DEFINES) $(INCLUDES) \
CFLAGS += -fPIC $(WARNINGS) $(DEFINES) $(INCLUDES) \
$(shell pkg-config --cflags $(PKGS)) -MMD

ifndef KEEP_SYMBOLS
Expand All @@ -78,13 +82,21 @@ endif

DEBUG_CFLAGS = $(DEBUG_FLAGS) $(DEBUG_DEFS) $(CFLAGS)
RELEASE_CFLAGS = $(RELEASE_FLAGS) $(RELEASE_DEFS) $(CFLAGS)
DEBUG_CPPFLAGS = -Wno-psabi $(DEBUG_CFLAGS)
RELEASE_CPPFLAGS = -Wno-psabi $(RELEASE_CFLAGS)

#
# Files
#

DEBUG_OBJS = $(SRC:%.c=$(DEBUG_BUILD_DIR)/%.o)
RELEASE_OBJS = $(SRC:%.c=$(RELEASE_BUILD_DIR)/%.o)
DEBUG_OBJS = \
$(SRC:%.c=$(DEBUG_BUILD_DIR)/%.o) \
$(SRC_CPP:%.cpp=$(DEBUG_BUILD_DIR)/%.o)
RELEASE_OBJS = \
$(SRC:%.c=$(RELEASE_BUILD_DIR)/%.o) \
$(SRC_CPP:%.cpp=$(RELEASE_BUILD_DIR)/%.o)

$(info $(DEBUG_OBJS))

#
# Dependencies
Expand Down Expand Up @@ -128,3 +140,9 @@ $(DEBUG_BUILD_DIR)/%.o : $(SRC_DIR)/%.c

$(RELEASE_BUILD_DIR)/%.o : $(SRC_DIR)/%.c
$(CC) -c $(RELEASE_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(DEBUG_BUILD_DIR)/%.o : $(SRC_DIR)/%.cpp
$(CC) -c $(DEBUG_CPPFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(RELEASE_BUILD_DIR)/%.o : $(SRC_DIR)/%.cpp
$(CC) -c $(RELEASE_CPPFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@
2 changes: 2 additions & 0 deletions mms-lib/mms-lib-config.pri
@@ -0,0 +1,2 @@
#CONFIG += ResizeImageMagick
CONFIG += ResizeQt
16 changes: 12 additions & 4 deletions mms-lib/mms-lib.pro
@@ -1,13 +1,21 @@
TEMPLATE = lib
CONFIG += staticlib
CONFIG -= qt
CONFIG += link_pkgconfig
PKGCONFIG += glib-2.0 libsoup-2.4 libwspcodec
INCLUDEPATH += include
QMAKE_CFLAGS += -Wno-unused

DEFINES += HAVE_IMAGEMAGICK
PKGCONFIG += ImageMagick
include(mms-lib-config.pri)

ResizeImageMagick {
CONFIG -= qt
PKGCONFIG += ImageMagick
DEFINES += MMS_RESIZE_IMAGEMAGICK
} else {
ResizeQt {
DEFINES += MMS_RESIZE_QT
}
}

CONFIG(debug, debug|release) {
DEFINES += DEBUG
Expand All @@ -20,6 +28,7 @@ SOURCES += \
src/mms_attachment.c \
src/mms_attachment_image.c \
src/mms_attachment_jpeg.c \
src/mms_attachment_qt.cpp \
src/mms_codec.c \
src/mms_connection.c \
src/mms_connman.c \
Expand Down Expand Up @@ -56,7 +65,6 @@ HEADERS += \
HEADERS += \
include/mms_connection.h \
include/mms_connman.h \
include/mms_database.h \
include/mms_dispatcher.h \
include/mms_handler.h \
include/mms_lib_log.h \
Expand Down
24 changes: 12 additions & 12 deletions mms-lib/src/mms_attachment_image.c
Expand Up @@ -15,7 +15,7 @@
#include "mms_attachment_image.h"
#include "mms_file_util.h"

#ifdef HAVE_IMAGEMAGICK
#ifdef MMS_RESIZE_IMAGEMAGICK
# include <magick/api.h>
#endif

Expand All @@ -29,7 +29,6 @@ G_DEFINE_TYPE(MMSAttachmentImage, mms_attachment_image, MMS_TYPE_ATTACHMENT);
#define MMS_ATTACHMENT_IMAGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), \
MMS_TYPE_ATTACHMENT_IMAGE, MMSAttachmentImageClass))

static
int
mms_attachment_image_next_resize_step(
MMSAttachmentImage* image,
Expand All @@ -47,7 +46,6 @@ mms_attachment_image_next_resize_step(
return next_step;
}

static
const char*
mms_attachment_image_prepare_filename(
MMSAttachmentImage* image)
Expand All @@ -69,13 +67,13 @@ mms_attachment_image_prepare_filename(
return image->resized;
}

#ifdef HAVE_IMAGEMAGICK
static
gboolean
mms_attachment_image_resize_imagemagick(
mms_attachment_image_resize_default(
MMSAttachmentImage* image)
{
gboolean ok = FALSE;
#ifdef MMS_RESIZE_IMAGEMAGICK
ExceptionInfo ex;
Image* src;
ImageInfo* info = CloneImageInfo(NULL);
Expand All @@ -87,10 +85,10 @@ mms_attachment_image_resize_imagemagick(
src = ReadImage(info, &ex);
if (src) {
if (src->magick_columns > 1 && src->magick_rows > 1) {;
const int next_step = mms_attachment_image_next_resize_step(image,
src->magick_columns, src->magick_rows);
const unsigned int src_cols = src->magick_columns;
const unsigned int src_rows = src->magick_rows;
const int next_step = mms_attachment_image_next_resize_step(image,
src_cols, src_rows);
const unsigned int cols = src_cols/(next_step+1);
const unsigned int rows = src_rows/(next_step+1);
Image* dest;
Expand Down Expand Up @@ -119,9 +117,13 @@ mms_attachment_image_resize_imagemagick(
ClearMagickException(&ex);
DestroyExceptionInfo(&ex);
DestroyImageInfo(info);
#else
# ifdef MMS_RESIZE_QT
ok = mms_attachment_image_resize_qt(image);
# endif /* MMS_RESIZE_QT */
#endif /* MMS_RESIZE_IMAGEMAGICK */
return ok;
}
#endif /* HAVE_IMAGEMAGICK */

static
gboolean
Expand Down Expand Up @@ -255,9 +257,7 @@ mms_attachment_image_resize(
at->map = NULL;
}
ok = mms_attachment_image_resize_type_specific(image);
#ifdef HAVE_IMAGEMAGICK
if (!ok) ok = mms_attachment_image_resize_imagemagick(image);
#endif /* HAVE_IMAGEMAGICK */
if (!ok) ok = mms_attachment_image_resize_default(image);
if (ok) {
GError* error = NULL;
GMappedFile* map = g_mapped_file_new(image->resized, FALSE, &error);
Expand Down Expand Up @@ -317,7 +317,7 @@ void
mms_attachment_image_init(
MMSAttachmentImage* image)
{
#ifdef HAVE_IMAGEMAGICK
#ifdef MMS_RESIZE_IMAGEMAGICK
image->attachment.flags |= MMS_ATTACHMENT_RESIZABLE;
#endif
}
Expand Down
16 changes: 16 additions & 0 deletions mms-lib/src/mms_attachment_image.h
Expand Up @@ -71,6 +71,22 @@ typedef struct mms_attachment_image_class {

} MMSAttachmentImageClass;

int
mms_attachment_image_next_resize_step(
MMSAttachmentImage* image,
unsigned int columns,
unsigned int rows);

const char*
mms_attachment_image_prepare_filename(
MMSAttachmentImage* image);

#ifdef MMS_RESIZE_QT
gboolean
mms_attachment_image_resize_qt(
MMSAttachmentImage* image);
#endif

#endif /* JOLLA_MMS_ATTACHMENT_IMAGE_H */

/*
Expand Down
56 changes: 56 additions & 0 deletions mms-lib/src/mms_attachment_qt.cpp
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2013-2014 Jolla Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*
*/

extern "C" {
#define MMS_LOG_MODULE_NAME mms_attachment_log
#include "mms_lib_log.h"
#include "mms_attachment_image.h"
}

#ifdef MMS_RESIZE_QT

#include <QtGui/QtGui>

gboolean
mms_attachment_image_resize_qt(
MMSAttachmentImage* image)
{
gboolean ok = FALSE;
QImage qimage;
if (qimage.load(image->attachment.original_file)) {
const int w = qimage.width();
const int h = qimage.height();
const int step = mms_attachment_image_next_resize_step(image, w, h);
const char* fname = mms_attachment_image_prepare_filename(image);
const int w1 = w/(step+1);
QImage scaled = qimage.scaledToWidth(w1, Qt::SmoothTransformation);
if (scaled.save(fname)) {
MMS_DEBUG("Scaling %s (%dx%d -> %dx%d) with Qt", fname, w, h,
scaled.width(), scaled.height());
image->resize_step = step;
ok = TRUE;
}
}
return ok;
}

#endif /* MMS_RESIZE_QT */

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/

0 comments on commit 740f598

Please sign in to comment.