/** * Translates a raw image resource into a graphics surface. The caller is then responsible * for managing and destroying the surface when done with it * * @imgData Raw image resource * @size Size of the resource */ GfxSurface surfaceFromRes(const byte *imgData) { Rect r(0, 0, READ_LE_UINT16(imgData), READ_LE_UINT16(imgData + 2)); GfxSurface s; s.create(r.width(), r.height()); s._centroid.x = READ_LE_UINT16(imgData + 4); s._centroid.y = READ_LE_UINT16(imgData + 6); s._transColor = *(imgData + 8); bool rleEncoded = (imgData[9] & 2) != 0; const byte *srcP = imgData + 10; Graphics::Surface destSurface = s.lockSurface(); byte *destP = (byte *)destSurface.getBasePtr(0, 0); if (!rleEncoded) { Common::copy(srcP, srcP + (r.width() * r.height()), destP); } else { Common::set_to(destP, destP + (r.width() * r.height()), s._transColor); for (int yp = 0; yp < r.height(); ++yp) { int width = r.width(); destP = (byte *)destSurface.getBasePtr(0, yp); while (width > 0) { uint8 controlVal = *srcP++; if ((controlVal & 0x80) == 0) { // Copy specified number of bytes Common::copy(srcP, srcP + controlVal, destP); width -= controlVal; srcP += controlVal; destP += controlVal; } else if ((controlVal & 0x40) == 0) { // Skip a specified number of output pixels destP += controlVal & 0x3f; width -= controlVal & 0x3f; } else { // Copy a specified pixel a given number of times controlVal &= 0x3f; int pixel = *srcP++; Common::set_to(destP, destP + controlVal, pixel); destP += controlVal; width -= controlVal; } } assert(width == 0); } } s.unlockSurface(); return s; }
/** * Scales a passed surface, creating a new surface with the result * @param srcImage Source image to scale * @param NewWidth New width for scaled image * @param NewHeight New height for scaled image * @remarks Caller is responsible for freeing the returned surface */ static GfxSurface ResizeSurface(GfxSurface &src, int xSize, int ySize, int transIndex) { GfxSurface s; s.create(xSize, ySize); Graphics::Surface srcImage = src.lockSurface(); Graphics::Surface destImage = s.lockSurface(); int *horizUsage = scaleLine(xSize, srcImage.w); int *vertUsage = scaleLine(ySize, srcImage.h); // Loop to create scaled version for (int yp = 0; yp < ySize; ++yp) { byte *destP = (byte *)destImage.getBasePtr(0, yp); if (vertUsage[yp] == -1) { Common::set_to(destP, destP + xSize, transIndex); } else { const byte *srcP = (const byte *)srcImage.getBasePtr(0, vertUsage[yp]); for (int xp = 0; xp < xSize; ++xp) { if (horizUsage[xp] != -1) { const byte *tempSrcP = srcP + horizUsage[xp]; *destP++ = *tempSrcP++; } else { // Pixel overrun at the end of the line *destP++ = transIndex; } } } } // Unlock surfaces src.unlockSurface(); s.unlockSurface(); // Delete arrays and return surface delete[] horizUsage; delete[] vertUsage; return s; }
/** * Copys an area from one GfxSurface to another */ void GfxSurface::copyFrom(GfxSurface &src, Rect srcBounds, Rect destBounds, Region *priorityRegion) { GfxSurface srcImage; if (srcBounds.isEmpty()) return; if (srcBounds == src.getBounds()) srcImage = src; else { // Set the source image to be the subset specified by the source bounds Graphics::Surface srcSurface = src.lockSurface(); srcImage.create(srcBounds.width(), srcBounds.height()); Graphics::Surface destSurface = srcImage.lockSurface(); const byte *srcP = (const byte *)srcSurface.getBasePtr(srcBounds.left, srcBounds.top); byte *destP = (byte *)destSurface.pixels; for (int yp = srcBounds.top; yp < srcBounds.bottom; ++yp, srcP += srcSurface.pitch, destP += destSurface.pitch) { Common::copy(srcP, srcP + srcBounds.width(), destP); } srcImage.unlockSurface(); src.unlockSurface(); } if ((destBounds.width() != srcBounds.width()) || (destBounds.height() != srcBounds.height())) srcImage = ResizeSurface(srcImage, destBounds.width(), destBounds.height(), src._transColor); Graphics::Surface srcSurface = srcImage.lockSurface(); Graphics::Surface destSurface = lockSurface(); // Adjust bounds to ensure destination will be on-screen int srcX = 0, srcY = 0; if (destBounds.left < 0) { srcX = -destBounds.left; destBounds.left = 0; } if (destBounds.top < 0) { srcY = -destBounds.top; destBounds.top = 0; } if (destBounds.right > destSurface.w) destBounds.right = destSurface.w; if (destBounds.bottom > destSurface.h) destBounds.bottom = destSurface.h; if (destBounds.isValidRect()) { const byte *pSrc = (const byte *)srcSurface.getBasePtr(srcX, srcY); byte *pDest = (byte *)destSurface.getBasePtr(destBounds.left, destBounds.top); for (int y = 0; y < destBounds.height(); ++y, pSrc += srcSurface.pitch, pDest += destSurface.pitch) { if (!priorityRegion && (src._transColor == -1)) Common::copy(pSrc, pSrc + destBounds.width(), pDest); else { const byte *tempSrc = pSrc; byte *tempDest = pDest; int xp = destBounds.left; while (tempSrc < (pSrc + destBounds.width())) { if (!priorityRegion || !priorityRegion->contains(Common::Point( xp + _globals->_sceneManager._scene->_sceneBounds.left, destBounds.top + y + _globals->_sceneManager._scene->_sceneBounds.top))) { if (*tempSrc != src._transColor) *tempDest = *tempSrc; } ++tempSrc; ++tempDest; ++xp; } } } } unlockSurface(); srcImage.unlockSurface(); }