r18118 by fschmid - CGM-Import: added support for 1, 2 and 4 bit depth images.

scribus-commit scribus-commit at lists.scribus.net
Mon Jan 28 21:59:23 UTC 2013


Author: fschmid
Date: Mon Jan 28 21:59:23 2013
New Revision: 18118

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=18118
Log:
CGM-Import: added support for 1, 2 and 4 bit depth images.

Modified:
    trunk/Scribus/scribus/plugins/import/cgm/importcgm.cpp
    trunk/Scribus/scribus/plugins/import/cgm/importcgm.h

Modified: trunk/Scribus/scribus/plugins/import/cgm/importcgm.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18118&path=/trunk/Scribus/scribus/plugins/import/cgm/importcgm.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/import/cgm/importcgm.cpp (original)
+++ trunk/Scribus/scribus/plugins/import/cgm/importcgm.cpp Mon Jan 28 21:59:23 2013
@@ -54,6 +54,49 @@
 #include "util_math.h"
 
 extern SCRIBUS_API ScribusQApp * ScQApp;
+
+ScBitReader::ScBitReader(QByteArray &data)
+{
+	buffer = data;
+	actBit = 7;
+	actByte = 0;
+}
+
+ScBitReader::~ScBitReader()
+{
+}
+
+quint32 ScBitReader::getUInt(uint size)
+{
+	quint32 ret = 0;
+	if (size > 32)
+		return 0;
+	quint8 dat = buffer[actByte];
+	for (uint c = 0; c < size; c++)
+	{
+		ret = (ret << 1) | ((dat & (0x01 << actBit)) >> actBit);
+		actBit--;
+		if (actBit < 0)
+		{
+			actBit = 7;
+			actByte++;
+			if (actByte >= buffer.count())
+				break;
+			dat = buffer[actByte];
+		}
+	}
+	return ret;
+}
+
+void ScBitReader::alignToWord()
+{
+	if (actByte < buffer.count() - 1)
+	{
+		actByte++;
+		actByte += actByte % 2;
+		actBit = 7;
+	}
+}
 
 CgmPlug::CgmPlug(ScribusDoc* doc, int flags)
 {
@@ -852,7 +895,7 @@
 			sc = getBinaryReal(ts, 0, 9);
 		if (metaFileScaleMode != 0)
 			metaFileScale = sc;
-//		qDebug() << "SCALING MODE" << metaFileScaleMode << metaFileScale;
+	//	qDebug() << "SCALING MODE" << metaFileScaleMode << metaFileScale;
 	}
 	else if (elemID == 2)
 	{
@@ -864,7 +907,15 @@
 	{
 		ts >> data;
 		lineWidthMode = data;
-		// qDebug() << "LINE WIDTH SPECIFICATION MODE" << lineWidthMode;
+		if (lineWidthMode == 0)
+			lineWidth = 0; // qMax(vdcWidth, vdcHeight) / 1000;
+		else if (lineWidthMode == 1)
+			lineWidth = 1.0;
+		else if (lineWidthMode == 2)
+			lineWidth = 0.001;
+		else if (lineWidthMode == 3)
+			lineWidth = 0.35;
+	//	qDebug() << "LINE WIDTH SPECIFICATION MODE" << lineWidthMode;
 	}
 	else if (elemID == 4)
 	{
@@ -877,14 +928,14 @@
 		ts >> data;
 		edgeWidthMode = data;
 		if (edgeWidthMode == 0)
-			edgeWidth = qMax(vdcWidth, vdcHeight) / 1000;
+			edgeWidth = 0; // qMax(vdcWidth, vdcHeight) / 1000;
 		else if (edgeWidthMode == 1)
 			edgeWidth = 1.0;
 		else if (edgeWidthMode == 2)
 			edgeWidth = 0.001;
 		else if (edgeWidthMode == 3)
 			edgeWidth = 0.35;
-		// qDebug() << "EDGE WIDTH SPECIFICATION MODE" << edgeWidthMode;
+	//	qDebug() << "EDGE WIDTH SPECIFICATION MODE" << edgeWidthMode;
 	}
 	else if (elemID == 6)
 	{
@@ -901,7 +952,7 @@
 		vdcHeight = vd.height();
 		metaScale = 400.0 / qMax(vdcWidth, vdcHeight);
 		if (lineWidthMode == 0)
-			lineWidth = qMax(vdcWidth, vdcHeight) / 1000;
+			lineWidth = 0; // qMax(vdcWidth, vdcHeight) / 1000;
 		else if (lineWidthMode == 1)
 			lineWidth = 1.0;
 		else if (lineWidthMode == 2)
@@ -913,7 +964,7 @@
 		vcdSet = true;
 		if (!clipSet)
 			clipRect = QRectF(vd.left() * metaScale, vd.top() * metaScale, vdcWidth * metaScale, vdcHeight * metaScale);
-//		qDebug() << "VDC EXTENT" << vd.left() << vd.top() << vdcWidth << vdcHeight << metaScale;
+	//	qDebug() << "VDC EXTENT" << vd.left() << vd.top() << vdcWidth << vdcHeight << metaScale;
 	}
 	else if (elemID == 7)
 	{
@@ -1361,17 +1412,11 @@
 		int t_colorIndexPrecision = colorIndexPrecision;
 		colorPrecision = getBinaryUInt(ts, intPrecision);
 		colorIndexPrecision = colorPrecision;
-		qDebug() << "CELL ARRAY Size" << nx << ny << "Compression" << mode << "Color Prec" << colorPrecision;
+//		qDebug() << "CELL ARRAY at" << pos << "Size" << nx << ny << "Compression" << mode << "Color Prec" << colorPrecision;
 		if (colorPrecision == 0)
 		{
 			colorPrecision = t_colorPrecision;
 			colorIndexPrecision = t_colorIndexPrecision;
-		}
-		if (colorPrecision < 8)
-		{
-			colorPrecision = t_colorPrecision;
-			colorIndexPrecision = t_colorIndexPrecision;
-			return;
 		}
 		ts >> mode;
 		int bytesRead = ts.device()->pos() - pos;
@@ -1410,52 +1455,102 @@
 			QByteArray rD = ts.device()->read(pLen);
 			imageData.append(rD);
 		}
-		QDataStream istr(imageData);
-		istr.setByteOrder(QDataStream::BigEndian);
-		for (int yy = 0; yy < ny; yy++)
-		{
-			ScColor color;
-			QRgb *s = (QRgb*)(image.scanLine(yy));
-			if (mode == 1)
-			{
-				for (int xx = 0; xx < nx; xx++)
+		if (colorPrecision < 8)
+		{
+			ScBitReader *breader = new ScBitReader(imageData);
+			for (int yy = 0; yy < ny; yy++)
+			{
+				ScColor color;
+				QRgb *s = (QRgb*)(image.scanLine(yy));
+				if (mode == 1)
 				{
-					if (colorMode == 0)
-						color = m_Doc->PageColors[getBinaryIndexedColor(istr)];
-					else
-						color = getBinaryDirectColor(istr);
-					QColor co = color.getRawRGBColor();
-					*s++ = qRgba(co.red(), co.green(), co.blue(), 255);
-				}
-			}
-			else
-			{
-				int xx = 0;
-				while (xx < nx)
-				{
-					int counter = getBinaryUInt(istr, intPrecision);
-					if ((counter > nx) || (counter == 0))
+					for (int xx = 0; xx < nx; xx++)
 					{
-						importRunning = false;
-						return;
-					}
-					if (colorMode == 0)
-						color = m_Doc->PageColors[getBinaryIndexedColor(istr)];
-					else
-						color = getBinaryDirectColor(istr);
-					QColor co = color.getRawRGBColor();
-					for (int xc = 0; xc < counter; xc++)
-					{
+						if (colorMode == 0)
+							color = m_Doc->PageColors[getBinaryIndexedColor(breader)];
+						else
+							color = getBinaryDirectColor(breader);
+						QColor co = color.getRawRGBColor();
 						*s++ = qRgba(co.red(), co.green(), co.blue(), 255);
-						xx++;
-						if (xx >= nx)
-							break;
 					}
 				}
-			}
-			uint adj = istr.device()->pos() % 2;
-			if (adj != 0)
-				istr.skipRawData(1);
+				else
+				{
+					int xx = 0;
+					while (xx < nx)
+					{
+						int counter = breader->getUInt(intPrecision);
+						if ((counter > nx) || (counter == 0))
+						{
+							importRunning = false;
+							return;
+						}
+						if (colorMode == 0)
+							color = m_Doc->PageColors[getBinaryIndexedColor(breader)];
+						else
+							color = getBinaryDirectColor(breader);
+						QColor co = color.getRawRGBColor();
+						for (int xc = 0; xc < counter; xc++)
+						{
+							*s++ = qRgba(co.red(), co.green(), co.blue(), 255);
+							xx++;
+							if (xx >= nx)
+								break;
+						}
+					}
+				}
+				breader->alignToWord();
+			}
+		}
+		else
+		{
+			QDataStream istr(imageData);
+			istr.setByteOrder(QDataStream::BigEndian);
+			for (int yy = 0; yy < ny; yy++)
+			{
+				ScColor color;
+				QRgb *s = (QRgb*)(image.scanLine(yy));
+				if (mode == 1)
+				{
+					for (int xx = 0; xx < nx; xx++)
+					{
+						if (colorMode == 0)
+							color = m_Doc->PageColors[getBinaryIndexedColor(istr)];
+						else
+							color = getBinaryDirectColor(istr);
+						QColor co = color.getRawRGBColor();
+						*s++ = qRgba(co.red(), co.green(), co.blue(), 255);
+					}
+				}
+				else
+				{
+					int xx = 0;
+					while (xx < nx)
+					{
+						int counter = getBinaryUInt(istr, intPrecision);
+						if ((counter > nx) || (counter == 0))
+						{
+							importRunning = false;
+							return;
+						}
+						if (colorMode == 0)
+							color = m_Doc->PageColors[getBinaryIndexedColor(istr)];
+						else
+							color = getBinaryDirectColor(istr);
+						QColor co = color.getRawRGBColor();
+						for (int xc = 0; xc < counter; xc++)
+						{
+							*s++ = qRgba(co.red(), co.green(), co.blue(), 255);
+							xx++;
+							if (xx >= nx)
+								break;
+						}
+					}
+				}
+				uint adj = istr.device()->pos() % 2;
+				if (adj != 0)
+					istr.skipRawData(1);
+			}
 		}
 		ite->tempImageFile = new QTemporaryFile(QDir::tempPath() + "/scribus_temp_cgm_XXXXXX.png");
 		ite->tempImageFile->open();
@@ -2708,6 +2803,34 @@
 			bytesRead += posN - posA;
 		}
 	}
