r15677 by christoph - first set of patches from Herm for #9109

scribus-commit scribus-commit at lists.scribus.net
Fri Oct 29 04:38:12 CEST 2010


Author: christoph
Date: Fri Oct 29 02:38:12 2010
New Revision: 15677

URL: http://scribus.info/websvn/listing.php?repname=Scribus&sc=1&rev=15677
Log:
first set of patches from Herm for #9109

Modified:
    trunk/Scribus/scribus/scimgdataloader_gmagick.cpp
    trunk/Scribus/scribus/scimgdataloader_gmagick.h
    trunk/Scribus/scribus/util_formats.cpp

Modified: trunk/Scribus/scribus/scimgdataloader_gmagick.cpp
URL: http://scribus.info/websvn/diff.php?repname=Scribus&rev=15677&path=/trunk/Scribus/scribus/scimgdataloader_gmagick.cpp
==============================================================================
--- trunk/Scribus/scribus/scimgdataloader_gmagick.cpp (original)
+++ trunk/Scribus/scribus/scimgdataloader_gmagick.cpp Fri Oct 29 02:38:12 2010
@@ -9,16 +9,38 @@
 #include <QObject>
 #include <QDebug>
 #include <setjmp.h>
+#include "scimgdataloader_gmagick.h"
 #include "scconfig.h"
