/** * Load this tileset from the given tileset \a image. This will replace * existing tile images in this tileset with new ones. If the new image * contains more tiles than exist in the tileset new tiles will be * appended, if there are fewer tiles the excess images will be blanked. * * The tile width and height of this tileset must be higher than 0. * * @param image the image to load the tiles from * @param fileName the file name of the image, which will be remembered * as the image source of this tileset. * @return <code>true</code> if loading was successful, otherwise * returns <code>false</code> */ bool Tileset::loadFromImage(const QImage &image, const QString &fileName) { mImageReference.source = fileName; if (image.isNull()) { mImageReference.loaded = false; return false; } const QSize tileSize = this->tileSize(); const int margin = this->margin(); const int spacing = this->tileSpacing(); Q_ASSERT(tileSize.width() > 0 && tileSize.height() > 0); const int stopWidth = image.width() - tileSize.width(); const int stopHeight = image.height() - tileSize.height(); int tileNum = 0; for (int y = margin; y <= stopHeight; y += tileSize.height() + spacing) { for (int x = margin; x <= stopWidth; x += tileSize.width() + spacing) { const QImage tileImage = image.copy(x, y, tileSize.width(), tileSize.height()); QPixmap tilePixmap = QPixmap::fromImage(tileImage); const QColor &transparent = mImageReference.transparentColor; if (transparent.isValid()) { const QImage mask = tileImage.createMaskFromColor(transparent.rgb()); tilePixmap.setMask(QBitmap::fromImage(mask)); } auto it = mTiles.find(tileNum); if (it != mTiles.end()) it.value()->setImage(tilePixmap); else mTiles.insert(tileNum, new Tile(tilePixmap, tileNum, this)); ++tileNum; } } // Blank out any remaining tiles to avoid confusion (todo: could be more clear) for (Tile *tile : mTiles) { if (tile->id() >= tileNum) { QPixmap tilePixmap = QPixmap(tileSize); tilePixmap.fill(); tile->setImage(tilePixmap); } } mNextTileId = std::max(mNextTileId, tileNum); mImageReference.size = image.size(); mColumnCount = columnCountForWidth(mImageReference.size.width()); mImageReference.loaded = true; return true; }
/** * Load this tileset from the given tileset \a image. This will replace * existing tile images in this tileset with new ones. If the new image * contains more tiles than exist in the tileset new tiles will be * appended, if there are fewer tiles the excess images will be blanked. * * The tile width and height of this tileset must be higher than 0. * * @param image the image to load the tiles from * @param fileName the file name of the image, which will be remembered * as the image source of this tileset. * @return <code>true</code> if loading was successful, otherwise * returns <code>false</code> */ bool Tileset::loadFromImage(const QImage &image, const QString &fileName) { const QSize tileSize = this->tileSize(); const int margin = this->margin(); const int spacing = this->tileSpacing(); Q_ASSERT(tileSize.width() > 0 && tileSize.height() > 0); if (image.isNull()) return false; const int stopWidth = image.width() - tileSize.width(); const int stopHeight = image.height() - tileSize.height(); int oldTilesetSize = tileCount(); int tileNum = 0; for (int y = margin; y <= stopHeight; y += tileSize.height() + spacing) { for (int x = margin; x <= stopWidth; x += tileSize.width() + spacing) { const QImage tileImage = image.copy(x, y, tileSize.width(), tileSize.height()); QPixmap tilePixmap = QPixmap::fromImage(tileImage); if (mTransparentColor.isValid()) { const QImage mask = tileImage.createMaskFromColor(mTransparentColor.rgb()); tilePixmap.setMask(QBitmap::fromImage(mask)); } if (tileNum < oldTilesetSize) { mTiles.at(tileNum)->setImage(tilePixmap); } else { mTiles.append(new Tile(tilePixmap, tileNum, this)); } ++tileNum; } } // Blank out any remaining tiles to avoid confusion while (tileNum < oldTilesetSize) { QPixmap tilePixmap = QPixmap(tileSize); tilePixmap.fill(); mTiles.at(tileNum)->setImage(tilePixmap); ++tileNum; } mImageWidth = image.width(); mImageHeight = image.height(); mColumnCount = columnCountForWidth(mImageWidth); mImageSource = fileName; return true; }
void QWpsDrawer::transparent_bitmap_part(const void *src, int src_x, int src_y, int stride, int x, int y, int width, int height) { QImage img; img.load((char*)src); DEBUGF2("transparent_bitmap_part(const void *src=%s, int src_x=%d, int src_y=%d,int stride=%d, int x=%d, int y=%d, int width=%d, int height=%d",(char*)src,src_x, src_y,stride, x, y, width, height); QPainter p(pix); QPoint target(x,y); QRectF source(src_x, src_y, width, height); QImage pink = img.createMaskFromColor(qRgb(255,0,255),Qt::MaskOutColor); img.setAlphaChannel(pink); p.drawImage(target, img, source); }
bool Tileset::loadFromImage(const QString &fileName) { Q_ASSERT(mTileWidth > 0 && mTileHeight > 0); const QImage image(fileName); if (image.isNull()) return false; mTileSetHeight = image.height(); mTileSetWidth = image.width(); const int stopWidth = image.width() - mTileWidth; const int stopHeight = image.height() - mTileHeight; int oldTilesetSize = mTiles.size(); int tileNum = 0; for (int y = mMargin; y <= stopHeight; y += mTileHeight + mTileSpacing) { for (int x = mMargin; x <= stopWidth; x += mTileWidth + mTileSpacing) { const QImage tileImage = image.copy(x, y, mTileWidth, mTileHeight); QPixmap tilePixmap = QPixmap::fromImage(tileImage); if (mTransparentColor.isValid()) { const QImage mask = tileImage.createMaskFromColor(mTransparentColor.rgb()); tilePixmap.setMask(QBitmap::fromImage(mask)); } if (tileNum < oldTilesetSize) { mTiles.at(tileNum)->setImage(tilePixmap); } else { mTiles.append(new Tile(tilePixmap, tileNum, this)); } ++tileNum; } } // Blank out any remaining tiles to avoid confusion while (tileNum < oldTilesetSize) { QPixmap tilePixmap = QPixmap(mTileWidth, mTileHeight); tilePixmap.fill(); mTiles.at(tileNum)->setImage(tilePixmap); ++tileNum; } mColumnCount = (image.width() - mMargin * 2 + mTileSpacing) / (mTileWidth + mTileSpacing); mImageSource = fileName; return true; }
QImage *createTransparentImage(QString filename, QRgb trColor) { QImage * img = new QImage(filename); QImage * transparentImage = new QImage(*img); img->convertToFormat(QImage::Format_ARGB32); QImage mask = img->createMaskFromColor(trColor); transparentImage->fill(QColor::fromRgba(qRgba(0,0,0,0))); QPainter p(transparentImage); p.setRenderHint(QPainter::Antialiasing, true); p.setClipRegion(QRegion(QBitmap::fromImage(mask))); p.drawImage(QPoint(0,0),*img); delete img; return transparentImage; }
bool ImageLayer::loadFromImage(const QImage &image, const QString &fileName) { mImageSource = fileName; if (image.isNull()) { mImage = QPixmap(); return false; } mImage = QPixmap::fromImage(image); if (mTransparentColor.isValid()) { const QImage mask = image.createMaskFromColor(mTransparentColor.rgb()); mImage.setMask(QBitmap::fromImage(mask)); } return true; }
bool ImageLayer::loadFromImage(const QImage &image, const QUrl &source) { mImageSource = source; if (image.isNull()) { mImage = QPixmap(); return false; } // todo: allow caching of this QPixmap in the ImageCache mImage = QPixmap::fromImage(image); if (mTransparentColor.isValid()) { const QImage mask = image.createMaskFromColor(mTransparentColor.rgb()); mImage.setMask(QBitmap::fromImage(mask)); } return true; }
void ItemImages::load(const QImage &srcImage, QTextStream &srcDesc){ for(ItemImage& i:images)i.loaded=false; QImage mask=srcImage.createMaskFromColor(srcImage.pixel(0,0)); QPixmap pixmap=QPixmap::fromImage(srcImage); pixmap.setMask(QBitmap::fromImage(mask)); while(!srcDesc.atEnd()){ QString in; QStringList inl; in=srcDesc.readLine(); inl=in.split('.'); int currentSpec=inl[0].toInt(); int x,y,w,h,dx,dy; x=inl[1].toInt(); y=inl[2].toInt(); w=inl[3].toInt(); h=inl[4].toInt(); if(inl.size()>5){ dx=inl[5].toInt(); dy=inl[6].toInt(); }else{ dx=w/2; dy=h/2; } images[currentSpec].dx=dx; images[currentSpec].dy=dy; images[currentSpec].large=pixmap.copy(x,y,w,h); images[currentSpec].small=pixmap.copy( x+(w-smallImageSize)/2, y+(h-smallImageSize)/2, smallImageSize, smallImageSize ); images[currentSpec].loaded=true; } }