+}
+
+ScColor CgmPlug::getBinaryDirectColor(ScBitReader *breader)
+{
+	ScColor ret;
+	if (colorModel == 1)		// RGB
+	{
+		uint r = breader->getUInt(colorPrecision);
+		uint g = breader->getUInt(colorPrecision);
+		uint b = breader->getUInt(colorPrecision);
+		r = qRound(r * (maxColor - minColor) / static_cast<double>(maxColor));
+		g = qRound(g * (maxColor - minColor) / static_cast<double>(maxColor));
+		b = qRound(b * (maxColor - minColor) / static_cast<double>(maxColor));
+		ret = ScColor(r, g, b);
+	}
+	else if (colorModel == 4)	// CMYK
+	{
+		uint c = breader->getUInt(colorPrecision);
+		uint m = breader->getUInt(colorPrecision);
+		uint y = breader->getUInt(colorPrecision);
+		uint k = breader->getUInt(colorPrecision);
+		c = qRound(c * (maxColor - minColor) / static_cast<double>(maxColor));
+		m = qRound(m * (maxColor - minColor) / static_cast<double>(maxColor));
+		y = qRound(y * (maxColor - minColor) / static_cast<double>(maxColor));
+		k = qRound(k * (maxColor - minColor) / static_cast<double>(maxColor));
+		ret = ScColor(c, m, y, k);
+	}
+	return ret;
 }
 
 ScColor CgmPlug::getBinaryDirectColor(QDataStream &ts)