-#include "scimgdataloader_gmagick.h"
-#include <Magick++.h>
-using namespace Magick;
-
-#ifndef ScaleQuantumToChar
-    #define ScaleQuantumToChar MagickLib::ScaleQuantumToChar
-#endif
-
-static bool read_cmyk(Image *input, RawImage *output, int width, int height)
+
+
+
+bool ScImgDataLoader_GMagick::gm_initialized = false;
+
+ScImgDataLoader_GMagick::ScImgDataLoader_GMagick(void)
+{
+	initSupportedFormatList();
+	m_useRawImage = true;
+}
+
+
+void ScImgDataLoader_GMagick::initSupportedFormatList(void)
+{
+	//This is unused! See util_formats.{h,cpp}
+	m_supportedFormats.clear();
+	m_supportedFormats.append("bmp"); //TODO
+}
+
+
+void ScImgDataLoader_GMagick::initGraphicsMagick()
+{
+	if (!gm_initialized) {
+		InitializeMagick(0); //TODO: Get path
+		gm_initialized = true;
+	}
+}
+
+
+bool ScImgDataLoader_GMagick::readCMYK(Image *input, RawImage *output, int width, int height)
 {
 	/* Mapping:
 		red:     cyan
@@ -33,23 +55,30 @@
 	#define GetYellowSample(p) (p.blue)
 	#define GetCMYKBlackSample(p) (p.opacity)
 	#define GetAlphaSample(p) (p)
-	
-	bool hasAlpha = input->matte();
+
+	bool hasAlpha = input->matte;
+
 	if (!output->create(width, height, hasAlpha ? 5 : 4)) return false;
-	const PixelPacket *pixels = input->getConstPixels(0, 0, width, height);
+
+	ExceptionInfo exception;
+	GetExceptionInfo(&exception);
+	const PixelPacket *pixels = AcquireImagePixels(input, 0, 0, width, height, &exception);
+	if (exception.severity != UndefinedException)
+		CatchException(&exception);
 	if (!pixels) {
-	   qCritical() << QObject::tr("Could not get pixel data!");
-	   return false;
-    }
-
-    const IndexPacket *alpha;
+		qCritical() << QObject::tr("Could not get pixel data!");
+		return false;
+	}
+
+    const IndexPacket *alpha = 0;
     if (hasAlpha) {
-        alpha = input->getConstIndexes();
+        alpha = AccessImmutableIndexes(input);
         if (!alpha) {
             qCritical() << QObject::tr("Could not get alpha channel data!");
             return false;
         }
     }
+
 	unsigned char *buffer = output->bits();
 	if (!buffer) {
 	   qCritical() << QObject::tr("Could not allocate output buffer!");
@@ -70,26 +99,32 @@
 }
 
 
-static bool read_rgb(Image *input, QImage *output, int width, int height)
+bool ScImgDataLoader_GMagick::readRGB(Image *input, QImage *output, int width, int height)
 {
 	//Copied from GraphicsMagick header and modified
-	#define GetRedSample(p) (p.red)
-	#define GetGreenSample(p) (p.green)
-	#define GetBlueSample(p) (p.blue)
-	#define GetRGBAlphaSample(p) (p.opacity)
-	
-	bool hasAlpha = input->matte();
-	const PixelPacket *pixels = input->getConstPixels(0, 0, width, height);
-    if (!pixels) {
-	   qCritical() << QObject::tr("Could not get pixel data!");
-	   return false;
-    }
+#define GetRedSample(p) (p.red)
+#define GetGreenSample(p) (p.green)
+#define GetBlueSample(p) (p.blue)
+#define GetRGBAlphaSample(p) (p.opacity)
+
+	bool hasAlpha = input->matte;
+
+	ExceptionInfo exception;
+	GetExceptionInfo(&exception);
+	const PixelPacket *pixels = AcquireImagePixels(input, 0, 0, width, height, &exception);
+	if (exception.severity != UndefinedException)
+		CatchException(&exception);
+	if (!pixels) {
+		qCritical() << QObject::tr("Could not get pixel data!");
+		return false;
+	}
+
 	unsigned char *buffer = output->bits();
 	if (!buffer) {
-	   qCritical() << QObject::tr("Could not allocate output buffer!");
-	   return false;
-    }
-    
+		qCritical() << QObject::tr("Could not allocate output buffer!");
+		return false;
+	}
+
 	int i;
 	for (i = 0; i < width*height; i++) {
 		*buffer++ = ScaleQuantumToChar(GetBlueSample(pixels[i]));
@@ -104,139 +139,123 @@
 	return true;
 }
 
-ScImgDataLoader_GMagick::ScImgDataLoader_GMagick(void)
-{
-	initSupportedFormatList();
-	m_useRawImage = true;
-}
-
-void ScImgDataLoader_GMagick::initSupportedFormatList(void)
-{
-	//This is unused! See util_formats.{h,cpp}
-	m_supportedFormats.clear();
-	m_supportedFormats.append("bmp"); //TODO
-}
-
-void ScImgDataLoader_GMagick::loadEmbeddedProfile(const QString& fn, int /*page*/)
-{	
-	if (!QFile::exists(fn)) {
-		m_embeddedProfile.resize(0);
-		m_profileComponents = 0;
-		return;
-	}
-	
-	try {
-		Image image(std::string(fn.toUtf8()));
-		Blob icc = image.iccColorProfile();
-		
-		if (image.colorSpace() == CMYKColorspace) {
-			m_profileComponents = 4;
-		} else if (image.colorSpace() == RGBColorspace) {
-			m_profileComponents = 3;
-		} else {
-			m_profileComponents = 0;
-			m_embeddedProfile.resize(0);
-		}
-		
-		m_embeddedProfile.resize(icc.length());
-		
-		char *dest = m_embeddedProfile.data();
-		const char *src = (const char *)icc.data();
-		
-		unsigned int i;
-		for (i=0; i<icc.length(); i++) {
-			dest[i] = src[i];
-		}
-
-	} catch (std::exception &error_) {
-		m_profileComponents = 0;
-		m_embeddedProfile.resize(0);
-		
-		qDebug() << "GraphicsMagic Exception in ScImgDataLoader_GMagick::loadEmbeddedProfile(" 
-			<< fn << "):" << error_.what();
-		
-		return;
-	}
-}
+
+bool ScImgDataLoader_GMagick::loadPicture(const QString& fn, int /*page*/, int res, bool thumbnail)
+{
+	initGraphicsMagick();
+	qDebug() << "Loading image" << fn << "Tumbnail:" << thumbnail;
+
+	if (!QFile::exists(fn))
+		return false;
+
+	initialize();
+
+	ExceptionInfo exception;
+	GetExceptionInfo(&exception);
+	ImageInfo *image_info = CloneImageInfo(0);
+	strcpy(image_info->filename, fn.toUtf8().data());
+	image_info->units = PixelsPerInchResolution;
+	Image *image = ReadImage(image_info, &exception);
+	if (exception.severity != UndefinedException)
+		CatchException(&exception);
+	if (!image) {
+		qCritical() << "Failed to read image" << fn;
+		return false;
+	}
+
+	qDebug() << "has matte(alpha):" << image->matte;
+	int width = image->columns;// .baseColumns();
+	int height = image->rows;// baseRows();
+
+	double xres = image->x_resolution; //.xResolution();
+	double yres = image->y_resolution; //.yResolution();
+
+	if (image->colorspace == CMYKColorspace) {
+		qDebug() << "CMYK image";
+		m_useRawImage = true;
+		if (!readCMYK(image, &r_image, width, height)) return false;
+		m_imageInfoRecord.colorspace = ColorSpaceCMYK;
+		m_pixelFormat = (r_image.channels() == 5) ? Format_CMYKA_8 : Format_CMYK_8;
+	} else {
+		m_useRawImage = false;
+		m_image = QImage(width, height, QImage::Format_ARGB32);
+		if (!readRGB(image, &m_image, width, height)) return false;
+		m_imageInfoRecord.colorspace = ColorSpaceRGB;
+		m_pixelFormat = Format_BGRA_8;
+	}
+	m_imageInfoRecord.type = ImageTypeOther;
+	m_imageInfoRecord.exifDataValid = false;
+	m_imageInfoRecord.xres = qMax(72, qRound(xres));
+	m_imageInfoRecord.yres = qMax(72, qRound(yres));
+	m_imageInfoRecord.BBoxX = 0;
+	m_imageInfoRecord.BBoxH = m_image.height();
+	m_imageInfoRecord.valid = true;
+	return true;
+}
+
 
 bool ScImgDataLoader_GMagick::preloadAlphaChannel(const QString& fn, int /*page*/, int res, bool& hasAlpha)
 {
+	initGraphicsMagick();
 	qDebug() << "GMAGICK: preloadAlpha" << fn;
-	
 	initialize();
 	hasAlpha = false;
-	
+
 	if (!QFile::exists(fn))
 		return false;
 
-	try {
-		Image image(std::string(fn.toUtf8()));
-		
-		hasAlpha = image.matte();
-		
-		if (!hasAlpha) {
-			return true; //No need to load any image data
-		} else {
-			return loadPicture(fn, 0, 0, false);
-		}
-		
-	} catch (std::exception &error_) {
-		qDebug() << "GraphicsMagic Exception in ScImgDataLoader_GMagick::preloadAlphaChannel(" 
-			<< fn << "):" << error_.what();
-		return false;
-	}
-	return true;
-}
-
-bool ScImgDataLoader_GMagick::loadPicture(const QString& fn, int /*page*/, int res, bool thumbnail)
-{
-	qDebug() << "Loading image" << fn << "Tumbnail:" << thumbnail;
-
-	if (!QFile::exists(fn))
-		return false;
-		
-	initialize();
-	
-	try {
-		Image image(std::string(fn.toUtf8()));
-		
-		qDebug() << "Detected format:" << QString(image.format().c_str()) 
-			<< ", has matte(alpha):" << image.matte();
-
-		int width = image.baseColumns();
-		int height = image.baseRows();
-		
-		image.resolutionUnits(PixelsPerInchResolution);
-		double xres = image.xResolution();
-		double yres = image.yResolution();
-		int resInf = m_imageInfoRecord.lowResType;
-		
-		if (image.colorSpace() == CMYKColorspace) {
-			qDebug() << "CMYK image";
-			m_useRawImage = true;
-			if (!read_cmyk(&image, &r_image, width, height)) return false;
-			m_imageInfoRecord.colorspace = ColorSpaceCMYK;
-			m_pixelFormat = (r_image.channels() == 5) ? Format_CMYKA_8 : Format_CMYK_8;
-		} else {
-			m_useRawImage = false;
-			m_image = QImage(width, height, QImage::Format_ARGB32);
-			if (!read_rgb(&image, &m_image, width, height)) return false;
-			m_imageInfoRecord.colorspace = ColorSpaceRGB;
-			m_pixelFormat = Format_BGRA_8;
-		}
-		m_imageInfoRecord.type = ImageTypeOther;
-		m_imageInfoRecord.exifDataValid = false;
-		m_imageInfoRecord.xres = qMax(72, qRound(xres));
-		m_imageInfoRecord.yres = qMax(72, qRound(yres));
-		m_imageInfoRecord.lowResType = resInf;
-		m_imageInfoRecord.BBoxX = 0;
-		m_imageInfoRecord.BBoxH = m_image.height();
-		m_imageInfoRecord.valid = true;
-	} catch (std::exception &error_) {
-		qDebug() << "GraphicsMagic Exception in ScImgDataLoader_GMagick::loadImage(" 
-			<< fn << "):" << error_.what();
-		return false;
-	}
-	return true;
-}
-
+	ExceptionInfo exception;
+	GetExceptionInfo(&exception);
+	ImageInfo *image_info = CloneImageInfo(0);
+	strcpy(image_info->filename, fn.toUtf8().data());
+	image_info->units = PixelsPerInchResolution;
+	Image *image = PingImage(image_info, &exception);
+	if (exception.severity != UndefinedException)
+		CatchException(&exception);
+	if (!image) {
+		qCritical() << "Failed to read image" << fn;
+		return false;
+	}
+
+	hasAlpha = image->matte;
+	if (!hasAlpha) return true;
+	return loadPicture(fn, 0, 0, false);
+}
+
+
+void ScImgDataLoader_GMagick::loadEmbeddedProfile(const QString& fn, int /*page*/)
+{
+	qDebug() << "GMAGICK: loadEmbeddedProfile";
+
+	initGraphicsMagick();
+	m_embeddedProfile.resize(0);
+	m_profileComponents = 0;
+	if (!QFile::exists(fn)) return;
+
+	ExceptionInfo exception;
+	GetExceptionInfo(&exception);
+	ImageInfo *image_info = CloneImageInfo(0);
+	strcpy(image_info->filename, fn.toUtf8().data());
+	image_info->units = PixelsPerInchResolution;
+	Image *image = ReadImage(image_info, &exception);
+	if (exception.severity != UndefinedException)
+		CatchException(&exception);
+	if (!image) {
+		qCritical() << "Failed to read image" << fn;
+		return;
+	}
+
+	size_t length = 0;
+	const unsigned char *src = GetImageProfile(image, "ICM", &length);
+	char *dest = m_embeddedProfile.data();
+
+	if (image->colorspace == CMYKColorspace) {
+		m_profileComponents = 4;
+	} else if (image->colorspace == RGBColorspace) {
+		m_profileComponents = 3;
+	}
+
+	m_embeddedProfile.resize(length);
+	memcpy(dest, src, length);
+}
+

