static QPixmap makeInvisible(unsigned flags, const QPixmap &p) { unsigned swapColor = flags & ICON_COLOR_MASK; char shift = (flags >> 8) & 0xFF; QImage image = p.convertToImage(); if (image.depth() != 32) image = image.convertDepth(32); unsigned int *data = (unsigned int*)image.bits(); for (int y = 0; y < image.width(); y++){ int x = image.width() / 2 - (y - image.height() / 2) * 2 / 3 + shift; if (x < 0) x = 0; if (x > image.width()) x = image.width(); unsigned int *line = data + y * (image.width()) + x; for (; x < image.width(); x++, line++){ QColor c(qRed(*line), qGreen(*line), qBlue(*line)); int a = qAlpha(*line); int h, s, v; c.hsv(&h, &s, &v); if (swapColor){ h = (swapColor * 2 - h) & 0xFF; c.setHsv(h, s / 2, v * 3 / 4); }else{ c.setHsv(h, s / 2, v * 3 / 4); } *line = qRgba(c.red(), c.green(), c.blue(), a); } } QPixmap pict; pict.convertFromImage(image); return pict; }
void fom ::setIconImage(const QImage &newImage) { if (newImage != image) { image = newImage.convertDepth(60); image.detach(); update(); updateGeometry(); } }
const QMimeSource *MyMimeSourceFactory::data(const QString &abs_name) const { QString name = abs_name; if (name.left(5) == "icon:"){ name = name.mid(5); const QIconSet *icons = Icon(name.latin1()); if (icons){ QImage img = icons->pixmap(QIconSet::Small, QIconSet::Normal).convertToImage(); img = img.convertDepth(32); ((QMimeSourceFactory*)this)->setImage(abs_name, img); } } return QMimeSourceFactory::data(abs_name); }
/* * Creates a thumbnail * * It seems that only plugins that create an image "from scratch", like * the TextCreator should directly use the specified size. * */ bool IsoFsCreator::create( const QString &p_path, // path of the cmg file to open int p_width, // maximum width for the preview int p_height, // maximum height for the preview QImage &p_img // location to fill with an image ) { QString v_image = "/.DirIcon"; // initialize the CmgUtl class with the cmg file _isoUtl = new CmgUtl(p_path.latin1()); // first, we try to load the /.DirIcon file, if (!this->readImage(v_image)) { // in case of failure, we check into the recipe file if (this->readRecipe(v_image)) { (void)this->readImage(v_image); } } // close the CmgUtl class delete _isoUtl; // load the image into the QImage buffer if (_buffer == NULL) return false; if (!p_img.loadFromData((const uchar *)_buffer,_size)) { free(_buffer); _buffer = NULL; return false; } if (p_img.depth() != 32) p_img=p_img.convertDepth(32); if (p_img.width()>p_width || p_img.height()>p_height) { p_img = p_img.smoothScale(p_width, p_height, QImage::ScaleMin); } free(_buffer); return true; }
void LibartCanvas::drawImage(QImage image, SVGStylableImpl *style, const SVGMatrixImpl *matrix, const KSVGPolygon& clippingPolygon) { SVGShapeImpl *shape = dynamic_cast<SVGShapeImpl *>(style); if(shape) { if(image.depth() != 32) image = image.convertDepth(32); ArtSVP *imageBorder = svpFromPolygon(clippingPolygon); ArtSVP *clipSvp = clipSingleSVP(imageBorder, shape); ArtDRect bbox; art_drect_svp(&bbox, clipSvp); // clamp to viewport int x0 = int(bbox.x0); int y0 = int(bbox.y0); // Use inclusive coords for x1/y1 for clipToBuffer int x1 = int(ceil(bbox.x1)) - 1; int y1 = int(ceil(bbox.y1)) - 1; if(x0 < int(m_width) && y0 < int(m_height) && x1 >= 0 && y1 >= 0) { clipToBuffer(x0, y0, x1, y1); QRect screenBBox(x0, y0, x1 - x0 + 1, y1 - y0 + 1); QByteArray mask = SVGMaskElementImpl::maskRectangle(shape, screenBBox); double affine[6]; KSVGHelper::matrixToAffine(matrix, affine); ksvg_art_rgb_affine_clip(clipSvp, m_buffer + x0 * nrChannels() + y0 * rowStride(), x0, y0, x1 + 1, y1 + 1, rowStride(), nrChannels(), image.bits(), image.width(), image.height(), image.width() * 4, affine, int(style->getOpacity() * 255), (const art_u8 *)mask.data()); } art_svp_free(imageBorder); art_svp_free(clipSvp); } }
//This function makes a pixmap which is based on icon, but has a number painted on it. QPixmap BoxContainerItem::calcComplexPixmap(const QPixmap &icon, const QColor &fgColour, const QFont *font, const int count) { QPixmap result(icon); QPixmap numberPixmap(icon.size()); QImage iconImage(icon.convertToImage()); QImage numberImage; QRgb *rgbline; QPainter p; //Make a transparent number; first make a white number on a black background. //This pixmap also is the base alpha-channel, the foreground colour is added later. numberPixmap.fill(Qt::black); p.begin(&numberPixmap, false); p.setPen(Qt::white); if(font) p.setFont(*font); p.drawText(icon.rect(), Qt::AlignCenter, QString::number(count)); p.end(); //Convert to image and add the alpha channel. numberImage = numberPixmap.convertToImage(); if(numberImage.depth() != 32) //Make sure depth is 32 (and thus can have an alpha channel) numberImage = numberImage.convertDepth(32); numberImage.setAlphaBuffer(true); //Enable alpha channel for(int xx = 0; xx < numberImage.height(); ++xx) { rgbline = (QRgb *)numberImage.scanLine(xx); for(int yy = 0; yy < numberImage.width(); ++yy) { //Set colour and alpha channel rgbline[ yy ] = qRgba(fgColour.red(), fgColour.green(), fgColour.blue(), qRed(rgbline[ yy ])); } } //Merge icon and number and convert to result. KIconEffect::overlay(iconImage, numberImage); result.convertFromImage(iconImage); return result; }
QImage QGLWidget::convertToGLFormat( const QImage& img ) { QImage res = img.convertDepth( 32 ); res = res.mirror(); if ( QImage::systemByteOrder() == QImage::BigEndian ) { // Qt has ARGB; OpenGL wants RGBA for ( int i=0; i < res.height(); i++ ) { uint *p = (uint*)res.scanLine( i ); uint *end = p + res.width(); while ( p < end ) { *p <<= 8; p++; } } } else { // Qt has ARGB; OpenGL wants ABGR (i.e. RGBA backwards) res = res.swapRGB(); } return res; }
void QAnimationWriter::appendFrame(const QImage& frm, const QPoint& offset) { QImage frame = frm.convertDepth(32); const int alignx = 1; if ( dev ) { if ( prev.isNull() || !d->canCompose() ) { d->setImage(frame); } else { bool done; int minx, maxx, miny, maxy; int w = frame.width(); int h = frame.height(); QRgb** jt = (QRgb**)frame.jumpTable(); QRgb** pjt = (QRgb**)prev.jumpTable() + offset.y(); // Find left edge of change done = FALSE; for (minx = 0; minx < w && !done; minx++) { for (int ty = 0; ty < h; ty++) { if ( jt[ty][minx] != pjt[ty][minx+offset.x()] ) { done = TRUE; break; } } } minx--; // Find right edge of change done = FALSE; for (maxx = w-1; maxx >= 0 && !done; maxx--) { for (int ty = 0; ty < h; ty++) { if ( jt[ty][maxx] != pjt[ty][maxx+offset.x()] ) { done = TRUE; break; } } } maxx++; // Find top edge of change done = FALSE; for (miny = 0; miny < h && !done; miny++) { for (int tx = 0; tx < w; tx++) { if ( jt[miny][tx] != pjt[miny][tx+offset.x()] ) { done = TRUE; break; } } } miny--; // Find right edge of change done = FALSE; for (maxy = h-1; maxy >= 0 && !done; maxy--) { for (int tx = 0; tx < w; tx++) { if ( jt[maxy][tx] != pjt[maxy][tx+offset.x()] ) { done = TRUE; break; } } } maxy++; if ( minx > maxx ) minx=maxx=0; if ( miny > maxy ) miny=maxy=0; if ( alignx > 1 ) { minx -= minx % alignx; maxx = maxx - maxx % alignx + alignx - 1; } int dw = maxx-minx+1; int dh = maxy-miny+1; QImage diff(dw, dh, 32); diff.setAlphaBuffer(TRUE); int x, y; for (y = 0; y < dh; y++) { QRgb* li = (QRgb*)frame.scanLine(y+miny)+minx; QRgb* lp = (QRgb*)prev.scanLine(y+miny+offset.y())+minx+offset.x(); QRgb* ld = (QRgb*)diff.scanLine(y); if ( alignx ) { for (x = 0; x < dw; x+=alignx) { int i; for (i=0; i<alignx; i++) { if ( li[x+i] != lp[x+i] ) break; } if ( i == alignx ) { // All the same for (i=0; i<alignx; i++) { ld[x+i] = qRgba(0,0,0,0); } } else { // Some different for (i=0; i<alignx; i++) { ld[x+i] = 0xff000000 | li[x+i]; } } } } else { for (x = 0; x < dw; x++) { if ( li[x] != lp[x] ) ld[x] = 0xff000000 | li[x]; else ld[x] = qRgba(0,0,0,0); } } } qDebug("%d,%d %d,%d",minx,miny,offset.x(),offset.y()); d->composeImage(diff,QPoint(minx,miny)+offset); } if ( prev.isNull() || prev.size() == frame.size() && offset == QPoint(0,0) ) { prev = frame; } else { bitBlt(&prev,offset.x(),offset.y(),&frame,0,0,frame.width(),frame.height()); } } }
int main( int*, char**) { QImage *img; img = new QImage( "in.png" ); int w,h; int y; img->setAlphaBuffer( TRUE ); *img = img->convertDepth( 32 ); w = img->width(); h = img->height(); #if 0 for ( y = 0; y < h; y ++ ) { uint *line = (uint*)img->scanLine( y ); for ( int x = 0; x < w; x++ ) { uint pixel = line[x]; int r = qRed(pixel); int g = qGreen(pixel); int b = qBlue(pixel); int min = QMIN( r, QMIN( g, b ) ); int max = QMAX( r, QMAX( g, b ) ); r -= min; g -= min; b -= min; if ( max !=min ) { r = (r*255)/(max-min); g = (g*255)/(max-min); b = (b*255)/(max-min); } int a = 255-min; a -= (max-min)/3; //hack more transparency for colors. line[x] = qRgba( r, g, b, a ); } } #endif *img = img->smoothScale( w/2, h/2 ); qDebug( "saving out.png"); img->save( "out.png", "PNG" ); w = img->width(); h = img->height(); QImage *img2 = new QImage( w, h, 32 ); img2->setAlphaBuffer( TRUE ); for ( y = 0; y < h; y++ ) { for ( int x = 0; x < w; x++ ) { QRgb shader = img->pixel( x, y ); int as = qAlpha(shader)/3; int r = (qRed(shader)*(255-as))/255; int g = (qGreen(shader)*(255-as))/255; int b = (qBlue(shader)*(255-as))/255; img2->setPixel( x, y, qRgba(r,g,b,as) ); } } img2->save( "outshade.png", "PNG" ); }
void get_transformed_pixmap(QPixmap* originalPixmap, QPixmap* destPixmap, int src_x, int src_y, int src_width, int src_height, int transform, bool hasAlpha) { QImage originalImage = originalPixmap->convertToImage(); if ( hasAlpha ) { // Qt's handling of the alpha channel in the conversion // process between QPixmap and QImage is buggy. // If the pixmap's pixels only have alpha values 0x00 and 0xFF // then the resulting QImage from conversion will return // false for hasAlphaBuffer(). // so we set our own flag instead of depending on Qt to // maintain alpha information. originalImage.setAlphaBuffer(TRUE); } /*Qt gives us this useful API that returns a section of a QImage*/ QImage sectionImage = originalImage.copy(src_x, src_y, src_width, src_height); /* Skip this pixel-by-pixel copy if there is no transform */ if (0 != transform) { QImage sectionImage32bpp = sectionImage.convertDepth(32); QImage processedImage; int nXOriginSrc = 0; int nYOriginSrc = 0; int nWidth = src_width; int nHeight = src_height; /*scan length of the source image*/ int imageWidth = src_width; /*number of rows of the source image*/ int imageHeight = src_height; int imgLen; int srcImgLen; int t_width; int t_height; int srcX; int srcY; int xStart; int yStart; int xIncr; int yIncr; int destX; int destY; int yCounter; int xCounter; int srcIndex; int destIndex; uchar* srcBits = NULL; uchar* destBits = NULL; uchar* srcBitsPtr = NULL; uchar* destBitsPtr = NULL; /* set dimensions of image being created, depending on transform */ if (transform & TRANSFORM_INVERTED_AXES) { t_width = src_height; t_height = src_width; } else { t_width = src_width; t_height = src_height; } /* width * height * 4 gives us the size of a 32 bpp image */ imgLen = nWidth * nHeight << 2; srcImgLen = imageWidth * imageHeight << 2; /* Qt specific */ processedImage.create(t_width, t_height, 32); srcBits = sectionImage32bpp.bits(); destBits = processedImage.bits(); /* ----------- */ if (transform & TRANSFORM_Y_FLIP) { yStart = nHeight-1; yIncr = -1; } else { yStart = 0; yIncr = +1; } if (transform & TRANSFORM_X_FLIP) { xStart = nWidth-1; xIncr = -1; } else { xStart = 0; xIncr = +1; } srcBitsPtr = srcBits; destBitsPtr = destBits; /* increment srcX,Y regular. increment destX,Y according to transform. this makes handling of mask and alpha values easier */ for (srcY = nYOriginSrc, destY = yStart, yCounter = 0; yCounter < nHeight; srcY++, destY+=yIncr, yCounter++) { /* in the current implementation we have source bitmap dimension as the width of the image and the height of the region destination bitmap is of the dimensions of the region */ for (srcX = nXOriginSrc, destX = xStart, xCounter = 0; xCounter < nWidth; srcX++, destX+=xIncr, xCounter++) { if ( transform & TRANSFORM_INVERTED_AXES ) { destIndex = ( ( (destX) * t_width) + (destY) ); } else { destIndex = ( ( (destY) * t_width) + (destX) ); } destBitsPtr = destBits + (destIndex * 4) ; srcIndex = (((srcY) * imageWidth) + (srcX)); srcBitsPtr = srcBits + (srcIndex * 4); /* copy the pixel that is pointed to */ *((int *)destBitsPtr) = *((int *)srcBitsPtr); } /*for x*/ } /* for y */ /* ---------- */ if(TRUE == sectionImage.hasAlphaBuffer() ) { processedImage.setAlphaBuffer(TRUE); } else { processedImage.setAlphaBuffer(FALSE); } destPixmap->convertFromImage(processedImage); } else { /* No transform, just copy the image sub-section */ destPixmap->convertFromImage(sectionImage); } }
extern "C" void gxpport_decodeimmutable_to_platformbuffer (unsigned char* srcBuffer, long length, unsigned char** ret_dataBuffer, long* ret_length, gxutl_native_image_error_codes* creationErrorPtr) { gxutl_image_format format; MIDP_ERROR err; unsigned int w, h; err = gxutl_image_get_info(srcBuffer, (unsigned int)length, &format, &w, &h); switch (err) { case MIDP_ERROR_NONE: break; /* continue */ case MIDP_ERROR_IMAGE_CORRUPTED: *creationErrorPtr = GXUTL_NATIVE_IMAGE_DECODING_ERROR; return; default: *creationErrorPtr = GXUTL_NATIVE_IMAGE_UNSUPPORTED_FORMAT_ERROR; return; } switch (format) { case GXUTL_IMAGE_FORMAT_RAW: /* already in RAW format. make a copy */ { unsigned char* dataBuffer = (unsigned char*) midpMalloc(length); if (NULL == dataBuffer) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; } else { memcpy(dataBuffer, srcBuffer, length); *ret_dataBuffer = dataBuffer; *ret_length = length; *creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR; } } break; case GXUTL_IMAGE_FORMAT_JPEG: case GXUTL_IMAGE_FORMAT_PNG: { QImage qimage; if (qimage.loadFromData((uchar*)srcBuffer, (unsigned int)length)) { int imgWidth = qimage.width(); int imgHeight = qimage.height(); if ((0 == imgWidth) || (0 == imgHeight)) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_DECODING_ERROR; } else { QImage image; if (IMAGE_DEPTH != qimage.depth()) { image = qimage.convertDepth(IMAGE_DEPTH); } else { image = qimage; } *ret_length = offsetof(gxutl_image_buffer_raw, data) + image.numBytes(); gxutl_image_buffer_raw *dataBuffer = (gxutl_image_buffer_raw *) midpMalloc(*ret_length); if (NULL == dataBuffer) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; } else { dataBuffer->width = (unsigned int)imgWidth; dataBuffer->height = (unsigned int)imgHeight; dataBuffer->hasAlpha = (unsigned int)image.hasAlphaBuffer(); memcpy(dataBuffer->header, gxutl_raw_header, 4); memcpy(dataBuffer->data, image.bits(), image.numBytes()); *ret_dataBuffer = (unsigned char *)dataBuffer; *creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR; } } } else { *creationErrorPtr = GXUTL_NATIVE_IMAGE_DECODING_ERROR; } } break; default: *creationErrorPtr = GXUTL_NATIVE_IMAGE_UNSUPPORTED_FORMAT_ERROR; break; } /* switch (format) */ }
bool KImGalleryPlugin::createThumb( const QString& imgName, const QString& sourceDirName, const QString& imgGalleryDir, const QString& imageFormat) { QImage img; const QString pixPath = sourceDirName + QString::fromLatin1("/") + imgName; if (m_copyFiles) { KURL srcURL = KURL::fromPathOrURL(pixPath); //kdDebug(90170) << "srcURL: " << srcURL << endl; KURL destURL = KURL::fromPathOrURL(imgGalleryDir + QString::fromLatin1("/images/") + imgName); //kdDebug(90170) << "destURL: " << destURL << endl; KIO::NetAccess::copy(srcURL, destURL, static_cast<KParts::Part *>(parent())->widget()); } const QString imgNameFormat = imgName + extension(imageFormat); const QString thumbDir = imgGalleryDir + QString::fromLatin1("/thumbs/"); int extent = m_configDlg->getThumbnailSize(); // this code is stolen from kdebase/kioslave/thumbnail/imagecreator.cpp // (c) 2000 gis and malte m_imgWidth = 120; // Setting the size of the images is m_imgHeight = 90; // required to generate faster 'loading' pages if ( img.load( pixPath ) ) { int w = img.width(), h = img.height(); // scale to pixie size // kdDebug(90170) << "w: " << w << " h: " << h << endl; // Resizing if to big if(w > extent || h > extent) { if(w > h) { h = (int)( (double)( h * extent ) / w ); if ( h == 0 ) h = 1; w = extent; Q_ASSERT( h <= extent ); } else { w = (int)( (double)( w * extent ) / h ); if ( w == 0 ) w = 1; h = extent; Q_ASSERT( w <= extent ); } const QImage scaleImg(img.smoothScale( w, h )); if ( scaleImg.width() != w || scaleImg.height() != h ) { kdDebug(90170) << "Resizing failed. Aborting." << endl; return false; } img = scaleImg; if (m_configDlg->colorDepthSet() == true ) { const QImage depthImg(img.convertDepth(m_configDlg->getColorDepth())); img = depthImg; } } kdDebug(90170) << "Saving thumbnail to: " << thumbDir + imgNameFormat << endl; if (!img.save(thumbDir + imgNameFormat, imageFormat.latin1())) { kdDebug(90170) << "Saving failed. Aborting." << endl; return false; } m_imgWidth = w; m_imgHeight = h; return true; } return false; }
void KdmPixmap::drawContents(QPainter *p, const QRect &r) { // choose the correct pixmap class PixmapStruct::PixmapClass *pClass = &pixmap.normal; if(state == Sactive && pixmap.active.present) pClass = &pixmap.active; if(state == Sprelight && pixmap.prelight.present) pClass = &pixmap.prelight; if(pClass->pixmap.isNull()) { if(pClass->fullpath.isEmpty()) // if neither is set, we're empty return; kdDebug() << "renderSVG\n"; renderSvg(pClass, area); } int px = area.left() + r.left(); int py = area.top() + r.top(); int sx = r.x(); int sy = r.y(); int sw = r.width(); int sh = r.height(); if(px < 0) { px *= -1; sx += px; px = 0; } if(py < 0) { py *= -1; sy += py; py = 0; } if(pClass->readyPixmap.isNull()) { QImage scaledImage; // use the loaded pixmap or a scaled version if needed if(area.size() != pClass->pixmap.size()) { if(pClass->fullpath.endsWith(".svg")) { kdDebug() << "renderSVG\n"; renderSvg(pClass, area); scaledImage = pClass->pixmap.convertToImage(); } else { kdDebug() << "convertFromImage\n"; QImage tempImage = pClass->pixmap.convertToImage(); scaledImage = tempImage.smoothScale(area.width(), area.height()); } } else scaledImage = pClass->pixmap.convertToImage(); bool haveTint = pClass->tint.rgb() != 0xFFFFFF; bool haveAlpha = pClass->alpha < 1.0; if(haveTint || haveAlpha) { // blend image(pix) with the given tint scaledImage = scaledImage.convertDepth(32); int w = scaledImage.width(); int h = scaledImage.height(); float tint_red = float(pClass->tint.red()) / 255; float tint_green = float(pClass->tint.green()) / 255; float tint_blue = float(pClass->tint.blue()) / 255; float tint_alpha = pClass->alpha; for(int y = 0; y < h; ++y) { QRgb *ls = (QRgb *)scaledImage.scanLine(y); for(int x = 0; x < w; ++x) { QRgb l = ls[x]; int r = int(qRed(l) * tint_red); int g = int(qGreen(l) * tint_green); int b = int(qBlue(l) * tint_blue); int a = int(qAlpha(l) * tint_alpha); ls[x] = qRgba(r, g, b, a); } } } pClass->readyPixmap.convertFromImage(scaledImage); } // kdDebug() << "Pixmap::drawContents " << pClass->readyPixmap.size() << " " << px << " " << py << " " << sx << " " << sy << " " << sw << " " << // sh << endl; p->drawPixmap(px, py, pClass->readyPixmap, sx, sy, sw, sh); }