@@ -2861,6 +2984,15 @@
 	return ret;
 }
 
+QString CgmPlug::getBinaryIndexedColor(ScBitReader *breader)
+{
+	QString ret = "Black";
+	uint c = breader->getUInt(colorIndexPrecision);
+	if (ColorTableMap.contains(c) && (c <= maxColorIndex))
+		ret = ColorTableMap[c];
+	return ret;
+}
+
 QString CgmPlug::getBinaryIndexedColor(QDataStream &ts)
 {
 	QString ret = "Black";

Modified: trunk/Scribus/scribus/plugins/import/cgm/importcgm.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18118&path=/trunk/Scribus/scribus/plugins/import/cgm/importcgm.h
==============================================================================
--- trunk/Scribus/scribus/plugins/import/cgm/importcgm.h (original)
+++ trunk/Scribus/scribus/plugins/import/cgm/importcgm.h Mon Jan 28 21:59:23 2013
@@ -30,6 +30,19 @@
 class ScribusDoc;
 class Selection;
 class TransactionSettings;
+
+class ScBitReader
+{
+	public:
+		ScBitReader(QByteArray &data);
+		~ScBitReader();
+		quint32 getUInt(uint size);
+		void alignToWord();
+	private:
+		int actByte;
+		int actBit;
+		QByteArray buffer;
+};
 
 //! \brief Cgm importer plugin
 class CgmPlug : public QObject
@@ -82,8 +95,10 @@
 	void    getBinaryBezierPath(QDataStream &ts, quint16 paramLen);
 	void    getBinaryPath(QDataStream &ts, quint16 paramLen, bool disjoint = false);
 	void    getBinaryColorTable(QDataStream &ts, quint16 paramLen);
+	ScColor getBinaryDirectColor(ScBitReader *breader);
+	ScColor getBinaryDirectColor(QDataStream &ts);
+	QString getBinaryIndexedColor(ScBitReader *breader);
 	QString getBinaryIndexedColor(QDataStream &ts);
-	ScColor getBinaryDirectColor(QDataStream &ts);
 	QString getBinaryColor(QDataStream &ts);
 	double  getBinaryDistance(QDataStream &ts);
 	QPointF getBinaryCoords(QDataStream &ts, bool raw = false);




More information about the scribus-commit mailing list