Modified: trunk/Scribus/scribus/scimgdataloader_gmagick.h
URL: http://scribus.info/websvn/diff.php?repname=Scribus&rev=15677&path=/trunk/Scribus/scribus/scimgdataloader_gmagick.h
==============================================================================
--- trunk/Scribus/scribus/scimgdataloader_gmagick.h (original)
+++ trunk/Scribus/scribus/scimgdataloader_gmagick.h Fri Oct 29 02:38:12 2010
@@ -8,13 +8,17 @@
 #define SCIMGDATALOADER_GMAGICK_H
 
 #include "scimgdataloader.h"
-
+#include <magick/api.h>
 
 class ScImgDataLoader_GMagick : public ScImgDataLoader
 {
 protected:
 	void initSupportedFormatList();
 	bool m_useRawImage;
+private:
+	static bool gm_initialized;
+	bool readCMYK(Image *input, RawImage *output, int width, int height);
+	bool readRGB(Image *input, QImage *output, int width, int height);
 
 public:
 	ScImgDataLoader_GMagick(void);
@@ -23,6 +27,7 @@
 	virtual void loadEmbeddedProfile(const QString& fn, int page = 0);
 	virtual bool loadPicture(const QString& fn, int page, int res, bool thumbnail);
 	virtual bool useRawImage() { return m_useRawImage; }
+	virtual void initGraphicsMagick();
 };
 
 #endif

