Back to index

scribus-ng  1.3.4.dfsg+svn20071115
rawimage.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "rawimage.h"
00008 
00009 RawImage::RawImage()
00010 {
00011        m_width = 0;
00012        m_height = 0;
00013        m_channels = 0;
00014        resize(0);
00015 }
00016 
00017 RawImage::RawImage( int width, int height, int channels )
00018 {
00019        create(width, height, channels);
00020 }
00021 
00022 RawImage::~RawImage()
00023 {
00024        resize(0);
00025 }
00026 
00027 bool RawImage::create( int width, int height, int channels )
00028 {
00029        m_width = width;
00030        m_height = height;
00031        m_channels = channels;
00032        return resize(width * height * channels);
00033 }
00034 
00035 uchar *RawImage::scanLine(int row)
00036 {
00037        if (row < m_height)
00038               return (uchar*)(data() + (row * m_channels * m_width));
00039        else
00040               return (uchar*)data();
00041 }
00042 
00043 void RawImage::setAlpha(int x, int y, int alpha)
00044 {
00045        uchar *d;
00046        if ((y < m_height) && (x < m_width))
00047        {
00048               d = (uchar*)(data() + (y * m_channels * m_width) + (x * m_channels));
00049               d[m_channels-1] = alpha;
00050        }
00051 }
00052 
00053 QImage RawImage::convertToQImage(bool cmyk, bool raw)
00054 {
00055        int chans = channels();
00056        QImage img;
00057        QRgb *ptr;
00058        uchar *src;
00059        uchar cr, cg, cb, ck, ca;
00060        img.create(width(), height(), 32);
00061        if (raw)
00062        {
00063               for (int i = 0; i < height(); i++)
00064               {
00065                      ptr = (QRgb *)img.scanLine(i);
00066                      src = scanLine(i);
00067                      for (int j = 0; j < width(); j++)
00068                      {
00069                             *ptr++ = qRgba(src[0],src[1],src[2],src[3]);
00070                             src += chans;
00071                      }
00072               }
00073        }
00074        else
00075        {
00076               img.setAlphaBuffer( true );
00077               for (int i = 0; i < height(); i++)
00078               {
00079                      ptr = (QRgb *)img.scanLine(i);
00080                      src = scanLine(i);
00081                      for (int j = 0; j < width(); j++)
00082                      {
00083                             if (chans > 1)
00084                             {
00085                                    if (cmyk)
00086                                    {
00087                                           ck = src[3];
00088                                           cr = 255 - QMIN(255, src[0] + ck);
00089                                           cg = 255 - QMIN(255, src[1] + ck);
00090                                           cb = 255 - QMIN(255, src[2] + ck);
00091                                           if (chans > 4)
00092                                           {
00093                                                  ca = src[4];
00094                                                  *ptr++ = qRgba(cr,cg,cb,ca);
00095                                           }
00096                                           else
00097                                                  *ptr++ = qRgba(cr,cg,cb,255);
00098                                    }
00099                                    else
00100                                    {
00101                                           if (chans > 3)
00102                                                  *ptr++ = qRgba(src[0],src[1],src[2],src[3]);
00103                                           else
00104                                                  *ptr++ = qRgba(src[0],src[1],src[2],255);
00105                                    }
00106                             }
00107                             else
00108                                    *ptr++ = qRgba(src[0],src[0],src[0],255);
00109                             src += chans;
00110                      }
00111               }
00112        }
00113        return img;
00114 }