Modified: trunk/Scribus/scribus/util_formats.cpp
URL: http://scribus.info/websvn/diff.php?repname=Scribus&rev=15677&path=/trunk/Scribus/scribus/util_formats.cpp
==============================================================================
--- trunk/Scribus/scribus/util_formats.cpp (original)
+++ trunk/Scribus/scribus/util_formats.cpp Fri Oct 29 02:38:12 2010
@@ -36,7 +36,7 @@
 #ifdef GMAGICK_FOUND
 	m_fmts.insert(FormatsManager::GMAGICK, QStringList() << "xbm" << "tga" << "ptif" << "ppm" << "pnm" << "pgm" << "pcds" << "pcd" << "pbm" << "mng" << "ico" << "gif" << "fax" << "dpx" << "bmp" << "xcf");
 #endif
-	m_fmts.insert(FormatsManager::UNICONV, QStringList() << "cdr" << "cdt" << "ccx" << "cmx" << "aff" << "sk" << "sk1");
+	m_fmts.insert(FormatsManager::UNICONV, QStringList() << "cdr" << "cdt" << "ccx" << "cmx" << "aff" << "sk" << "sk1" << "cgm" << "plt" << "dxf" << "dst" << "pes" << "exp" << "pcs");
 	m_fmts.insert(FormatsManager::PCT,  QStringList() << "pct" << "pic" << "pict");
 	
 	m_fmtNames[FormatsManager::EPS]  = QObject::tr("Encapsulated PostScript \"*.eps\"");




More information about the scribus-commit mailing list