diff options
Diffstat (limited to 'graphics')
32 files changed, 5298 insertions, 505 deletions
diff --git a/graphics/VectorRenderer.cpp b/graphics/VectorRenderer.cpp new file mode 100644 index 0000000000..e67f81a363 --- /dev/null +++ b/graphics/VectorRenderer.cpp @@ -0,0 +1,161 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#include "common/util.h" +#include "common/system.h" +#include "common/events.h" + +#include "graphics/surface.h" +#include "graphics/colormasks.h" + +#include "gui/ThemeEngine.h" +#include "graphics/VectorRenderer.h" + +#define VECTOR_RENDERER_FAST_TRIANGLES + +namespace Graphics { + +#ifndef DISABLE_FANCY_THEMES +const VectorRenderer::ConvolutionDataSet VectorRenderer::_convolutionData[VectorRenderer::kConvolutionMAX] = { + { {{1, 1, 1}, {1, 8, 1}, {1, 1, 1}}, 16, 0 }, // soft blur matrix + { {{2, 2, 2}, {2, 2, 2}, {2, 2, 2}}, 18, 0 }, // hard blur matrix + { {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}}, 16, 0 }, // gaussian blur matrix + { {{2, 0, 0}, {0, -1, 0}, {0, 0, -1}}, 1, 127}, // emboss matrix + { {{-1, -1, -1}, {-1, 9, -1}, {-1, -1, -1}}, 1, 0}, // sharpen matrix + { {{1, 1, 1}, {1, -7, 1}, {1, 1, 1}}, 1, 0} // edge find matrix +}; +#endif + +/******************************************************************** + * DRAWSTEP handling functions + ********************************************************************/ +void VectorRenderer::drawStep(const Common::Rect &area, const DrawStep &step, uint32 extra) { + + if (step.bgColor.set) + setBgColor(step.bgColor.r, step.bgColor.g, step.bgColor.b); + + if (step.fgColor.set) + setFgColor(step.fgColor.r, step.fgColor.g, step.fgColor.b); + + if (step.bevelColor.set) + setBevelColor(step.bevelColor.r, step.bevelColor.g, step.bevelColor.b); + + if (step.gradColor1.set && step.gradColor2.set) + setGradientColors(step.gradColor1.r, step.gradColor1.g, step.gradColor1.b, + step.gradColor2.r, step.gradColor2.g, step.gradColor2.b); + + setShadowOffset(_disableShadows ? 0 : step.shadow); + setBevel(step.bevel); + setGradientFactor(step.factor); + setStrokeWidth(step.stroke); + setFillMode((FillMode)step.fillMode); + + _dynamicData = extra; + + (this->*(step.drawingCall))(area, step); +} + +int VectorRenderer::stepGetRadius(const DrawStep &step, const Common::Rect &area) { + int radius = 0; + + if (step.radius == 0xFF) + radius = MIN(area.width(), area.height()) / 2; + else + radius = step.radius; + + if (step.scale != (1 << 16) && step.scale != 0) + radius = (radius * step.scale) >> 16; + + return radius; +} + +void VectorRenderer::stepGetPositions(const DrawStep &step, const Common::Rect &area, uint16 &in_x, uint16 &in_y, uint16 &in_w, uint16 &in_h) { + if (!step.autoWidth) { + in_w = step.w == -1 ? area.height() : step.w; + + switch(step.xAlign) { + case Graphics::DrawStep::kVectorAlignManual: + if (step.x >= 0) in_x = area.left + step.x; + else in_x = area.left + area.width() + step.x; // value relative to the opposite corner. + break; + + case Graphics::DrawStep::kVectorAlignCenter: + in_x = area.left + (area.width() / 2) - (in_w / 2); + break; + + case Graphics::DrawStep::kVectorAlignLeft: + in_x = area.left; + break; + + case Graphics::DrawStep::kVectorAlignRight: + in_x = area.left + area.width() - in_w; + break; + + default: + error("Vertical alignment in horizontal data."); + } + } else { + in_x = area.left; + in_w = area.width(); + } + + if (!step.autoHeight) { + in_h = step.h == -1 ? area.width() : step.h; + + switch(step.yAlign) { + case Graphics::DrawStep::kVectorAlignManual: + if (step.y >= 0) in_y = area.top + step.y; + else in_y = area.top + area.height() + step.y; // relative + break; + + case Graphics::DrawStep::kVectorAlignCenter: + in_y = area.top + (area.height() / 2) - (in_h / 2); + break; + + case Graphics::DrawStep::kVectorAlignTop: + in_y = area.top; + break; + + case Graphics::DrawStep::kVectorAlignBottom: + in_y = area.top + area.height() - in_h; + break; + + default: + error("Horizontal alignment in vertical data."); + } + } else { + in_y = area.top; + in_h = area.height(); + } + + if (step.scale != (1 << 16) && step.scale != 0) { + in_x = (in_x * step.scale) >> 16; + in_y = (in_y * step.scale) >> 16; + in_w = (in_w * step.scale) >> 16; + in_h = (in_h * step.scale) >> 16; + } +} + +} // end of namespace Graphics diff --git a/graphics/VectorRenderer.h b/graphics/VectorRenderer.h new file mode 100644 index 0000000000..ca61720d06 --- /dev/null +++ b/graphics/VectorRenderer.h @@ -0,0 +1,575 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef VECTOR_RENDERER_H +#define VECTOR_RENDERER_H + +#include "common/scummsys.h" +#include "common/system.h" + +#include "graphics/surface.h" +#include "graphics/colormasks.h" + +#include "gui/ThemeEngine.h" + +// To assure we have VECTOR_RENDERER_FORMAT specified when +// DISABLE_FANCY_THEMES is defined, we error out elsewise +#if defined(DISABLE_FANCY_THEMES) && !defined(VECTOR_RENDERER_FORMAT) +#error "You need to specify a fixed overlay format via VECTOR_RENDERER_FORMAT" +#endif + +namespace Graphics { +class VectorRenderer; + +struct DrawStep { + struct Color { + uint8 r, g, b; + bool set; + }; + Color fgColor; /**< Foreground color */ + Color bgColor; /**< background color */ + Color gradColor1; /**< gradient start*/ + Color gradColor2; /**< gradient end */ + Color bevelColor; + + bool autoWidth, autoHeight; + int16 x, y, w, h; /**< width, height and position, if not measured automatically. + negative values mean counting from the opposite direction */ + + enum VectorAlignment { + kVectorAlignManual, + kVectorAlignLeft, + kVectorAlignRight, + kVectorAlignBottom, + kVectorAlignTop, + kVectorAlignCenter + } xAlign, yAlign; + + uint8 shadow, stroke, factor, radius, bevel; /**< Misc options... */ + + uint8 fillMode; /**< active fill mode */ + uint32 extraData; /**< Generic parameter for extra options (orientation/bevel) */ + + uint32 scale; /**< scale of all the coordinates in FIXED POINT with 16 bits mantissa */ + + void (VectorRenderer::*drawingCall)(const Common::Rect &, const DrawStep &); /** Pointer to drawing function */ + Graphics::Surface *blitSrc; +}; + +VectorRenderer *createRenderer(int mode); + +/** + * VectorRenderer: The core Vector Renderer Class + * + * This virtual class exposes the API with all the vectorial + * rendering functions that may be used to draw on a given Surface. + * + * This class must be instantiated as one of its children, which implement + * the actual rendering functionality for each Byte Depth / Byte Format + * combination, and may also contain platform specific code. + * + * When specifying define DISABLE_FANCY_THEMES eye candy related code + * gets stripped off. This is especially useful for small devices like NDS. + * Also note that if you specify DISABLE_FANCY_THEMES, you'll need to + * specify a forced overlay bit format via VECTOR_RENDERER_FORMAT define. + * The value looks like 'XYZ' for RXGYBZ mode, so R5G5B5 would be specified + * via: + * #define VECTOR_RENDERER_FORMAT 565 + * + * TODO: Expand documentation. + * + * @see VectorRendererSpec + * @see VectorRendererAA + */ +class VectorRenderer { +public: + VectorRenderer() : _activeSurface(NULL), _fillMode(kFillDisabled), _shadowOffset(0), + _disableShadows(false), _strokeWidth(1), _gradientFactor(1) { + + } + + virtual ~VectorRenderer() {} + + /** Specifies the way in which a shape is filled */ + enum FillMode { + kFillDisabled = 0, + kFillForeground = 1, + kFillBackground = 2, + kFillGradient = 3 + }; + + enum TriangleOrientation { + kTriangleAuto = 0, + kTriangleUp, + kTriangleDown, + kTriangleLeft, + kTriangleRight + }; + +#ifndef DISABLE_FANCY_THEMES + enum ConvolutionData { + kConvolutionSoftBlur, + kConvolutionHardBlur, + kConvolutionGaussianBlur, + kConvolutionEmboss, + kConvolutionSharpen, + kConvolutionEdgeDetect, + kConvolutionMAX + }; + + struct ConvolutionDataSet { + int matrix[3][3]; + int divisor; + int offset; + }; +#endif + + /** + * Draws a line by considering the special cases for optimization. + * + * @param x1 Horizontal (X) coordinate for the line start + * @param x2 Horizontal (X) coordinate for the line end + * @param y1 Vertical (Y) coordinate for the line start + * @param y2 Vertical (Y) coordinate for the line end + */ + virtual void drawLine(int x1, int y1, int x2, int y2) = 0; + + /** + * Draws a circle centered at (x,y) with radius r. + * + * @param x Horizontal (X) coordinate for the center of the circle + * @param y Vertical (Y) coordinate for the center of the circle + * @param r Radius of the circle. + */ + virtual void drawCircle(int x, int y, int r) = 0; + + /** + * Draws a square starting at (x,y) with the given width and height. + * + * @param x Horizontal (X) coordinate for the center of the square + * @param y Vertical (Y) coordinate for the center of the square + * @param w Width of the square. + * @param h Height of the square + */ + virtual void drawSquare(int x, int y, int w, int h) = 0; + + /** + * Draws a rounded square starting at (x,y) with the given width and height. + * The corners of the square are rounded with the given radius. + * + * @param x Horizontal (X) coordinate for the center of the square + * @param y Vertical (Y) coordinate for the center of the square + * @param w Width of the square. + * @param h Height of the square + * @param r Radius of the corners. + */ + virtual void drawRoundedSquare(int x, int y, int r, int w, int h) = 0; + + /** + * Draws a triangle starting at (x,y) with the given base and height. + * The triangle will always be isosceles, with the given base and height. + * The orientation parameter controls the position of the base of the triangle. + * + * @param x Horizontal (X) coordinate for the top left corner of the triangle + * @param y Vertical (Y) coordinate for the top left corner of the triangle + * @param base Width of the base of the triangle + * @param h Height of the triangle + * @param orient Orientation of the triangle. + */ + virtual void drawTriangle(int x, int y, int base, int height, TriangleOrientation orient) = 0; + + /** + * Draws a beveled square like the ones in the Classic GUI themes. + * Beveled squares are always drawn with a transparent background. Draw them on top + * of a standard square to fill it. + * + * @param x Horizontal (X) coordinate for the center of the square + * @param y Vertical (Y) coordinate for the center of the square + * @param w Width of the square. + * @param h Height of the square + * @param bevel Amount of bevel. Must be positive. + */ + virtual void drawBeveledSquare(int x, int y, int w, int h, int bevel) = 0; + + /** + * Draws a tab-like shape, specially thought for the Tab widget. + * If a radius is given, the tab will have rounded corners. Otherwise, + * the tab will be squared. + * + * @param x Horizontal (X) coordinate for the tab + * @param y Vertical (Y) coordinate for the tab + * @param w Width of the tab + * @param h Height of the tab + * @param r Radius of the corners of the tab (0 for squared tabs). + */ + virtual void drawTab(int x, int y, int r, int w, int h) = 0; + + + /** + * Simple helper function to draw a cross. + */ + virtual void drawCross(int x, int y, int w, int h) { + drawLine(x, y, x + w, y + w); + drawLine(x + w, y, x, y + h); + } + + /** + * Gets the pixel pitch for the current drawing surface. + * Note: This is a real pixel-pitch, not a byte-pitch. + * That means it can be safely used in pointer arithmetics and + * in pixel manipulation. + * + * @return integer with the active bytes per pixel + */ + virtual uint16 surfacePitch() { + return _activeSurface->pitch / _activeSurface->bytesPerPixel; + } + + /** + * Gets the BYTES (not bits) per Pixel we are working on, + * based on the active drawing surface. + * + * @return integer byte with the active bytes per pixel value + */ + virtual uint8 bytesPerPixel() { + return _activeSurface->bytesPerPixel; + } + + /** + * Set the active foreground painting color for the renderer. + * All the foreground drawing from then on will be done with that color, unless + * specified otherwise. + * + * Foreground drawing means all outlines and basic shapes. + * + * @param r value of the red color byte + * @param g value of the green color byte + * @param b value of the blue color byte + */ + virtual void setFgColor(uint8 r, uint8 g, uint8 b) = 0; + + /** + * Set the active background painting color for the renderer. + * All the background drawing from then on will be done with that color, unless + * specified otherwise. + * + * Background drawing means all the shape filling. + * + * @param r value of the red color byte + * @param g value of the green color byte + * @param b value of the blue color byte + */ + virtual void setBgColor(uint8 r, uint8 g, uint8 b) = 0; + + virtual void setBevelColor(uint8 r, uint8 g, uint8 b) = 0; + + /** + * Set the active gradient color. All shapes drawn using kFillGradient + * as their fill mode will use this VERTICAL gradient as their fill color. + * + * @param r1 value of the red color byte for the start color + * @param g1 value of the green color byte for the start color + * @param b1 value of the blue color byte for the start color + * @param r2 value of the red color byte for the end color + * @param g2 value of the green color byte for the end color + * @param b2 value of the blue color byte for the end color + */ + virtual void setGradientColors(uint8 r1, uint8 g1, uint8 b1, uint8 r2, uint8 g2, uint8 b2) = 0; + + /** + * Sets the active drawing surface. All drawing from this + * point on will be done on that surface. + * + * @param surface Pointer to a Surface object. + */ + virtual void setSurface(Surface *surface) { + _activeSurface = surface; + } + + /** + * Fills the active surface with the specified fg/bg color or the active gradient. + * Defaults to using the active Foreground color for filling. + * + * @param mode Fill mode (bg, fg or gradient) used to fill the surface + */ + virtual void fillSurface() = 0; + + /** + * Clears the active surface. + */ + virtual void clearSurface() { + byte *src = (byte *)_activeSurface->pixels; + memset(src, 0, _activeSurface->w * _activeSurface->h * _activeSurface->bytesPerPixel); + } + + /** + * Sets the active fill mode for all shapes. + * + * @see VectorRenderer::FillMode + * @param mode Specified fill mode. + */ + virtual void setFillMode(FillMode mode) { + _fillMode = mode; + } + + /** + * Sets the stroke width. All shapes drawn with a stroke will + * have that width. Pass 0 to disable shape stroking. + * + * @param width Width of the stroke in pixels. + */ + virtual void setStrokeWidth(int width) { + _strokeWidth = width; + } + + /** + * Enables adding shadows to all drawn primitives. + * Shadows are drawn automatically under the shapes. The given offset + * controls their intensity and size (the higher the offset, the + * bigger the shadows). If the offset is 0, no shadows are drawn. + * + * @param offset Shadow offset. + */ + virtual void setShadowOffset(int offset) { + if (offset >= 0) + _shadowOffset = offset; + } + + virtual void setBevel(int amount) { + if (amount >= 0) + _bevel = amount; + } + + /** + * Sets the multiplication factor of the active gradient. + * + * @see _gradientFactor + * @param factor Multiplication factor. + */ + virtual void setGradientFactor(int factor) { + if (factor > 0) + _gradientFactor = factor; + } + + /** + * Translates the position data inside a DrawStep into actual + * screen drawing positions. + */ + void stepGetPositions(const DrawStep &step, const Common::Rect &area, uint16 &in_x, uint16 &in_y, uint16 &in_w, uint16 &in_h); + + /** + * Translates the radius data inside a drawstep into the real radius + * for the shape. Used for automatic radius calculations. + */ + int stepGetRadius(const DrawStep &step, const Common::Rect &area); + + /** + * DrawStep callback functions for each drawing feature + */ + void drawCallback_CIRCLE(const Common::Rect &area, const DrawStep &step) { + uint16 x, y, w, h, radius; + + radius = stepGetRadius(step, area); + stepGetPositions(step, area, x, y, w, h); + + drawCircle(x + radius, y + radius, radius); + } + + void drawCallback_SQUARE(const Common::Rect &area, const DrawStep &step) { + uint16 x, y, w, h; + stepGetPositions(step, area, x, y, w, h); + drawSquare(x, y, w, h); + } + + void drawCallback_LINE(const Common::Rect &area, const DrawStep &step) { + uint16 x, y, w, h; + stepGetPositions(step, area, x, y, w, h); + drawLine(x, y, x + w, y + w); + } + + void drawCallback_ROUNDSQ(const Common::Rect &area, const DrawStep &step) { + uint16 x, y, w, h; + stepGetPositions(step, area, x, y, w, h); + drawRoundedSquare(x, y, stepGetRadius(step, area), w, h); + } + + void drawCallback_FILLSURFACE(const Common::Rect &area, const DrawStep &step) { + fillSurface(); + } + + void drawCallback_TRIANGLE(const Common::Rect &area, const DrawStep &step) { + uint16 x, y, w, h; + stepGetPositions(step, area, x, y, w, h); + drawTriangle(x, y, w, h, (TriangleOrientation)step.extraData); + } + + void drawCallback_BEVELSQ(const Common::Rect &area, const DrawStep &step) { + uint16 x, y, w, h; + stepGetPositions(step, area, x, y, w, h); + drawBeveledSquare(x, y, w, h, _bevel); + } + + void drawCallback_TAB(const Common::Rect &area, const DrawStep &step) { + uint16 x, y, w, h; + stepGetPositions(step, area, x, y, w, h); + drawTab(x, y, stepGetRadius(step, area), w, h); + } + + void drawCallback_BITMAP(const Common::Rect &area, const DrawStep &step) { + uint16 x, y, w, h; + stepGetPositions(step, area, x, y, w, h); + blitAlphaBitmap(step.blitSrc, Common::Rect(x, y, x + w, y + h)); + } + + void drawCallback_CROSS(const Common::Rect &area, const DrawStep &step) { + uint16 x, y, w, h; + stepGetPositions(step, area, x, y, w, h); + drawCross(x, y, w, h); + } + + void drawCallback_VOID(const Common::Rect &area, const DrawStep &step) {} + + /** + * Draws the specified draw step on the screen. + * + * @see DrawStep + * @param area Zone to paint on + * @param step Pointer to a DrawStep struct. + */ + virtual void drawStep(const Common::Rect &area, const DrawStep &step, uint32 extra = 0); + + /** + * Copies the part of the current frame to the system overlay. + * + * @param sys Pointer to the global System class + * @param r Zone of the surface to copy into the overlay. + */ + virtual void copyFrame(OSystem *sys, const Common::Rect &r) = 0; + + /** + * Copies the current surface to the system overlay + * + * @param sys Pointer to the global System class + */ + virtual void copyWholeFrame(OSystem *sys) = 0; + + /** + * Blits a given graphics surface on top of the current drawing surface. + * + * Note that the source surface and the active + * surface are expected to be of the same size, hence the area delimited + * by "r" in the source surface will be blitted into the area delimited by + * "r" on the current surface. + * + * If you wish to blit a smaller surface into the active drawing area, use + * VectorRenderer::blitSubSurface(). + * + * @param source Surface to blit into the drawing surface. + * @param r Position in the active drawing surface to do the blitting. + */ + virtual void blitSurface(const Graphics::Surface *source, const Common::Rect &r) = 0; + + /** + * Blits a given graphics surface into a small area of the current drawing surface. + * + * Note that the given surface is expected to be smaller than the + * active drawing surface, hence the WHOLE source surface will be + * blitted into the active surface, at the position specified by "r". + */ + virtual void blitSubSurface(const Graphics::Surface *source, const Common::Rect &r) = 0; + + virtual void blitAlphaBitmap(const Graphics::Surface *source, const Common::Rect &r) = 0; + + /** + * Draws a string into the screen. Wrapper for the Graphics::Font string drawing + * method. + */ + virtual void drawString(const Graphics::Font *font, const Common::String &text, + const Common::Rect &area, Graphics::TextAlign alignH, + GUI::ThemeEngine::TextAlignVertical alignV, int deltax, bool useEllipsis) = 0; + + /** + * Allows to temporarily enable/disable all shadows drawing. + * i.e. for performance issues, blitting, etc + */ + virtual void disableShadows() { _disableShadows = true; } + virtual void enableShadows() { _disableShadows = false; } + +#ifndef DISABLE_FANCY_THEMES + /** + * Applies a convolution matrix on the given surface area. + * Call applyConvolutionMatrix() instead if you want to use + * the embedded matrixes (blur/sharpen masks, bevels, etc). + * + * @param area Area in which the convolution matrix will be applied. + * @param filter Convolution matrix (3X3) + * @param filterDiv Divisor for the convolution matrix. + * Make sure this equals the total sum of the elements + * of the matrix or brightness data will be distorted. + * @param offset Offset on the convolution area. + */ + virtual void areaConvolution(const Common::Rect &area, const int filter[3][3], int filterDiv, int offset) = 0; + + /** + * Applies one of the predefined convolution effects on the given area. + * + * WARNING: Because of performance issues, this is currently disabled on all renderers. + * + * @param id Id of the convolution data set (see VectorRenderer::ConvolutionData) + * @param area Area in which the convolution effect will be applied. + */ + virtual void applyConvolutionMatrix(const ConvolutionData id, const Common::Rect &area) { + areaConvolution(area, _convolutionData[id].matrix, _convolutionData[id].divisor, _convolutionData[id].offset); + } +#endif + + /** + * Applies a whole-screen shading effect, used before opening a new dialog. + * Currently supports screen dimmings and luminance (b&w). + */ + virtual void applyScreenShading(GUI::ThemeEngine::ShadingStyle) = 0; + +protected: + Surface *_activeSurface; /**< Pointer to the surface currently being drawn */ + + FillMode _fillMode; /**< Defines in which way (if any) are filled the drawn shapes */ + + int _shadowOffset; /**< offset for drawn shadows */ + int _bevel; /**< amount of fake bevel */ + bool _disableShadows; /**< Disables temporarily shadow drawing for overlayed images. */ + int _strokeWidth; /**< Width of the stroke of all drawn shapes */ + uint32 _dynamicData; /**< Dynamic data from the GUI Theme that modifies the drawing of the current shape */ + + int _gradientFactor; /**< Multiplication factor of the active gradient */ + int _gradientBytes[3]; /**< Color bytes of the active gradient, used to speed up calculation */ + +#ifndef DISABLE_FANCY_THEMES + static const ConvolutionDataSet _convolutionData[kConvolutionMAX]; +#endif +}; + +} // end of namespace Graphics + +#endif diff --git a/graphics/VectorRendererSpec.cpp b/graphics/VectorRendererSpec.cpp new file mode 100644 index 0000000000..46c0ec6a67 --- /dev/null +++ b/graphics/VectorRendererSpec.cpp @@ -0,0 +1,1620 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#include "common/util.h" +#include "common/system.h" +#include "common/events.h" + +#include "graphics/surface.h" +#include "graphics/colormasks.h" + +#include "gui/ThemeEngine.h" +#include "graphics/VectorRenderer.h" +#include "graphics/VectorRendererSpec.h" + +#define VECTOR_RENDERER_FAST_TRIANGLES + +/** Fixed point SQUARE ROOT **/ +inline uint32 fp_sqroot(uint32 x) { + register uint32 root, remHI, remLO, testDIV, count; + + root = 0; + remHI = 0; + remLO = x; + count = 23; + + do { + remHI = (remHI << 2) | (remLO >> 30); + remLO <<= 2; + root <<= 1; + testDIV = (root << 1) + 1; + + if (remHI >= testDIV) { + remHI -= testDIV; + root++; + } + } while (count--); + + return root; +} + +/* + HELPER MACROS for Bresenham's circle drawing algorithm + Note the proper spelling on this header. +*/ +#define __BE_ALGORITHM() { \ + if (f >= 0) { \ + y--; \ + ddF_y += 2; \ + f += ddF_y; \ + py -= pitch; \ + } \ + px += pitch; \ + ddF_x += 2; \ + f += ddF_x + 1; \ +} + +#define __BE_DRAWCIRCLE(ptr1,ptr2,ptr3,ptr4,x,y,px,py) { \ + *(ptr1 + (y) - (px)) = color; \ + *(ptr1 + (x) - (py)) = color; \ + *(ptr2 - (x) - (py)) = color; \ + *(ptr2 - (y) - (px)) = color; \ + *(ptr3 - (y) + (px)) = color; \ + *(ptr3 - (x) + (py)) = color; \ + *(ptr4 + (x) + (py)) = color; \ + *(ptr4 + (y) + (px)) = color; \ +} + +#define __BE_DRAWCIRCLE_XCOLOR(ptr1,ptr2,ptr3,ptr4,x,y,px,py) { \ + *(ptr1 + (y) - (px)) = color1; \ + *(ptr1 + (x) - (py)) = color2; \ + *(ptr2 - (x) - (py)) = color2; \ + *(ptr2 - (y) - (px)) = color1; \ + *(ptr3 - (y) + (px)) = color3; \ + *(ptr3 - (x) + (py)) = color4; \ + *(ptr4 + (x) + (py)) = color4; \ + *(ptr4 + (y) + (px)) = color3; \ +} + +#define __BE_RESET() { \ + f = 1 - r; \ + ddF_x = 0; ddF_y = -2 * r; \ + x = 0; y = r; px = 0; py = pitch * r; \ +} + +#define __TRIANGLE_MAINX() \ + if (error_term >= 0) { \ + ptr_right += pitch; \ + ptr_left += pitch; \ + error_term += dysub; \ + } else { \ + error_term += ddy; \ + } \ + ptr_right++; \ + ptr_left--; + +#define __TRIANGLE_MAINY() \ + if (error_term >= 0) { \ + ptr_right++; \ + ptr_left--; \ + error_term += dxsub; \ + } else { \ + error_term += ddx; \ + } \ + ptr_right += pitch; \ + ptr_left += pitch; + +/** HELPER MACROS for WU's circle drawing algorithm **/ +#define __WU_DRAWCIRCLE(ptr1,ptr2,ptr3,ptr4,x,y,px,py,a) { \ + blendPixelPtr(ptr1 + (y) - (px), color, a); \ + blendPixelPtr(ptr1 + (x) - (py), color, a); \ + blendPixelPtr(ptr2 - (x) - (py), color, a); \ + blendPixelPtr(ptr2 - (y) - (px), color, a); \ + blendPixelPtr(ptr3 - (y) + (px), color, a); \ + blendPixelPtr(ptr3 - (x) + (py), color, a); \ + blendPixelPtr(ptr4 + (x) + (py), color, a); \ + blendPixelPtr(ptr4 + (y) + (px), color, a); \ +} + +// optimized Wu's algorithm +#define __WU_ALGORITHM() { \ + oldT = T; \ + T = fp_sqroot(rsq - ((y * y) << 16)) ^ 0xFFFF; \ + py += p; \ + if (T < oldT) { \ + x--; px -= p; \ + } \ + a2 = (T >> 8); \ + a1 = ~a2; \ +} + + +extern int gBitFormat; + +namespace Graphics { + +VectorRenderer *createRenderer(int mode) { +#ifdef DISABLE_FANCY_THEMES + assert(mode == GUI::ThemeEngine::kGfxStandard16bit); + return new VectorRendererSpec<uint16, ColorMasks<VECTOR_RENDERER_FORMAT> >; +#else +#define CREATE_RENDERER_16(bitFormat) \ + switch (mode) { \ + case GUI::ThemeEngine::kGfxStandard16bit: \ + return new VectorRendererSpec<uint16, ColorMasks<bitFormat> >; \ + \ + case GUI::ThemeEngine::kGfxAntialias16bit: \ + return new VectorRendererAA<uint16, ColorMasks<bitFormat> >; \ + \ + default: \ + return 0; \ + } + + // FIXME/TODO: This looks like a real gross hack. + // It might be fine to assume that '1555' only happens for PSP + // so it could maybe be handled via DISABLE_FANCY_THEMES, + // same goes for 4444, which is only used by DC port. + if (gBitFormat == 1555) { + CREATE_RENDERER_16(1555) + } else if (gBitFormat == 4444) { + CREATE_RENDERER_16(4444) + } else if (gBitFormat == 555) { + CREATE_RENDERER_16(555) + } else if (gBitFormat == 565) { + CREATE_RENDERER_16(565) + } else { + return 0; + } +#undef CREATE_RENDERER_16 +#endif +} + +#ifndef DISABLE_FANCY_THEMES +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +areaConvolution(const Common::Rect &area, const int filter[3][3], int filterDiv, int offset) { + PixelType *ptr = 0; + int newR, newG, newB; + uint8 r, g, b; + int yVal; + + for (int y = area.top; y < area.bottom; ++y) { + for (int x = area.left; x < area.right; ++x) { + newR = newG = newB = 0; + + for (int j = 0; j < 3; ++j) { + yVal = MIN(MAX(y - 1 + j, 0), area.bottom - 1); + + for (int i = 0; i < 3; ++i) { + ptr = (PixelType *)Base::_activeSurface->getBasePtr(MIN(MAX(x - 1 + j, 0), area.right - 1), yVal); + colorToRGB<PixelFormat>((uint32)*ptr, r, g, b); + + newR += r * filter[j][i]; + newG += g * filter[j][i]; + newB += b * filter[j][i]; + } + } + + newR = (newR / filterDiv) + offset; + newG = (newG / filterDiv) + offset; + newB = (newB / filterDiv) + offset; + + ptr = (PixelType *)Base::_activeSurface->getBasePtr(x, y); + *ptr = RGBToColor<PixelFormat>(CLIP(newR, 0, 255), CLIP(newG, 0, 255), CLIP(newB, 0, 255)); + } + } +} +#endif + +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +setGradientColors(uint8 r1, uint8 g1, uint8 b1, uint8 r2, uint8 g2, uint8 b2) { + _gradientEnd = RGBToColor<PixelFormat>(r2, g2, b2); + _gradientStart = RGBToColor<PixelFormat>(r1, g1, b1); + + Base::_gradientBytes[0] = (_gradientEnd & PixelFormat::kRedMask) - (_gradientStart & PixelFormat::kRedMask); + Base::_gradientBytes[1] = (_gradientEnd & PixelFormat::kGreenMask) - (_gradientStart & PixelFormat::kGreenMask); + Base::_gradientBytes[2] = (_gradientEnd & PixelFormat::kBlueMask) - (_gradientStart & PixelFormat::kBlueMask); +} + +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +fillSurface() { + PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(0, 0); + + int w = _activeSurface->w; + int h = _activeSurface->h ; + int pitch = surfacePitch(); + + if (Base::_fillMode == kFillBackground) + colorFill(ptr, ptr + w * h, _bgColor); + else if (Base::_fillMode == kFillForeground) + colorFill(ptr, ptr + w * h, _fgColor); + else if (Base::_fillMode == kFillGradient) { + int i = h; + while (i--) { + colorFill(ptr, ptr + w, calcGradient(h - i, h)); + ptr += pitch; + } + } +} + +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +copyFrame(OSystem *sys, const Common::Rect &r) { + + sys->copyRectToOverlay( + +#ifdef OVERLAY_MULTIPLE_DEPTHS + (const PixelType*) +#else + (const OverlayColor*) +#endif + + _activeSurface->getBasePtr(r.left, r.top), _activeSurface->w, + r.left, r.top, r.width(), r.height() + ); +} + +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +blitSurface(const Graphics::Surface *source, const Common::Rect &r) { + assert(source->w == _activeSurface->w && source->h == _activeSurface->h); + + PixelType *dst_ptr = (PixelType *)_activeSurface->getBasePtr(r.left, r.top); + PixelType *src_ptr = (PixelType *)source->getBasePtr(r.left, r.top); + + int dst_pitch = surfacePitch(); + int src_pitch = source->pitch / source->bytesPerPixel; + + int h = r.height(), w = r.width(); + + while (h--) { + memcpy(dst_ptr, src_ptr, w * sizeof(PixelType)); + dst_ptr += dst_pitch; + src_ptr += src_pitch; + } +} + +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +blitSubSurface(const Graphics::Surface *source, const Common::Rect &r) { + PixelType *dst_ptr = (PixelType *)_activeSurface->getBasePtr(r.left, r.top); + PixelType *src_ptr = (PixelType *)source->getBasePtr(0, 0); + + int dst_pitch = surfacePitch(); + int src_pitch = source->pitch / source->bytesPerPixel; + + int h = r.height(), w = r.width(); + + while (h--) { + memcpy(dst_ptr, src_ptr, w * sizeof(PixelType)); + dst_ptr += dst_pitch; + src_ptr += src_pitch; + } +} + +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +blitAlphaBitmap(const Graphics::Surface *source, const Common::Rect &r) { + int16 x = r.left; + int16 y = r.top; + + if (r.width() > source->w) + x = x + (r.width() >> 1) - (source->w >> 1); + + if (r.height() > source->h) + y = y + (r.height() >> 1) - (source->h >> 1); + + PixelType *dst_ptr = (PixelType *)_activeSurface->getBasePtr(x, y); + PixelType *src_ptr = (PixelType *)source->getBasePtr(0, 0); + + int dst_pitch = surfacePitch(); + int src_pitch = source->pitch / source->bytesPerPixel; + + int w, h = source->h; + + while (h--) { + w = source->w; + + while (w--) { + if (*src_ptr != _bitmapAlphaColor) + *dst_ptr = *src_ptr; + + dst_ptr++; + src_ptr++; + } + + dst_ptr = dst_ptr - source->w + dst_pitch; + src_ptr = src_ptr - source->w + src_pitch; + } +} + +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +applyScreenShading(GUI::ThemeEngine::ShadingStyle shadingStyle) { + int pixels = _activeSurface->w * _activeSurface->h; + PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(0, 0); + uint8 r, g, b; + uint lum; + + const uint32 shiftMask = (uint32)~( + (1 << PixelFormat::kGreenShift) | + (1 << PixelFormat::kRedShift) | + (1 << PixelFormat::kBlueShift)) >> 1; + + if (shadingStyle == GUI::ThemeEngine::kShadingDim) { + + int n = (pixels + 7) >> 3; + switch (pixels % 8) { + case 0: do { + *ptr = (*ptr >> 1) & shiftMask; ++ptr; + case 7: *ptr = (*ptr >> 1) & shiftMask; ++ptr; + case 6: *ptr = (*ptr >> 1) & shiftMask; ++ptr; + case 5: *ptr = (*ptr >> 1) & shiftMask; ++ptr; + case 4: *ptr = (*ptr >> 1) & shiftMask; ++ptr; + case 3: *ptr = (*ptr >> 1) & shiftMask; ++ptr; + case 2: *ptr = (*ptr >> 1) & shiftMask; ++ptr; + case 1: *ptr = (*ptr >> 1) & shiftMask; ++ptr; + } while (--n > 0); + } + + } else if (shadingStyle == GUI::ThemeEngine::kShadingLuminance) { + while (pixels--) { + colorToRGB<PixelFormat>(*ptr, r, g, b); + lum = (r >> 2) + (g >> 1) + (b >> 3); + *ptr++ = RGBToColor<PixelFormat>(lum, lum, lum); + } + } +} + +template <typename PixelType, typename PixelFormat> +inline void VectorRendererSpec<PixelType, PixelFormat>:: +blendPixelPtr(PixelType *ptr, PixelType color, uint8 alpha) { + register int idst = *ptr; + register int isrc = color; + + *ptr = (PixelType)( + (PixelFormat::kRedMask & ((idst & PixelFormat::kRedMask) + + ((int)(((int)(isrc & PixelFormat::kRedMask) - + (int)(idst & PixelFormat::kRedMask)) * alpha) >> 8))) | + (PixelFormat::kGreenMask & ((idst & PixelFormat::kGreenMask) + + ((int)(((int)(isrc & PixelFormat::kGreenMask) - + (int)(idst & PixelFormat::kGreenMask)) * alpha) >> 8))) | + (PixelFormat::kBlueMask & ((idst & PixelFormat::kBlueMask) + + ((int)(((int)(isrc & PixelFormat::kBlueMask) - + (int)(idst & PixelFormat::kBlueMask)) * alpha) >> 8))) ); +} + +template <typename PixelType, typename PixelFormat> +inline PixelType VectorRendererSpec<PixelType, PixelFormat>:: +calcGradient(uint32 pos, uint32 max) { + PixelType output = 0; + pos = (MIN(pos * Base::_gradientFactor, max) << 12) / max; + + output |= (_gradientStart + ((Base::_gradientBytes[0] * pos) >> 12)) & PixelFormat::kRedMask; + output |= (_gradientStart + ((Base::_gradientBytes[1] * pos) >> 12)) & PixelFormat::kGreenMask; + output |= (_gradientStart + ((Base::_gradientBytes[2] * pos) >> 12)) & PixelFormat::kBlueMask; + output |= ~(PixelFormat::kRedMask | PixelFormat::kGreenMask | PixelFormat::kBlueMask); + + return output; +} + +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +colorFill(PixelType *first, PixelType *last, PixelType color) { + register int count = (last - first); + if (!count) + return; + register int n = (count + 7) >> 3; + switch (count % 8) { + case 0: do { + *first++ = color; + case 7: *first++ = color; + case 6: *first++ = color; + case 5: *first++ = color; + case 4: *first++ = color; + case 3: *first++ = color; + case 2: *first++ = color; + case 1: *first++ = color; + } while (--n > 0); + } +} + +/******************************************************************** + ******************************************************************** + * Primitive shapes drawing - Public API calls - VectorRendererSpec * + ******************************************************************** + ********************************************************************/ +template <typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawString(const Graphics::Font *font, const Common::String &text, const Common::Rect &area, + Graphics::TextAlign alignH, GUI::ThemeEngine::TextAlignVertical alignV, int deltax, bool ellipsis) { + + int offset = area.top; + + if (font->getFontHeight() < area.height()) { + switch (alignV) { + case GUI::ThemeEngine::kTextAlignVCenter: + offset = area.top + ((area.height() - font->getFontHeight()) >> 1); + break; + case GUI::ThemeEngine::kTextAlignVBottom: + offset = area.bottom - font->getFontHeight(); + break; + default: + break; + } + } + + font->drawString(_activeSurface, text, area.left, offset, area.width(), _fgColor, alignH, deltax, ellipsis); +} + +/** LINES **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawLine(int x1, int y1, int x2, int y2) { + x1 = CLIP(x1, 0, (int)Base::_activeSurface->w); + x2 = CLIP(x2, 0, (int)Base::_activeSurface->w); + y1 = CLIP(y1, 0, (int)Base::_activeSurface->h); + y2 = CLIP(y2, 0, (int)Base::_activeSurface->h); + + // we draw from top to bottom + if (y2 < y1) { + SWAP(x1, x2); + SWAP(y1, y2); + } + + int dx = ABS(x2 - x1); + int dy = ABS(y2 - y1); + + // this is a point, not a line. stoopid. + if (dy == 0 && dx == 0) + return; + + if (Base::_strokeWidth == 0) + return; + + PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(x1, y1); + int pitch = Base::surfacePitch(); + int st = Base::_strokeWidth >> 1; + + if (dy == 0) { // horizontal lines + // these can be filled really fast with a single memset. + colorFill(ptr, ptr + dx + 1, (PixelType)_fgColor); + + for (int i = 0, p = pitch; i < st; ++i, p += pitch) { + colorFill(ptr + p, ptr + dx + 1 + p, (PixelType)_fgColor); + colorFill(ptr - p, ptr + dx + 1 - p, (PixelType)_fgColor); + } + + } else if (dx == 0) { // vertical lines + // these ones use a static pitch increase. + while (y1++ <= y2) { + colorFill(ptr - st, ptr + st, (PixelType)_fgColor); + ptr += pitch; + } + + } else if (ABS(dx) == ABS(dy)) { // diagonal lines + // these ones also use a fixed pitch increase + pitch += (x2 > x1) ? 1 : -1; + + while (dy--) { + colorFill(ptr - st, ptr + st, (PixelType)_fgColor); + ptr += pitch; + } + + } else { // generic lines, use the standard algorithm... + drawLineAlg(x1, y1, x2, y2, dx, dy, (PixelType)_fgColor); + } +} + +/** CIRCLES **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawCircle(int x, int y, int r) { + if (x + r > Base::_activeSurface->w || y + r > Base::_activeSurface->h || + x - r < 0 || y - r < 0 || x == 0 || y == 0 || r <= 0) + return; + + if (Base::_fillMode != kFillDisabled && Base::_shadowOffset + && x + r + Base::_shadowOffset < Base::_activeSurface->w + && y + r + Base::_shadowOffset < Base::_activeSurface->h) { + drawCircleAlg(x + Base::_shadowOffset + 1, y + Base::_shadowOffset + 1, r, 0, kFillForeground); + } + + switch (Base::_fillMode) { + case kFillDisabled: + if (Base::_strokeWidth) + drawCircleAlg(x, y, r, _fgColor, kFillDisabled); + break; + + case kFillForeground: + drawCircleAlg(x, y, r, _fgColor, kFillForeground); + break; + + case kFillBackground: + if (Base::_strokeWidth > 1) { + drawCircleAlg(x, y, r, _fgColor, kFillForeground); + drawCircleAlg(x, y, r - Base::_strokeWidth, _bgColor, kFillBackground); + } else { + drawCircleAlg(x, y, r, _bgColor, kFillBackground); + drawCircleAlg(x, y, r, _fgColor, kFillDisabled); + } + break; + + case kFillGradient: + break; + } +} + +/** SQUARES **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawSquare(int x, int y, int w, int h) { + if (x + w > Base::_activeSurface->w || y + h > Base::_activeSurface->h || + w <= 0 || h <= 0 || x < 0 || y < 0) + return; + + if (Base::_fillMode != kFillDisabled && Base::_shadowOffset + && x + w + Base::_shadowOffset < Base::_activeSurface->w + && y + h + Base::_shadowOffset < Base::_activeSurface->h) { + drawSquareShadow(x, y, w, h, Base::_shadowOffset); + } + + switch (Base::_fillMode) { + case kFillDisabled: + if (Base::_strokeWidth) + drawSquareAlg(x, y, w, h, _fgColor, kFillDisabled); + break; + + case kFillForeground: + drawSquareAlg(x, y, w, h, _fgColor, kFillForeground); + break; + + case kFillBackground: + drawSquareAlg(x, y, w, h, _bgColor, kFillBackground); + drawSquareAlg(x, y, w, h, _fgColor, kFillDisabled); + break; + + case kFillGradient: + VectorRendererSpec::drawSquareAlg(x, y, w, h, 0, kFillGradient); + if (Base::_strokeWidth) + drawSquareAlg(x, y, w, h, _fgColor, kFillDisabled); + break; + } +} + +/** ROUNDED SQUARES **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawRoundedSquare(int x, int y, int r, int w, int h) { + if (x + w > Base::_activeSurface->w || y + h > Base::_activeSurface->h || + w <= 0 || h <= 0 || x < 0 || y < 0 || r <= 0) + return; + + if ((r << 1) > w || (r << 1) > h) + r = MIN(w >> 1, h >> 1); + + if (Base::_fillMode != kFillDisabled && Base::_shadowOffset + && x + w + Base::_shadowOffset < Base::_activeSurface->w + && y + h + Base::_shadowOffset < Base::_activeSurface->h) { + drawRoundedSquareShadow(x, y, r, w, h, Base::_shadowOffset); + } + + switch (Base::_fillMode) { + case kFillDisabled: + if (Base::_strokeWidth) + drawRoundedSquareAlg(x, y, r, w, h, _fgColor, kFillDisabled); + break; + + case kFillForeground: + drawRoundedSquareAlg(x, y, r, w, h, _fgColor, kFillForeground); + break; + + case kFillBackground: + VectorRendererSpec::drawRoundedSquareAlg(x, y, r, w, h, _bgColor, kFillBackground); + drawRoundedSquareAlg(x, y, r, w, h, _fgColor, kFillDisabled); + break; + + case kFillGradient: + if (Base::_strokeWidth > 1) { + drawRoundedSquareAlg(x, y, r, w, h, _fgColor, kFillForeground); + VectorRendererSpec::drawRoundedSquareAlg(x + Base::_strokeWidth/2, y + Base::_strokeWidth/2, + r - Base::_strokeWidth/2, w - Base::_strokeWidth, h - Base::_strokeWidth, 0, kFillGradient); + } else { + VectorRendererSpec::drawRoundedSquareAlg(x, y, r, w, h, 0, kFillGradient); + if (Base::_strokeWidth) + drawRoundedSquareAlg(x, y, r, w, h, _fgColor, kFillDisabled); + } + break; + } + + if (Base::_bevel) + drawRoundedSquareFakeBevel(x, y, r, w, h, Base::_bevel); +} + +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawTab(int x, int y, int r, int w, int h) { + if (x + w > Base::_activeSurface->w || y + h > Base::_activeSurface->h || + w <= 0 || h <= 0 || x < 0 || y < 0 || r > w || r > h) + return; + + if (r == 0 && Base::_bevel > 0) { + drawBevelTabAlg(x, y, w, h, Base::_bevel, _bevelColor, _fgColor, (Base::_dynamicData >> 16), (Base::_dynamicData & 0xFFFF)); + return; + } + + if (r == 0) return; + + switch (Base::_fillMode) { + case kFillDisabled: + return; + + case kFillGradient: + case kFillBackground: + drawTabAlg(x, y, w, h, r, (Base::_fillMode == kFillBackground) ? _bgColor : _fgColor, Base::_fillMode); + if (Base::_strokeWidth) + drawTabAlg(x, y, w, h, r, _fgColor, kFillDisabled, (Base::_dynamicData >> 16), (Base::_dynamicData & 0xFFFF)); + break; + + case kFillForeground: + drawTabAlg(x, y, w, h, r, (Base::_fillMode == kFillBackground) ? _bgColor : _fgColor, Base::_fillMode); + break; + } +} + +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawTriangle(int x, int y, int w, int h, TriangleOrientation orient) { + + if (x + w > Base::_activeSurface->w || y + h > Base::_activeSurface->h) + return; + + PixelType color = 0; + + if (Base::_strokeWidth <= 1) { + if (Base::_fillMode == kFillForeground) + color = _fgColor; + else if (Base::_fillMode == kFillBackground) + color = _bgColor; + } else { + if (Base::_fillMode == kFillDisabled) + return; + color = _fgColor; + } + + if (Base::_dynamicData != 0) + orient = (TriangleOrientation)Base::_dynamicData; + + int newW = w / 2; + if (newW % 2) newW++; + + switch(orient) { + case kTriangleUp: + case kTriangleDown: + drawTriangleFast(x + (newW / 2), y + (h / 2) - (newW / 2), newW, (orient == kTriangleDown), color, Base::_fillMode); + break; + + case kTriangleLeft: + case kTriangleRight: + case kTriangleAuto: + break; + } + + if (Base::_strokeWidth > 0) + if (Base::_fillMode == kFillBackground || Base::_fillMode == kFillGradient) { + drawTriangleFast(x + (newW / 2), y + (h / 2) - (newW / 2), newW, (orient == kTriangleDown), _fgColor, kFillDisabled); + } +} + + + + + +/******************************************************************** + ******************************************************************** + * Aliased Primitive drawing ALGORITHMS - VectorRendererSpec + ******************************************************************** + ********************************************************************/ +/** TAB ALGORITHM - NON AA */ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawTabAlg(int x1, int y1, int w, int h, int r, PixelType color, VectorRenderer::FillMode fill_m, int baseLeft, int baseRight) { + int f, ddF_x, ddF_y; + int x, y, px, py; + int pitch = Base::surfacePitch(); + int sw = 0, sp = 0, hp = 0; + + PixelType *ptr_tl = (PixelType *)Base::_activeSurface->getBasePtr(x1 + r, y1 + r); + PixelType *ptr_tr = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w - r, y1 + r); + PixelType *ptr_fill = (PixelType *)Base::_activeSurface->getBasePtr(x1, y1); + + int real_radius = r; + int short_h = h - r + 2; + int long_h = h; + + if (fill_m == kFillDisabled) { + while (sw++ < Base::_strokeWidth) { + colorFill(ptr_fill + sp + r, ptr_fill + w + 1 + sp - r, color); + colorFill(ptr_fill + hp - sp + r, ptr_fill + w + hp + 1 - sp - r, color); + sp += pitch; + + __BE_RESET(); + r--; + + while (x++ < y) { + __BE_ALGORITHM(); + *(ptr_tr + (y) - (px)) = color; + *(ptr_tr + (x) - (py)) = color; + *(ptr_tl - (x) - (py)) = color; + *(ptr_tl - (y) - (px)) = color; + + if (Base::_strokeWidth > 1) { + *(ptr_tr + (y) - (px - pitch)) = color; + *(ptr_tr + (x) - (py)) = color; + *(ptr_tl - (x) - (py)) = color; + *(ptr_tl - (y) - (px - pitch)) = color; + } + } + } + + ptr_fill += pitch * real_radius; + while (short_h--) { + colorFill(ptr_fill, ptr_fill + Base::_strokeWidth, color); + colorFill(ptr_fill + w - Base::_strokeWidth + 1, ptr_fill + w + 1, color); + ptr_fill += pitch; + } + + if (baseLeft) { + sw = 0; + ptr_fill = (PixelType *)Base::_activeSurface->getBasePtr(x1, y1 + h + 1); + while (sw++ < Base::_strokeWidth) { + colorFill(ptr_fill - baseLeft, ptr_fill, color); + ptr_fill += pitch; + } + } + + if (baseRight) { + sw = 0; + ptr_fill = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w, y1 + h + 1); + while (sw++ < Base::_strokeWidth) { + colorFill(ptr_fill, ptr_fill + baseRight, color); + ptr_fill += pitch; + } + } + } else { + __BE_RESET(); + + PixelType color1, color2; + color1 = color2 = color; + + while (x++ < y) { + __BE_ALGORITHM(); + + if (fill_m == kFillGradient) { + color1 = calcGradient(real_radius - x, long_h); + color2 = calcGradient(real_radius - y, long_h); + } + + colorFill(ptr_tl - x - py, ptr_tr + x - py, color2); + colorFill(ptr_tl - y - px, ptr_tr + y - px, color1); + + *(ptr_tr + (y) - (px)) = color1; + *(ptr_tr + (x) - (py)) = color2; + *(ptr_tl - (x) - (py)) = color2; + *(ptr_tl - (y) - (px)) = color1; + } + + ptr_fill += pitch * r; + while (short_h--) { + if (fill_m == kFillGradient) + color = calcGradient(real_radius++, long_h); + colorFill(ptr_fill, ptr_fill + w + 1, color); + ptr_fill += pitch; + } + } +} + + +/** BEVELED TABS FOR CLASSIC THEME **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawBevelTabAlg(int x, int y, int w, int h, int bevel, PixelType top_color, PixelType bottom_color, int baseLeft, int baseRight) { + int pitch = Base::surfacePitch(); + int i, j; + + PixelType *ptr_left = (PixelType *)_activeSurface->getBasePtr(x, y); + + i = bevel; + while (i--) { + colorFill(ptr_left, ptr_left + w, top_color); + ptr_left += pitch; + } + + if (baseLeft > 0) { + i = h - bevel; + ptr_left = (PixelType *)_activeSurface->getBasePtr(x, y); + while (i--) { + colorFill(ptr_left, ptr_left + bevel, top_color); + ptr_left += pitch; + } + } + + i = h - bevel; + j = bevel - 1; + ptr_left = (PixelType *)_activeSurface->getBasePtr(x + w - bevel, y); + while (i--) { + colorFill(ptr_left + j, ptr_left + bevel, bottom_color); + if (j > 0) j--; + ptr_left += pitch; + } + + i = bevel; + ptr_left = (PixelType *)_activeSurface->getBasePtr(x + w - bevel, y + h - bevel); + while (i--) { + colorFill(ptr_left, ptr_left + baseRight + bevel, bottom_color); + + if (baseLeft) + colorFill(ptr_left - w - baseLeft + bevel, ptr_left - w + bevel + bevel, top_color); + ptr_left += pitch; + } +} + +/** SQUARE ALGORITHM **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawSquareAlg(int x, int y, int w, int h, PixelType color, VectorRenderer::FillMode fill_m) { + PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(x, y); + int pitch = Base::surfacePitch(); + int max_h = h; + + if (fill_m != kFillDisabled) { + while (h--) { + if (fill_m == kFillGradient) + color = calcGradient(max_h - h, max_h); + + colorFill(ptr, ptr + w, color); + ptr += pitch; + } + } else { + int sw = Base::_strokeWidth, sp = 0, hp = pitch * (h - 1); + + while (sw--) { + colorFill(ptr + sp, ptr + w + sp, color); + colorFill(ptr + hp - sp, ptr + w + hp - sp, color); + sp += pitch; + } + + while (h--) { + colorFill(ptr, ptr + Base::_strokeWidth, color); + colorFill(ptr + w - Base::_strokeWidth, ptr + w, color); + ptr += pitch; + } + } +} + +/** SQUARE ALGORITHM **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawBevelSquareAlg(int x, int y, int w, int h, int bevel, PixelType top_color, PixelType bottom_color, bool fill) { + int pitch = Base::surfacePitch(); + + int height = h; + PixelType *ptr_fill = (PixelType *)_activeSurface->getBasePtr(x, y); + + if (fill) { + while (height--) { + blendFill(ptr_fill, ptr_fill + w, _bgColor, 200); + ptr_fill += pitch; + } + } + + int i, j; + x = MAX(x - bevel, 0); + y = MAX(y - bevel, 0); + h += bevel << 1; + w += bevel << 1; + + PixelType *ptr_left = (PixelType *)_activeSurface->getBasePtr(x, y); + + i = bevel; + while (i--) { + colorFill(ptr_left, ptr_left + w, top_color); + ptr_left += pitch; + } + + i = h - bevel; + ptr_left = (PixelType *)_activeSurface->getBasePtr(x, y + bevel); + while (i--) { + colorFill(ptr_left, ptr_left + bevel, top_color); + ptr_left += pitch; + } + + i = bevel; + ptr_left = (PixelType *)_activeSurface->getBasePtr(x, y + h - bevel); + while (i--) { + colorFill(ptr_left + i, ptr_left + w, bottom_color); + ptr_left += pitch; + } + + i = h - bevel; + j = bevel - 1; + ptr_left = (PixelType *)_activeSurface->getBasePtr(x + w - bevel, y); + while (i--) { + colorFill(ptr_left + j, ptr_left + bevel, bottom_color); + if (j > 0) j--; + ptr_left += pitch; + } +} + +/** GENERIC LINE ALGORITHM **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType,PixelFormat>:: +drawLineAlg(int x1, int y1, int x2, int y2, int dx, int dy, PixelType color) { + PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(x1, y1); + int pitch = Base::surfacePitch(); + int xdir = (x2 > x1) ? 1 : -1; + + *ptr = (PixelType)color; + + if (dx > dy) { + int ddy = dy * 2; + int dysub = ddy - (dx * 2); + int error_term = ddy - dx; + + while (dx--) { + if (error_term >= 0) { + ptr += pitch; + error_term += dysub; + } else { + error_term += ddy; + } + + ptr += xdir; + *ptr = (PixelType)color; + } + } else { + int ddx = dx * 2; + int dxsub = ddx - (dy * 2); + int error_term = ddx - dy; + + while (dy--) { + if (error_term >= 0) { + ptr += xdir; + error_term += dxsub; + } else { + error_term += ddx; + } + + ptr += pitch; + *ptr = (PixelType)color; + } + } + + ptr = (PixelType *)_activeSurface->getBasePtr(x2, y2); + *ptr = (PixelType)color; +} + +/** VERTICAL TRIANGLE DRAWING ALGORITHM **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType,PixelFormat>:: +drawTriangleVertAlg(int x1, int y1, int w, int h, bool inverted, PixelType color, VectorRenderer::FillMode fill_m) { + int dx = w >> 1, dy = h, gradient_h = 0; + int pitch = Base::surfacePitch(); + PixelType *ptr_right = 0, *ptr_left = 0; + + if (inverted) { + ptr_right = (PixelType *)_activeSurface->getBasePtr(x1, y1); + ptr_left = (PixelType *)_activeSurface->getBasePtr(x1 + w, y1); + } else { + ptr_right = ptr_left = (PixelType *)_activeSurface->getBasePtr(x1 + dx, y1); + } + + if (dx > dy) { + int ddy = dy * 2; + int dysub = ddy - (dx * 2); + int error_term = ddy - dx; + + switch(fill_m) { + case kFillDisabled: + while (dx--) { + __TRIANGLE_MAINX(); + *ptr_right = color; + *ptr_left = color; + } + colorFill(ptr_left, ptr_right, color); + break; + + case kFillForeground: + case kFillBackground: + while (dx--) { + __TRIANGLE_MAINX(); + if (inverted) colorFill(ptr_right, ptr_left, color); + else colorFill(ptr_left, ptr_right, color); + } + break; + + case kFillGradient: + while (dx--) { + __TRIANGLE_MAINX(); + if (inverted) colorFill(ptr_right, ptr_left, calcGradient(gradient_h++, h)); + else colorFill(ptr_left, ptr_right, calcGradient(gradient_h++, h)); + } + break; + } + } else { + int ddx = dx * 2; + int dxsub = ddx - (dy * 2); + int error_term = ddx - dy; + + switch(fill_m) { + case kFillDisabled: + while (dy--) { + __TRIANGLE_MAINY(); + *ptr_right = color; + *ptr_left = color; + } + colorFill(ptr_left, ptr_right, color); + break; + + case kFillForeground: + case kFillBackground: + while (dy--) { + __TRIANGLE_MAINY(); + if (inverted) colorFill(ptr_right, ptr_left, color); + else colorFill(ptr_left, ptr_right, color); + } + break; + case kFillGradient: + while (dy--) { + __TRIANGLE_MAINY(); + if (inverted) colorFill(ptr_right, ptr_left, calcGradient(gradient_h++, h)); + else colorFill(ptr_left, ptr_right, calcGradient(gradient_h++, h)); + } + break; + } + } +} + + +/** VERTICAL TRIANGLE DRAWING - FAST VERSION FOR SQUARED TRIANGLES */ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType,PixelFormat>:: +drawTriangleFast(int x1, int y1, int size, bool inverted, PixelType color, VectorRenderer::FillMode fill_m) { + int pitch = Base::surfacePitch(); + int hstep = 0, dy = size; + bool grad = (fill_m == kFillGradient); + + PixelType *ptr_right = 0, *ptr_left = 0; + + if (inverted) { + ptr_left = (PixelType *)_activeSurface->getBasePtr(x1, y1); + ptr_right = (PixelType *)_activeSurface->getBasePtr(x1 + size, y1); + } else { + ptr_left = (PixelType *)_activeSurface->getBasePtr(x1, y1 + size); + ptr_right = (PixelType *)_activeSurface->getBasePtr(x1 + size, y1 + size); + pitch = -pitch; + } + + if (fill_m == kFillDisabled) { + while (ptr_left < ptr_right) { + *ptr_left = color; + *ptr_right = color; + ptr_left += pitch; + ptr_right += pitch; + if (hstep++ % 2) { + ptr_left++; + ptr_right--; + } + } + } else { + while (ptr_left < ptr_right) { + colorFill(ptr_left, ptr_right, grad ? calcGradient(dy--, size) : color); + ptr_left += pitch; + ptr_right += pitch; + if (hstep++ % 2) { + ptr_left++; + ptr_right--; + } + } + } +} + +/** ROUNDED SQUARE ALGORITHM **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawRoundedSquareAlg(int x1, int y1, int r, int w, int h, PixelType color, VectorRenderer::FillMode fill_m) { + int f, ddF_x, ddF_y; + int x, y, px, py; + int pitch = Base::surfacePitch(); + int sw = 0, sp = 0, hp = h * pitch; + + PixelType *ptr_tl = (PixelType *)Base::_activeSurface->getBasePtr(x1 + r, y1 + r); + PixelType *ptr_tr = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w - r, y1 + r); + PixelType *ptr_bl = (PixelType *)Base::_activeSurface->getBasePtr(x1 + r, y1 + h - r); + PixelType *ptr_br = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w - r, y1 + h - r); + PixelType *ptr_fill = (PixelType *)Base::_activeSurface->getBasePtr(x1, y1); + + int real_radius = r; + int short_h = h - (2 * r) + 2; + int long_h = h; + + if (fill_m == kFillDisabled) { + while (sw++ < Base::_strokeWidth) { + colorFill(ptr_fill + sp + r, ptr_fill + w + 1 + sp - r, color); + colorFill(ptr_fill + hp - sp + r, ptr_fill + w + hp + 1 - sp - r, color); + sp += pitch; + + __BE_RESET(); + r--; + + while (x++ < y) { + __BE_ALGORITHM(); + __BE_DRAWCIRCLE(ptr_tr, ptr_tl, ptr_bl, ptr_br, x, y, px, py); + + if (Base::_strokeWidth > 1) { + __BE_DRAWCIRCLE(ptr_tr, ptr_tl, ptr_bl, ptr_br, x - 1, y, px, py); + __BE_DRAWCIRCLE(ptr_tr, ptr_tl, ptr_bl, ptr_br, x, y, px - pitch, py); + } + } + } + + ptr_fill += pitch * real_radius; + while (short_h--) { + colorFill(ptr_fill, ptr_fill + Base::_strokeWidth, color); + colorFill(ptr_fill + w - Base::_strokeWidth + 1, ptr_fill + w + 1, color); + ptr_fill += pitch; + } + } else { + __BE_RESET(); + PixelType color1, color2, color3, color4; + + if (fill_m == kFillGradient) { + while (x++ < y) { + __BE_ALGORITHM(); + + color1 = calcGradient(real_radius - x, long_h); + color2 = calcGradient(real_radius - y, long_h); + color3 = calcGradient(long_h - r + x, long_h); + color4 = calcGradient(long_h - r + y, long_h); + + colorFill(ptr_tl - x - py, ptr_tr + x - py, color2); + colorFill(ptr_tl - y - px, ptr_tr + y - px, color1); + + colorFill(ptr_bl - x + py, ptr_br + x + py, color4); + colorFill(ptr_bl - y + px, ptr_br + y + px, color3); + + __BE_DRAWCIRCLE_XCOLOR(ptr_tr, ptr_tl, ptr_bl, ptr_br, x, y, px, py); + } + } else { + while (x++ < y) { + __BE_ALGORITHM(); + + colorFill(ptr_tl - x - py, ptr_tr + x - py, color); + colorFill(ptr_tl - y - px, ptr_tr + y - px, color); + + colorFill(ptr_bl - x + py, ptr_br + x + py, color); + colorFill(ptr_bl - y + px, ptr_br + y + px, color); + + // do not remove - messes up the drawing at lower resolutions + __BE_DRAWCIRCLE(ptr_tr, ptr_tl, ptr_bl, ptr_br, x, y, px, py); + } + } + + ptr_fill += pitch * r; + while (short_h--) { + if (fill_m == kFillGradient) + color = calcGradient(real_radius++, long_h); + colorFill(ptr_fill, ptr_fill + w + 1, color); + ptr_fill += pitch; + } + } +} + +/** CIRCLE ALGORITHM **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawCircleAlg(int x1, int y1, int r, PixelType color, VectorRenderer::FillMode fill_m) { + int f, ddF_x, ddF_y; + int x, y, px, py, sw = 0; + int pitch = Base::surfacePitch(); + PixelType *ptr = (PixelType *)Base::_activeSurface->getBasePtr(x1, y1); + + if (fill_m == kFillDisabled) { + while (sw++ < Base::_strokeWidth) { + __BE_RESET(); + r--; + + *(ptr + y) = color; + *(ptr - y) = color; + *(ptr + py) = color; + *(ptr - py) = color; + + while (x++ < y) { + __BE_ALGORITHM(); + __BE_DRAWCIRCLE(ptr, ptr, ptr, ptr, x, y, px, py); + + if (Base::_strokeWidth > 1) { + __BE_DRAWCIRCLE(ptr, ptr, ptr, ptr, x - 1, y, px, py); + __BE_DRAWCIRCLE(ptr, ptr, ptr, ptr, x, y, px - pitch, py); + } + } + } + } else { + colorFill(ptr - r, ptr + r, color); + __BE_RESET(); + + while (x++ < y) { + __BE_ALGORITHM(); + colorFill(ptr - x + py, ptr + x + py, color); + colorFill(ptr - x - py, ptr + x - py, color); + colorFill(ptr - y + px, ptr + y + px, color); + colorFill(ptr - y - px, ptr + y - px, color); + } + } +} + + + + + +/******************************************************************** + ******************************************************************** + * SHADOW drawing algorithms - VectorRendererSpec ******************* + ******************************************************************** + ********************************************************************/ +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawSquareShadow(int x, int y, int w, int h, int blur) { + PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(x + w - 1, y + blur); + int pitch = Base::surfacePitch(); + int i, j; + + i = h - blur; + + while (i--) { + j = blur; + while (j--) + blendPixelPtr(ptr + j, 0, ((blur - j) << 8) / blur); + ptr += pitch; + } + + ptr = (PixelType *)_activeSurface->getBasePtr(x + blur, y + h - 1); + + while (i++ < blur) { + j = w - blur; + while (j--) + blendPixelPtr(ptr + j, 0, ((blur - i) << 8) / blur); + ptr += pitch; + } + + ptr = (PixelType *)_activeSurface->getBasePtr(x + w, y + h); + + i = 0; + while (i++ < blur) { + j = blur - 1; + while (j--) + blendPixelPtr(ptr + j, 0, (((blur - j) * (blur - i)) << 8) / (blur * blur)); + ptr += pitch; + } +} + +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawRoundedSquareShadow(int x1, int y1, int r, int w, int h, int blur) { + int f, ddF_x, ddF_y; + int x, y, px, py; + int pitch = Base::surfacePitch(); + int alpha = 102; + + x1 += blur; + y1 += blur; + + PixelType *ptr_tr = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w - r, y1 + r); + PixelType *ptr_bl = (PixelType *)Base::_activeSurface->getBasePtr(x1 + r, y1 + h - r); + PixelType *ptr_br = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w - r, y1 + h - r); + PixelType *ptr_fill = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w - blur, y1 + r); + + int short_h = h - (2 * r) + 1; + + __BE_RESET(); + + // HACK: As we are drawing circles exploting 8-axis symmetry, + // there are 4 pixels on each circle which are drawn twice. + // this is ok on filled circles, but when blending on surfaces, + // we cannot let it blend twice. awful. + uint32 hb = 0; + + while (x++ < y) { + __BE_ALGORITHM(); + + if (((1 << x) & hb) == 0) { + blendFill(ptr_tr - px - r, ptr_tr + y - px, 0, alpha); + blendFill(ptr_bl - y + px, ptr_br + y + px, 0, alpha); + hb |= (1 << x); + } + + if (((1 << y) & hb) == 0) { + blendFill(ptr_tr - r - py, ptr_tr + x - py, 0, alpha); + blendFill(ptr_bl - x + py, ptr_br + x + py, 0, alpha); + hb |= (1 << y); + } + } + + while (short_h--) { + blendFill(ptr_fill - r, ptr_fill + blur, 0, alpha); + ptr_fill += pitch; + } +} + +template<typename PixelType, typename PixelFormat> +void VectorRendererSpec<PixelType, PixelFormat>:: +drawRoundedSquareFakeBevel(int x1, int y1, int r, int w, int h, int amount) { + int x, y; + int p = Base::surfacePitch(), px, py; + int sw = 0, sp = 0; + + uint32 rsq = (r * r) << 16; + uint32 T = 0, oldT; + uint8 a1, a2; + + PixelType color = _bevelColor; //RGBToColor<PixelFormat>(63, 60, 17); + + PixelType *ptr_tl = (PixelType *)Base::_activeSurface->getBasePtr(x1 + r, y1 + r); + PixelType *ptr_tr = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w - r, y1 + r); + PixelType *ptr_bl = (PixelType *)Base::_activeSurface->getBasePtr(x1 + r, y1 + h - r); + PixelType *ptr_fill = (PixelType *)Base::_activeSurface->getBasePtr(x1, y1); + + int short_h = h - 2 * r; + + while (sw++ < amount) { + colorFill(ptr_fill + sp + r, ptr_fill + w + 1 + sp - r, color); + sp += p; + + x = r - (sw - 1); y = 0; T = 0; + px = p * x; py = 0; + + while (x > y++) { + __WU_ALGORITHM(); + + blendPixelPtr(ptr_tr + (y) - (px - p), color, a2); + blendPixelPtr(ptr_tr + (x - 1) - (py), color, a2); + blendPixelPtr(ptr_tl - (x - 1) - (py), color, a2); + blendPixelPtr(ptr_tl - (y) - (px - p), color, a2); + blendPixelPtr(ptr_bl - (y) + (px - p), color, a2); + blendPixelPtr(ptr_bl - (x - 1) + (py), color, a2); + + blendPixelPtr(ptr_tr + (y) - (px), color, a1); + blendPixelPtr(ptr_tr + (x) - (py), color, a1); + blendPixelPtr(ptr_tl - (x) - (py), color, a1); + blendPixelPtr(ptr_tl - (y) - (px), color, a1); + blendPixelPtr(ptr_bl - (y) + (px), color, a1); + blendPixelPtr(ptr_bl - (x) + (py), color, a1); + } + } + + ptr_fill += p * r; + while (short_h-- >= 0) { + colorFill(ptr_fill, ptr_fill + amount, color); + ptr_fill += p; + } +} + + + + + + + +/******************************************************************************/ + + + + + + + +#ifndef DISABLE_FANCY_THEMES + +/******************************************************************** + * ANTIALIASED PRIMITIVES drawing algorithms - VectorRendererAA + ********************************************************************/ +/** LINES **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererAA<PixelType, PixelFormat>:: +drawLineAlg(int x1, int y1, int x2, int y2, int dx, int dy, PixelType color) { + + PixelType *ptr = (PixelType *)Base::_activeSurface->getBasePtr(x1, y1); + int pitch = Base::surfacePitch(); + int xdir = (x2 > x1) ? 1 : -1; + uint16 error_tmp, error_acc, gradient; + uint8 alpha; + + *ptr = (PixelType)color; + + if (dx > dy) { + gradient = (uint32)(dy << 16) / (uint32)dx; + error_acc = 0; + + while (--dx) { + error_tmp = error_acc; + error_acc += gradient; + + if (error_acc <= error_tmp) + ptr += pitch; + + ptr += xdir; + alpha = (error_acc >> 8); + + blendPixelPtr(ptr, color, ~alpha); + blendPixelPtr(ptr + pitch, color, alpha); + } + } else { + gradient = (uint32)(dx << 16) / (uint32)dy; + error_acc = 0; + + while (--dy) { + error_tmp = error_acc; + error_acc += gradient; + + if (error_acc <= error_tmp) + ptr += xdir; + + ptr += pitch; + alpha = (error_acc >> 8); + + blendPixelPtr(ptr, color, ~alpha); + blendPixelPtr(ptr + xdir, color, alpha); + } + } + + Base::putPixel(x2, y2, color); +} + +/** ROUNDED SQUARES **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererAA<PixelType, PixelFormat>:: +drawRoundedSquareAlg(int x1, int y1, int r, int w, int h, PixelType color, VectorRenderer::FillMode fill_m) { + int x, y; + int p = Base::surfacePitch(), px, py; + int sw = 0, sp = 0, hp = h * p; + + uint32 rsq = (r * r) << 16; + uint32 T = 0, oldT; + uint8 a1, a2; + + PixelType *ptr_tl = (PixelType *)Base::_activeSurface->getBasePtr(x1 + r, y1 + r); + PixelType *ptr_tr = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w - r, y1 + r); + PixelType *ptr_bl = (PixelType *)Base::_activeSurface->getBasePtr(x1 + r, y1 + h - r); + PixelType *ptr_br = (PixelType *)Base::_activeSurface->getBasePtr(x1 + w - r, y1 + h - r); + PixelType *ptr_fill = (PixelType *)Base::_activeSurface->getBasePtr(x1, y1); + + int short_h = h - 2 * r; + + if (fill_m == VectorRenderer::kFillDisabled) { + while (sw++ < Base::_strokeWidth) { + colorFill(ptr_fill + sp + r, ptr_fill + w + 1 + sp - r, color); + colorFill(ptr_fill + hp - sp + r, ptr_fill + w + hp + 1 - sp - r, color); + sp += p; + + x = r - (sw - 1); y = 0; T = 0; + px = p * x; py = 0; + + while (x > y++) { + __WU_ALGORITHM(); + + if (sw != 1 && sw != Base::_strokeWidth) + a2 = a1 = 255; + + __WU_DRAWCIRCLE(ptr_tr, ptr_tl, ptr_bl, ptr_br, (x - 1), y, (px - p), py, a2); + __WU_DRAWCIRCLE(ptr_tr, ptr_tl, ptr_bl, ptr_br, x, y, px, py, a1); + } + } + + ptr_fill += p * r; + while (short_h-- >= 0) { + colorFill(ptr_fill, ptr_fill + Base::_strokeWidth, color); + colorFill(ptr_fill + w - Base::_strokeWidth + 1, ptr_fill + w + 1, color); + ptr_fill += p; + } + } else { + x = r; y = 0; T = 0; + px = p * x; py = 0; + + while (x > 1 + y++) { + __WU_ALGORITHM(); + + colorFill(ptr_tl - x - py, ptr_tr + x - py, color); + colorFill(ptr_tl - y - px, ptr_tr + y - px, color); + + colorFill(ptr_bl - x + py, ptr_br + x + py, color); + colorFill(ptr_bl - y + px, ptr_br + y + px, color); + + __WU_DRAWCIRCLE(ptr_tr, ptr_tl, ptr_bl, ptr_br, x, y, px, py, a1); + } + + ptr_fill += p * r; + while (short_h-- >= 0) { + colorFill(ptr_fill, ptr_fill + w + 1, color); + ptr_fill += p; + } + } +} + +/** CIRCLES **/ +template<typename PixelType, typename PixelFormat> +void VectorRendererAA<PixelType, PixelFormat>:: +drawCircleAlg(int x1, int y1, int r, PixelType color, VectorRenderer::FillMode fill_m) { + int x, y, sw = 0; + int p = Base::surfacePitch(), px, py; + + uint32 rsq = (r * r) << 16; + uint32 T = 0, oldT; + uint8 a1, a2; + + PixelType *ptr = (PixelType *)Base::_activeSurface->getBasePtr(x1, y1); + + if (fill_m == VectorRenderer::kFillDisabled) { + while (sw++ < Base::_strokeWidth) { + x = r - (sw - 1); y = 0; T = 0; + px = p * x; py = 0; + + *(ptr + x) = (PixelType)color; + *(ptr - x) = (PixelType)color; + *(ptr + px) = (PixelType)color; + *(ptr - px) = (PixelType)color; + + while (x > y++) { + __WU_ALGORITHM(); + + if (sw != 1 && sw != Base::_strokeWidth) + a2 = a1 = 255; + + __WU_DRAWCIRCLE(ptr, ptr, ptr, ptr, (x - 1), y, (px - p), py, a2); + __WU_DRAWCIRCLE(ptr, ptr, ptr, ptr, x, y, px, py, a1); + } + } + } else { + colorFill(ptr - r, ptr + r + 1, color); + x = r; y = 0; T = 0; + px = p * x; py = 0; + + while (x > y++) { + __WU_ALGORITHM(); + + colorFill(ptr - x + py, ptr + x + py, color); + colorFill(ptr - x - py, ptr + x - py, color); + colorFill(ptr - y + px, ptr + y + px, color); + colorFill(ptr - y - px, ptr + y - px, color); + + __WU_DRAWCIRCLE(ptr, ptr, ptr, ptr, x, y, px, py, a1); + } + } +} + +#endif + +} diff --git a/graphics/VectorRendererSpec.h b/graphics/VectorRendererSpec.h new file mode 100644 index 0000000000..f1bcd1854e --- /dev/null +++ b/graphics/VectorRendererSpec.h @@ -0,0 +1,300 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef VECTOR_RENDERER_SPEC_H +#define VECTOR_RENDERER_SPEC_H + +#include "graphics/VectorRenderer.h" + +namespace Graphics { + +/** + * VectorRendererSpec: Specialized Vector Renderer Class + * + * This templated class implements the basic subset of vector operations for + * all platforms by allowing the user to provide the actual Pixel Type and + * pixel information structs. + * + * This class takes two template parameters: + * + * @param PixelType Defines a type which may hold the color value of a single + * pixel, such as "byte" or "uint16" for 8 and 16 BPP respectively. + * + * @param PixelFormat Defines the type of the PixelFormat struct which contains all + * the actual information of the pixels being used, as declared in "graphics/colormasks.h" + * + * TODO: Expand documentation. + * + * @see VectorRenderer + */ +template<typename PixelType, typename PixelFormat> +class VectorRendererSpec : public VectorRenderer { + typedef VectorRenderer Base; + +public: + VectorRendererSpec() { + _bitmapAlphaColor = RGBToColor<PixelFormat>(255, 0, 255); + } + + void drawLine(int x1, int y1, int x2, int y2); + void drawCircle(int x, int y, int r); + void drawSquare(int x, int y, int w, int h); + void drawRoundedSquare(int x, int y, int r, int w, int h); + void drawTriangle(int x, int y, int base, int height, TriangleOrientation orient); + void drawTab(int x, int y, int r, int w, int h); + void drawBeveledSquare(int x, int y, int w, int h, int bevel) { + drawBevelSquareAlg(x, y, w, h, bevel, _bevelColor, _fgColor, Base::_fillMode != kFillDisabled); + } + void drawString(const Graphics::Font *font, const Common::String &text, + const Common::Rect &area, Graphics::TextAlign alignH, + GUI::ThemeEngine::TextAlignVertical alignV, int deltax, bool elipsis); + + void setFgColor(uint8 r, uint8 g, uint8 b) { _fgColor = RGBToColor<PixelFormat>(r, g, b); } + void setBgColor(uint8 r, uint8 g, uint8 b) { _bgColor = RGBToColor<PixelFormat>(r, g, b); } + void setBevelColor(uint8 r, uint8 g, uint8 b) { _bevelColor = RGBToColor<PixelFormat>(r, g, b); } + void setGradientColors(uint8 r1, uint8 g1, uint8 b1, uint8 r2, uint8 g2, uint8 b2); + + void copyFrame(OSystem *sys, const Common::Rect &r); + void copyWholeFrame(OSystem *sys) { copyFrame(sys, Common::Rect(0, 0, _activeSurface->w, _activeSurface->h)); } + + void fillSurface(); + void blitSurface(const Graphics::Surface *source, const Common::Rect &r); + void blitSubSurface(const Graphics::Surface *source, const Common::Rect &r); + void blitAlphaBitmap(const Graphics::Surface *source, const Common::Rect &r); + + void applyScreenShading(GUI::ThemeEngine::ShadingStyle shadingStyle); + +protected: + + /** + * Draws a single pixel on the surface with the given coordinates and + * the given color. + * + * @param x Horizontal coordinate of the pixel. + * @param y Vertical coordinate of the pixel. + * @param color Color of the pixel + */ + inline void putPixel(int x, int y, PixelType color) { + PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(x, y); + *ptr = color; + } + + /** + * Blends a single pixel on the surface with the given coordinates, color + * and Alpha intensity. + * + * @param x Horizontal coordinate of the pixel. + * @param y Vertical coordinate of the pixel. + * @param color Color of the pixel + * @param alpha Alpha intensity of the pixel (0-255) + */ + inline void blendPixel(int x, int y, PixelType color, uint8 alpha) { + blendPixelPtr((PixelType*)Base::_activeSurface->getBasePtr(x, y), color, alpha); + } + + /** + * Blends a single pixel on the surface in the given pixel pointer, using supplied color + * and Alpha intensity. + * + * This is implemented to prevent blendPixel() to calculate the surface pointer on each call. + * Optimized drawing algorithms should call this function when possible. + * + * @see blendPixel + * @param ptr Pointer to the pixel to blend on top of + * @param color Color of the pixel + * @param alpha Alpha intensity of the pixel (0-255) + */ + inline void blendPixelPtr(PixelType *ptr, PixelType color, uint8 alpha); + + /** + * PRIMITIVE DRAWING ALGORITHMS + * + * Generic algorithms for drawing all kinds of aliased primitive shapes. + * These may be overloaded in inheriting classes to implement platform-specific + * optimizations or improve looks. + * + * @see VectorRendererAA + * @see VectorRendererAA::drawLineAlg + * @see VectorRendererAA::drawCircleAlg + */ + virtual void drawLineAlg(int x1, int y1, int x2, int y2, + int dx, int dy, PixelType color); + + virtual void drawCircleAlg(int x, int y, int r, + PixelType color, FillMode fill_m); + + virtual void drawRoundedSquareAlg(int x1, int y1, int r, int w, int h, + PixelType color, FillMode fill_m); + + virtual void drawSquareAlg(int x, int y, int w, int h, + PixelType color, FillMode fill_m); + + virtual void drawTriangleVertAlg(int x, int y, int w, int h, + bool inverted, PixelType color, FillMode fill_m); + + virtual void drawTriangleFast(int x, int y, int size, + bool inverted, PixelType color, FillMode fill_m); + + virtual void drawBevelSquareAlg(int x, int y, int w, int h, + int bevel, PixelType top_color, PixelType bottom_color, bool fill); + + virtual void drawTabAlg(int x, int y, int w, int h, int r, + PixelType color, VectorRenderer::FillMode fill_m, + int baseLeft = 0, int baseRight = 0); + + virtual void drawBevelTabAlg(int x, int y, int w, int h, + int bevel, PixelType topColor, PixelType bottomColor, + int baseLeft = 0, int baseRight = 0); + + /** + * SHADOW DRAWING ALGORITHMS + * + * Optimized versions of the primitive drawing algorithms with alpha blending + * for shadow drawing. + * There functions may be overloaded in inheriting classes to improve performance + * in the slowest platforms where pixel alpha blending just doesn't cut it. + * + * @param blur Intensity/size of the shadow. + */ + virtual void drawSquareShadow(int x, int y, int w, int h, int blur); + virtual void drawRoundedSquareShadow(int x, int y, int r, int w, int h, int blur); + virtual void drawRoundedSquareFakeBevel(int x, int y, int r, int w, int h, int amount); + + /** + * Calculates the color gradient on a given point. + * This function assumes that the gradient start/end colors have been set + * beforehand from the API function call. + * + * @param pos Progress of the gradient. + * @param max Maximum amount of the progress. + * @return Composite color of the gradient at the given "progress" amount. + */ + inline PixelType calcGradient(uint32 pos, uint32 max); + + /** + * Fills several pixels in a row with a given color and the specified alpha blending. + * + * @see blendPixelPtr + * @see blendPixel + * @param first Pointer to the first pixel to fill. + * @param last Pointer to the last pixel to fill. + * @param color Color of the pixel + * @param alpha Alpha intensity of the pixel (0-255) + */ + inline void blendFill(PixelType *first, PixelType *last, PixelType color, uint8 alpha) { + while (first != last) blendPixelPtr(first++, color, alpha); + } + + /** + * Fills several pixels in a row with a given color. + * + * This is a replacement function for Common::set_to, using an unrolled + * loop to maximize performance on most architectures. + * This function may (and should) be overloaded in any child renderers + * for portable platforms with platform-specific assembly code. + * + * This fill operation is extensively used throughout the renderer, so this + * counts as one of the main bottlenecks. Please replace it with assembly + * when possible! + * + * @param first Pointer to the first pixel to fill. + * @param last Pointer to the last pixel to fill. + * @param color Color of the pixel + */ + inline void colorFill(PixelType *first, PixelType *last, PixelType color); + +#ifndef DISABLE_FANCY_THEMES + void areaConvolution(const Common::Rect &area, const int filter[3][3], int filterDiv, int offset); +#endif + + PixelType _fgColor; /**< Foreground color currently being used to draw on the renderer */ + PixelType _bgColor; /**< Background color currently being used to draw on the renderer */ + + PixelType _gradientStart; /**< Start color for the fill gradient */ + PixelType _gradientEnd; /**< End color for the fill gradient */ + + PixelType _bevelColor; + PixelType _bitmapAlphaColor; +}; + + +#ifndef DISABLE_FANCY_THEMES +/** + * VectorRendererAA: Anti-Aliased Vector Renderer Class + * + * This templated class inherits all the functionality of the VectorRendererSpec + * class but uses better looking yet slightly slower AA algorithms for drawing + * most primitives. May be used in faster platforms. + * + * TODO: Expand documentation. + * + * @see VectorRenderer + * @see VectorRendererSpec + */ +template<typename PixelType, typename PixelFormat> +class VectorRendererAA : public VectorRendererSpec<PixelType, PixelFormat> { + typedef VectorRendererSpec<PixelType, PixelFormat> Base; +protected: + /** + * "Wu's Line Antialiasing Algorithm" as published by Xiaolin Wu, July 1991 + * Based on the implementation found in Michael Abrash's Graphics Programming Black Book. + * + * Generic line drawing algorithm for the Antialiased renderer. Optimized with no + * floating point operations, assumes no special cases. + * + * @see VectorRenderer::drawLineAlg() + */ + virtual void drawLineAlg(int x1, int y1, int x2, int y2, int dx, int dy, PixelType color); + + /** + * "Wu's Circle Antialiasing Algorithm" as published by Xiaolin Wu, July 1991 + * Based on the theoretical concept of the algorithm. + * + * Implementation of Wu's algorithm for circles using fixed point arithmetics. + * Could be quite fast. + * + * @see VectorRenderer::drawCircleAlg() + */ + virtual void drawCircleAlg(int x, int y, int r, PixelType color, VectorRenderer::FillMode fill_m); + + /** + * "Wu's Circle Antialiasing Algorithm" as published by Xiaolin Wu, July 1991, + * modified with corner displacement to allow drawing of squares with rounded + * corners. + * + * @see VectorRenderer::drawRoundedAlg() + */ + virtual void drawRoundedSquareAlg(int x1, int y1, int r, int w, int h, PixelType color, VectorRenderer::FillMode fill_m); + + virtual void drawRoundedSquareShadow(int x, int y, int r, int w, int h, int blur) { + Base::drawRoundedSquareShadow(x, y, r, w, h, blur); +// VectorRenderer::applyConvolutionMatrix(VectorRenderer::kConvolutionHardBlur, +// Common::Rect(x, y, x + w + blur * 2, y + h + blur * 2)); + } +}; +#endif + +} +#endif diff --git a/graphics/colormasks.h b/graphics/colormasks.h index 5b9f0517a9..af72dbdf8c 100644 --- a/graphics/colormasks.h +++ b/graphics/colormasks.h @@ -26,6 +26,8 @@ #ifndef GRAPHICS_COLORMASKS_H #define GRAPHICS_COLORMASKS_H +namespace Graphics { + template<int bitFormat> struct ColorMasks { }; @@ -251,4 +253,75 @@ void colorToARGB(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b) { b = ((color & T::kBlueMask) >> T::kBlueShift) << (8 - T::kBlueBits); } +/** + * A pixel format description. + * + * Like ColorMasks it includes the given values to create colors from RGB + * values and to retrieve RGB values from colors. + * + * Unlike ColorMasks it is not dependend on knowing the exact pixel format + * on compile time. + * + * A minor difference between ColorMasks and PixelFormat is that ColorMasks + * stores the bit count per channel in 'kFooBits', while PixelFormat stores + * the loss compared to 8 bits per channel in '#Loss'. It also doesn't + * contain mask values. + */ +struct PixelFormat { + byte bytesPerPixel; /**< Number of bytes used in the pixel format. */ + + byte rLoss, gLoss, bLoss, aLoss; /**< Precision loss of each color component. */ + byte rShift, gShift, bShift, aShift; /**< Binary left shift of each color component in the pixel value. */ +}; + +template<int bitFormat> +PixelFormat createPixelFormat() { + PixelFormat format; + + format.bytesPerPixel = ColorMasks<bitFormat>::kBytesPerPixel; + + format.rLoss = 8 - ColorMasks<bitFormat>::kRedBits; + format.gLoss = 8 - ColorMasks<bitFormat>::kGreenBits; + format.bLoss = 8 - ColorMasks<bitFormat>::kBlueBits; + format.aLoss = 8 - ColorMasks<bitFormat>::kAlphaBits; + + format.rShift = ColorMasks<bitFormat>::kRedShift; + format.gShift = ColorMasks<bitFormat>::kGreenShift; + format.bShift = ColorMasks<bitFormat>::kBlueShift; + format.aShift = ColorMasks<bitFormat>::kAlphaShift; + + return format; +} + +inline uint32 RGBToColor(uint8 r, uint8 g, uint8 b, const PixelFormat &fmt) { + return + ((0xFF >> fmt.aLoss) << fmt.aShift) | + (( r >> fmt.rLoss) << fmt.rShift) | + (( g >> fmt.gLoss) << fmt.gShift) | + (( b >> fmt.bLoss) << fmt.bShift); +} + +inline uint32 ARGBToColor(uint8 a, uint8 r, uint8 g, uint8 b, const PixelFormat &fmt) { + return + ((a >> fmt.aLoss) << fmt.aShift) | + ((r >> fmt.rLoss) << fmt.rShift) | + ((g >> fmt.gLoss) << fmt.gShift) | + ((b >> fmt.bLoss) << fmt.bShift); +} + +inline void colorToRGB(uint32 color, uint8 &r, uint8 &g, uint8 &b, const PixelFormat &fmt) { + r = ((color >> fmt.rShift) << fmt.rLoss) & 0xFF; + g = ((color >> fmt.gShift) << fmt.gLoss) & 0xFF; + b = ((color >> fmt.bShift) << fmt.bLoss) & 0xFF; +} + +inline void colorToARGB(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b, const PixelFormat &fmt) { + a = ((color >> fmt.aShift) << fmt.aLoss) & 0xFF; + r = ((color >> fmt.rShift) << fmt.rLoss) & 0xFF; + g = ((color >> fmt.gShift) << fmt.gLoss) & 0xFF; + b = ((color >> fmt.bShift) << fmt.bLoss) & 0xFF; +} + +} // end of namespace Graphics + #endif diff --git a/graphics/cursorman.h b/graphics/cursorman.h index bf05ab762b..bc38466eda 100644 --- a/graphics/cursorman.h +++ b/graphics/cursorman.h @@ -81,7 +81,7 @@ public: /** * Pop all of the cursors and cursor palettes from their respective stacks. - * The purpose is to ensure that all unecessary cursors are removed from the + * The purpose is to ensure that all unecessary cursors are removed from the * stack when returning to the launcher from an engine. * */ diff --git a/graphics/dither.cpp b/graphics/dither.cpp new file mode 100644 index 0000000000..7a92441571 --- /dev/null +++ b/graphics/dither.cpp @@ -0,0 +1,314 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#include "common/endian.h" +#include "graphics/dither.h" + +namespace Graphics { + +PaletteLUT::PaletteLUT(byte depth, PaletteFormat format) { + assert((depth > 1) && (depth < 9)); + + // For adjusting depth + _depth1 = depth; + _depth2 = 2 * _depth1; + _shift = 8 - _depth1; + + // The table's dimensions + _dim1 = (1 << _depth1); + _dim2 = _dim1 * _dim1; + _dim3 = _dim1 * _dim1 * _dim1; + + _format = format; + + // What's already built + _got = _dim1; + _gots = new byte[_dim1]; + + // The lookup table + _lut = new byte[_dim3]; + + memset(_lutPal, 0, 768); + memset(_realPal, 0, 768); + memset(_gots, 1, _dim1); +} + +void PaletteLUT::setPalette(const byte *palette, PaletteFormat format, + byte depth, int transp) { + + assert((depth > 1) && (depth < 9)); + + _transp = transp; + + int shift = 8 - depth; + + // Checking for the table's and the palette's pixel format + if ((_format == kPaletteRGB) && (format == kPaletteYUV)) { + byte *newPal = _realPal; + const byte *oldPal = palette; + for (int i = 0; i < 256; i++, newPal += 3, oldPal += 3) + YUV2RGB(oldPal[0] << shift, oldPal[1] << shift, oldPal[2] << shift, + newPal[0], newPal[1], newPal[2]); + } else if ((_format == kPaletteYUV) && (format == kPaletteRGB)) { + byte *newPal = _realPal; + const byte *oldPal = palette; + for (int i = 0; i < 256; i++, newPal += 3, oldPal += 3) + RGB2YUV(oldPal[0] << shift, oldPal[1] << shift, oldPal[2] << shift, + newPal[0], newPal[1], newPal[2]); + } else + memcpy(_realPal, palette, 768); + + // Using the specified depth for the lookup + byte *newPal = _lutPal, *oldPal = _realPal; + for (int i = 0; i < 768; i++) + *newPal++ = (*oldPal++) >> _shift; + + // Everything has to be rebuilt + _got = 0; + memset(_gots, 0, _dim1); +} + +PaletteLUT::~PaletteLUT() { + delete[] _lut; + delete[] _gots; +} + +void PaletteLUT::buildNext() { + if (_got >= _dim1) + return; + + build(_got++); +} + +#define SQR(x) ((x) * (x)) +// Building one "slice" +void PaletteLUT::build(int d1) { + // First dimension + byte *lut = _lut + d1 * _dim2; + + // Second dimension + for (uint32 j = 0; j < _dim1; j++) { + // Third dimension + for (uint32 k = 0; k < _dim1; k++) { + const byte *p = _lutPal; + uint32 d = 0xFFFFFFFF; + byte n = 0; + + // Going over every palette entry, searching for the closest + for (int c = 0; c < 256; c++, p += 3) { + // Ignore the transparent color + if (c == _transp) + continue; + + uint32 di = SQR(d1 - p[0]) + SQR(j - p[1]) + SQR(k - p[2]); + if (di < d) { + d = di; + n = c; + if (d == 0) + break; + } + } + + *lut++ = n; + } + } + + // Got this slice now + _gots[d1] = 1; +} + +inline int PaletteLUT::getIndex(byte c1, byte c2, byte c3) const { + return ((c1 >> _shift) << _depth2) | ((c2 >> _shift) << _depth1) | (c3 >> _shift); +} + +void PaletteLUT::getEntry(byte index, byte &c1, byte &c2, byte &c3) const { + c1 = _realPal[index * 3 + 0]; + c2 = _realPal[index * 3 + 1]; + c3 = _realPal[index * 3 + 2]; +} + +byte PaletteLUT::findNearest(byte c1, byte c2, byte c3) { + return _lut[getIndex(c1, c2, c3)]; +} + +byte PaletteLUT::findNearest(byte c1, byte c2, byte c3, byte &nC1, byte &nC2, byte &nC3) { + // If we don't have the required "slice" yet, build it + if (!_gots[c1 >> _shift]) + build(c1 >> _shift); + + int palIndex = _lut[getIndex(c1, c2, c3)]; + int i = palIndex * 3; + + nC1 = _realPal[i + 0]; + nC2 = _realPal[i + 1]; + nC3 = _realPal[i + 2]; + + return palIndex; +} + +bool PaletteLUT::save(Common::WriteStream &stream) { + // The table has to be completely built before we can save + while (_got < _dim1) + buildNext(); + + stream.writeUint32BE(MKID_BE('PLUT')); // Magic + stream.writeUint32BE(kVersion); + stream.writeByte(_depth1); + if (stream.write(_realPal, 768) != 768) + return false; + if (stream.write(_lutPal, 768) != 768) + return false; + if (stream.write(_lut, _dim3) != _dim3) + return false; + if (!stream.flush()) + return false; + + if (stream.err()) + return false; + + return true; +} + +bool PaletteLUT::load(Common::SeekableReadStream &stream) { + // _realPal + _lutPal + _lut + _depth1 + magic + version + int32 needSize = 768 + 768 + _dim3 + 1 + 4 + 4; + + if ((stream.size() - stream.pos()) < needSize) + return false; + + // Magic + if (stream.readUint32BE() != MKID_BE('PLUT')) + return false; + + if (stream.readUint32BE() != kVersion) + return false; + + byte depth1 = stream.readByte(); + + if (depth1 != _depth1) + return false; + + if (stream.read(_realPal, 768) != 768) + return false; + if (stream.read(_lutPal, 768) != 768) + return false; + if (stream.read(_lut, _dim3) != _dim3) + return false; + + _got = _dim1; + memset(_gots, 1, _dim1); + + return true; +} + +SierraLight::SierraLight(int16 width, PaletteLUT *palLUT) { + assert(width > 0); + + _width = width; + _palLUT = palLUT; + + // Big buffer for the errors of the current and next line + _errorBuf = new int32[3 * (2 * (_width + 2*1))]; + memset(_errorBuf, 0, (3 * (2 * (_width + 2*1))) * sizeof(int32)); + + _curLine = 0; + _errors[0] = _errorBuf + 3; + _errors[1] = _errors[0] + 3 * (_width + 2*1); +} + +SierraLight::~SierraLight() { + delete[] _errorBuf; +} + +void SierraLight::newFrame() { + _curLine = 0; + memset(_errors[0], 0, 3 * _width * sizeof(int32)); + memset(_errors[1], 0, 3 * _width * sizeof(int32)); +} + +void SierraLight::nextLine() { + // Clear the finished line, it will become the last line in the buffer + memset(_errors[_curLine], 0, 3 * _width * sizeof(int32)); + + _curLine = (_curLine + 1) % 2; +} + +byte SierraLight::dither(byte c1, byte c2, byte c3, uint32 x) { + assert(_palLUT); + assert(x < (uint32)_width); + + int32 eC1, eC2, eC3; + + getErrors(x, eC1, eC2, eC3); + + // Apply error on values + c1 = CLIP<int>(c1 + eC1, 0, 255); + c2 = CLIP<int>(c2 + eC2, 0, 255); + c3 = CLIP<int>(c3 + eC3, 0, 255); + + // Find color + byte newC1, newC2, newC3; + byte newPixel = _palLUT->findNearest(c1, c2, c3, newC1, newC2, newC3); + + // Calculate new error + eC1 = c1 - newC1; + eC2 = c2 - newC2; + eC3 = c3 - newC3; + + // Add them + addErrors(x, eC1, eC2, eC3); + + return newPixel; +} + +inline void SierraLight::getErrors(uint32 x, int32 &eC1, int32 &eC2, int32 &eC3) { + int32 *errCur = _errors[_curLine]; + + x *= 3; + eC1 = errCur[x + 0] >> 2; + eC2 = errCur[x + 1] >> 2; + eC3 = errCur[x + 2] >> 2; +} + +inline void SierraLight::addErrors(uint32 x, int32 eC1, int32 eC2, int32 eC3) { + int32 *errCur = _errors[_curLine]; + int32 *errNext = _errors[(_curLine + 1) % 2]; + + // Indices for current error + int x0 = 3 * (x + 1); + int x1 = 3 * (x + 0); + int x2 = 3 * (x - 1); + + errCur [x0 + 0] += eC1 << 1; + errCur [x0 + 1] += eC2 << 1; + errCur [x0 + 2] += eC3 << 1; + errNext[x1 + 0] += eC1; + errNext[x1 + 1] += eC2; + errNext[x1 + 2] += eC3; + errNext[x2 + 0] += eC1; + errNext[x2 + 1] += eC2; + errNext[x2 + 2] += eC3; +} + +} // End of namespace Graphics diff --git a/graphics/dither.h b/graphics/dither.h new file mode 100644 index 0000000000..e6d606cdd4 --- /dev/null +++ b/graphics/dither.h @@ -0,0 +1,194 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#ifndef GRAPHICS_DITHER_H +#define GRAPHICS_DITHER_H + +#include "common/util.h" +#include "common/stream.h" + +namespace Graphics { + +/** A palette lookup table to find the nearest matching entry of a fixed palette to a true color. + * + * The table can be build up in slices, one slice consisting of all entries for + * one value of the first color component. + */ +class PaletteLUT { +public: + /** Palette format. */ + enum PaletteFormat { + kPaletteRGB, //!< Palette in RGB colorspace + kPaletteYUV //!< Palette in YUV colorspace + }; + + /** Converting a color from YUV to RGB colorspace. */ + inline static void YUV2RGB(byte y, byte u, byte v, byte &r, byte &g, byte &b) { + r = CLIP<int>(y + ((1357 * (v - 128)) >> 10), 0, 255); + g = CLIP<int>(y - (( 691 * (v - 128)) >> 10) - ((333 * (u - 128)) >> 10), 0, 255); + b = CLIP<int>(y + ((1715 * (u - 128)) >> 10), 0, 255); + } + /** Converting a color from RGB to YUV colorspace. */ + inline static void RGB2YUV(byte r, byte g, byte b, byte &y, byte &u, byte &v) { + y = CLIP<int>( ((r * 306) >> 10) + ((g * 601) >> 10) + ((b * 117) >> 10) , 0, 255); + u = CLIP<int>(-((r * 172) >> 10) - ((g * 340) >> 10) + ((b * 512) >> 10) + 128, 0, 255); + v = CLIP<int>( ((r * 512) >> 10) - ((g * 429) >> 10) - ((b * 83) >> 10) + 128, 0, 255); + } + + /** Create a lookup table of a given depth and palette format. + * + * @param depth How many bits of each color component to consider. + * @param format The format the palette should be in. + */ + PaletteLUT(byte depth, PaletteFormat format); + ~PaletteLUT(); + + /** Setting a palette. + * + * Any already built slices will be purged. + * + * @param palette The palette, plain 256 * 3 color components. + * @param format The format the palette is in. + * @param depth The number of significant bits in each color component. + * @param transp An index that's seen as transparent and therefore ignored. + */ + void setPalette(const byte *palette, PaletteFormat format, byte depth, int transp = -1); + + /** Build the next slice. + * + * This will build the next slice, if any. + */ + void buildNext(); + + /** Querying the color components to a given palette entry index. */ + void getEntry(byte index, byte &c1, byte &c2, byte &c3) const; + /** Finding the nearest matching entry. + * + * @param c1 The first component of the wanted color. + * @param c2 The second component of the wanted color. + * @param c3 The third component of the wanted color. + * @return The palette entry matching the wanted color best. + */ + byte findNearest(byte c1, byte c2, byte c3); + /** Finding the nearest matching entry, together with its color components. + * + * @param c1 The first component of the wanted color. + * @param c2 The second component of the wanted color. + * @param c3 The third component of the wanted color. + * @paran nC1 The first component of the found color. + * @paran nC2 The second component of the found color. + * @paran nC3 The third component of the found color. + * @return The palette entry matching the wanted color best. + */ + byte findNearest(byte c1, byte c2, byte c3, byte &nC1, byte &nC2, byte &nC3); + + /** Save the table to a stream. + * + * This will build the whole table first. + */ + bool save(Common::WriteStream &stream); + /** Load the table from a stream. */ + bool load(Common::SeekableReadStream &stream); + +private: + static const uint32 kVersion = 1; + + byte _depth1; //!< The table's depth for one dimension. + byte _depth2; //!< The table's depth for two dimensions. + byte _shift; //!< Amount to shift to adjust for the table's depth. + + uint32 _dim1; //!< The table's entry offset for one dimension. + uint32 _dim2; //!< The table's entry offset for two dimensions. + uint32 _dim3; //!< The table's entry offset for three dimensions. + + int _transp; //!< The transparent palette index. + + PaletteFormat _format; //!< The table's palette format. + byte _lutPal[768]; //!< The palette used for looking up a color. + byte _realPal[768]; //!< The original palette. + + uint32 _got; //!< Number of slices generated. + byte *_gots; //!< Map of generated slices. + byte *_lut; //!< The lookup table. + + /** Building a specified slice. */ + void build(int d1); + /** Calculates the index into the lookup table for a given color. */ + inline int getIndex(byte c1, byte c2, byte c3) const; +}; + +/** The Sierra-2-4A ("Filter Light") error distribution dithering algorithm. + * + * The image will be dithered line by line and pixel by pixel, without earlier + * values having to be changed. +*/ +class SierraLight { +public: + /** Constructor. + * + * @param width The width of the image to dither. + * @param palLUT The palette to which to dither. + */ + SierraLight(int16 width, PaletteLUT *palLUT); + ~SierraLight(); + + /** Signals that a new frame or image is about to be dithered. + * + * This clears all collected errors, so that a new image (of the same + * height and with the same palette) can be dithered. + */ + void newFrame(); + /** Signals that a new line is about the begin. + * + * The current line's errors will be forgotten and values collected for the + * next line will now count as the current line's. + */ + void nextLine(); + /** Dither a pixel. + * + * @param c1 The first color component of the pixel. + * @param c2 The second color component of the pixel. + * @param c3 The third color component of the pixel. + * @param x The pixel's x coordinate within the image. + */ + byte dither(byte c1, byte c2, byte c3, uint32 x); + +protected: + int16 _width; //!< The image's width. + + PaletteLUT *_palLUT; //!< The palette against which to dither. + + int32 *_errorBuf; //!< Big buffer for all collected errors. + int32 *_errors[2]; //!< Pointers into the error buffer for two lines. + int _curLine; //!< Which one is the current line? + + /** Querying a pixel's errors. */ + inline void getErrors(uint32 x, int32 &eC1, int32 &eC2, int32 &eC3); + /** Adding a pixel's errors. */ + inline void addErrors(uint32 x, int32 eC1, int32 eC2, int32 eC3); +}; + +} // End of namespace Graphics + +#endif diff --git a/graphics/font.cpp b/graphics/font.cpp index 0f67899706..f896b3c94e 100644 --- a/graphics/font.cpp +++ b/graphics/font.cpp @@ -48,10 +48,32 @@ int NewFont::getCharWidth(byte chr) const { return desc.width[chr - desc.firstchar]; } -void NewFont::drawChar(Surface *dst, byte chr, int tx, int ty, uint32 color) const { + +template <typename PixelType> +void drawCharIntern(byte *ptr, uint pitch, const bitmap_t *src, int h, int minX, int maxX, const PixelType color) { + const bitmap_t maxXMask = ~((1 << (16-maxX)) - 1); + while (h-- > 0) { + bitmap_t buffer = READ_UINT16(src); + src++; + + buffer &= maxXMask; + buffer <<= minX; + PixelType *tmp = (PixelType *)ptr; + while (buffer != 0) { + if ((buffer & 0x8000) != 0) + *tmp = color; + tmp++; + buffer <<= 1; + } + + ptr += pitch; + } +} + +void NewFont::drawChar(Surface *dst, byte chr, const int tx, const int ty, const uint32 color) const { assert(dst != 0); - assert(desc.bits != 0 && desc.maxwidth <= 17); + assert(desc.bits != 0 && desc.maxwidth <= 16); assert(dst->bytesPerPixel == 1 || dst->bytesPerPixel == 2); // If this character is not included in the font, use the default char. @@ -80,25 +102,14 @@ void NewFont::drawChar(Surface *dst, byte chr, int tx, int ty, uint32 color) con const bitmap_t *tmp = desc.bits + (desc.offset ? desc.offset[chr] : (chr * desc.fbbh)); - for (int y = 0; y < bbh; y++, ptr += dst->pitch) { - const bitmap_t buffer = READ_UINT16(tmp); - tmp++; - bitmap_t mask = 0x8000; - if (ty + desc.ascent - bby - bbh + y < 0 || - ty + desc.ascent - bby - bbh + y >= dst->h) - continue; + int y = MIN(bbh, ty + desc.ascent - bby); + tmp += bbh - y; + y -= MAX(0, ty + desc.ascent - bby - dst->h); - for (int x = 0; x < bbw; x++, mask >>= 1) { - if (tx + bbx + x < 0 || tx + bbx + x >= dst->w) - continue; - if ((buffer & mask) != 0) { - if (dst->bytesPerPixel == 1) - ptr[x] = color; - else if (dst->bytesPerPixel == 2) - ((uint16 *)ptr)[x] = color; - } - } - } + if (dst->bytesPerPixel == 1) + drawCharIntern<byte>(ptr, dst->pitch, tmp, y, MAX(0, -(tx + bbx)), MIN(bbw, dst->w - tx - bbx), color); + else if (dst->bytesPerPixel == 2) + drawCharIntern<uint16>(ptr, dst->pitch, tmp, y, MAX(0, -(tx + bbx)), MIN(bbw, dst->w - tx - bbx), color); } @@ -761,7 +772,7 @@ int Font::getStringWidth(const Common::String &str) const { return space; } -void Font::drawString(Surface *dst, const Common::String &s, int x, int y, int w, uint32 color, TextAlignment align, int deltax, bool useEllipsis) const { +void Font::drawString(Surface *dst, const Common::String &s, int x, int y, int w, uint32 color, TextAlign align, int deltax, bool useEllipsis) const { assert(dst != 0); const int leftX = x, rightX = x + w; uint i; @@ -816,7 +827,7 @@ void Font::drawString(Surface *dst, const Common::String &s, int x, int y, int w } if (align == kTextAlignCenter) - x = x + (w - width - 1)/2; + x = x + (w - width)/2; else if (align == kTextAlignRight) x = x + w - width; x += deltax; diff --git a/graphics/font.h b/graphics/font.h index 3911e6d32c..f68a6dd7a6 100644 --- a/graphics/font.h +++ b/graphics/font.h @@ -34,11 +34,11 @@ class SeekableReadStream; namespace Graphics { -// Text alignment modes for drawString() -enum TextAlignment { - kTextAlignLeft, - kTextAlignCenter, - kTextAlignRight +/** Text alignment modes */ +enum TextAlign { + kTextAlignLeft, //!< Text should be aligned to the left + kTextAlignCenter, //!< Text should be centered + kTextAlignRight //!< Text should be aligned to the right }; /** @@ -57,7 +57,7 @@ public: virtual int getCharWidth(byte chr) const = 0; virtual void drawChar(Surface *dst, byte chr, int x, int y, uint32 color) const = 0; - void drawString(Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlignment align = kTextAlignLeft, int deltax = 0, bool useEllipsis = true) const; + void drawString(Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlign align = kTextAlignLeft, int deltax = 0, bool useEllipsis = true) const; /** * Compute and return the width the string str has when rendered using this font. @@ -90,7 +90,7 @@ public: virtual void drawChar(Surface *dst, byte chr, int x, int y, uint32 color) const; }; -typedef unsigned short bitmap_t; /* bitmap image unit size*/ +typedef uint16 bitmap_t; /* bitmap image unit size*/ struct BBX { int8 w; diff --git a/graphics/imagedec.cpp b/graphics/imagedec.cpp index e84ca8ca0e..93a895ae03 100644 --- a/graphics/imagedec.cpp +++ b/graphics/imagedec.cpp @@ -120,13 +120,14 @@ Surface *BMPDecoder::decodeImage(Common::SeekableReadStream &stream) { newSurf->create(info.width, info.height, sizeof(OverlayColor)); assert(newSurf->pixels); OverlayColor *curPixel = (OverlayColor*)newSurf->pixels + (newSurf->h-1) * newSurf->w; + PixelFormat overlayFormat = g_system->getOverlayFormat(); int pitchAdd = info.width % 4; for (int i = 0; i < newSurf->h; ++i) { for (int i2 = 0; i2 < newSurf->w; ++i2) { b = stream.readByte(); g = stream.readByte(); r = stream.readByte(); - *curPixel = g_system->RGBToColor(r, g, b); + *curPixel = RGBToColor(r, g, b, overlayFormat); ++curPixel; } stream.seek(pitchAdd, SEEK_CUR); diff --git a/graphics/imageman.cpp b/graphics/imageman.cpp deleted file mode 100644 index 44ac7c9e75..0000000000 --- a/graphics/imageman.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * $URL$ - * $Id$ - */ - -#include "graphics/imagedec.h" -#include "graphics/imageman.h" -#include "graphics/surface.h" - -#include "common/unzip.h" - -DECLARE_SINGLETON(Graphics::ImageManager); - -namespace Graphics { - -ImageManager::ImageManager() { -} - -ImageManager::~ImageManager() { - for (Iterator pos = _surfaces.begin(); pos != _surfaces.end(); ++pos) { - (*pos)->surface->free(); - delete (*pos)->surface; - delete *pos; - *pos = 0; - } - _surfaces.clear(); -} - -bool ImageManager::addArchive(const Common::String &name) { -#ifdef USE_ZLIB - Common::ZipArchive *arch = new Common::ZipArchive(name); - if (!arch || !arch->isOpen()) - return false; - _archives.add(name, Common::ArchivePtr(arch)); -#endif - return true; -} - -void ImageManager::removeArchive(const Common::String &name) { -#ifdef USE_ZLIB - _archives.remove(name); -#endif -} - -bool ImageManager::registerSurface(const Common::String &name, Surface *surf) { - if (getSurface(name)) { - return false; - } - - Entry *newHandle = new Entry; - if (!newHandle) - return false; - - if (!surf) - surf = ImageDecoder::loadFile(name); - -#ifdef USE_ZLIB - if (!surf) { - Common::SeekableReadStream *stream = _archives.openFile(name); - if (stream) { - surf = ImageDecoder::loadFile(*stream); - delete stream; - } - } -#endif - - if (!surf) - return false; - - newHandle->surface = surf; - newHandle->name = name; - _surfaces.push_back(newHandle); - - return true; -} - -bool ImageManager::unregisterSurface(const Common::String &name) { - Iterator pos = searchHandle(name); - if (pos == _surfaces.end()) { - // no surface handle it as success - return true; - } - - (*pos)->surface->free(); - delete (*pos)->surface; - delete *pos; - *pos = 0; - - _surfaces.erase(pos); - - return true; -} - -Surface * ImageManager::getSurface(const Common::String &name) { - Iterator pos = searchHandle(name); - if (pos == _surfaces.end()) { - // no surface handle it as success - return 0; - } - return (*pos)->surface; -} - -ImageManager::Iterator ImageManager::searchHandle(const Common::String &name) { - Iterator pos = _surfaces.begin(); - while (pos != _surfaces.end()) { - if ((*pos)->name == name) - break; - ++pos; - } - return pos; -} -} // end of namespace Graphics diff --git a/graphics/imageman.h b/graphics/imageman.h deleted file mode 100644 index f555b4c844..0000000000 --- a/graphics/imageman.h +++ /dev/null @@ -1,111 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * $URL$ - * $Id$ - */ - -#ifndef GRAPHICS_IMAGEMAN_H -#define GRAPHICS_IMAGEMAN_H - -#include "common/scummsys.h" - -#include "common/archive.h" -#include "common/singleton.h" -#include "common/str.h" -#include "common/list.h" - -namespace Graphics { - -struct Surface; - -class ImageManager : public Common::Singleton<ImageManager> { -public: - ~ImageManager(); - - /** - * adds an .zip archive to the pool where the ImageManager searches - * for image files - * - * @param name the name of the archive - * @return true on success and false on failure - */ - bool addArchive(const Common::String &name); - - /** - * deletes an .zip archive from the pool where the Image Manager searches - * for image files - * - * @param name the name of the archive - */ - void removeArchive(const Common::String &name); - - /** - * registers a surface to the ImageManager. - * surf->free(), also delete surf, will be called when the ImageManager will - * be destroyed or if ImageManager::unregisterSurface is called. - * if the parameter 'surf' is 0 the Manger will try to load an image with - * the filename 'name' - * - * @param name the name of the new handle - * @param surf the surface which should be associated to the given name - * @return returns true on success and false on failure - */ - bool registerSurface(const Common::String &name, Surface *surf); - - /** - * unregisters a surface, after this the returned surface from - * getSurface should NOT be used anymore - * - * @param name the handle - * @return true on success, false on failure - */ - bool unregisterSurface(const Common::String &name); - - /** - * gets a surface registered to a handle - * - * @param name the name of the handle - * @return returns an pointer to an Surface object or 0 on failure - */ - Surface *getSurface(const Common::String &name); -private: - friend class Common::Singleton<SingletonBaseType>; - ImageManager(); - - struct Entry { - Common::String name; - Surface *surface; - }; - typedef Common::List<Entry*>::iterator Iterator; - - Iterator searchHandle(const Common::String &name); - - Common::List<Entry*> _surfaces; - Common::SearchSet _archives; -}; - -} // end of namespace Graphics - -/** Shortcut for accessing the image manager. */ -#define ImageMan (Graphics::ImageManager::instance()) - -#endif - diff --git a/graphics/module.mk b/graphics/module.mk index 6a5f7f9e22..75aea2b7da 100644 --- a/graphics/module.mk +++ b/graphics/module.mk @@ -2,7 +2,7 @@ MODULE := graphics MODULE_OBJS := \ cursorman.o \ - dxa_player.o \ + dither.o \ font.o \ fontman.o \ fonts/consolefont.o \ @@ -11,14 +11,18 @@ MODULE_OBJS := \ fonts/scummfont.o \ iff.o \ imagedec.o \ - imageman.o \ - mpeg_player.o \ primitives.o \ scaler.o \ scaler/thumbnail_intern.o \ surface.o \ thumbnail.o \ - surface-keycolored.o + VectorRenderer.o \ + VectorRendererSpec.o \ + video/dxa_player.o \ + video/flic_player.o \ + video/mpeg_player.o \ + video/smk_player.o \ + video/video_player.o ifndef DISABLE_SCALERS MODULE_OBJS += \ diff --git a/graphics/scaler.cpp b/graphics/scaler.cpp index ec20a722e6..e1f3775eac 100644 --- a/graphics/scaler.cpp +++ b/graphics/scaler.cpp @@ -105,9 +105,9 @@ void InitScalers(uint32 BitFormat) { gBitFormat = BitFormat; #ifndef DISABLE_HQ_SCALERS if (gBitFormat == 555) - InitLUT<ColorMasks<555> >(); + InitLUT<Graphics::ColorMasks<555> >(); if (gBitFormat == 565) - InitLUT<ColorMasks<565> >(); + InitLUT<Graphics::ColorMasks<565> >(); #endif } @@ -127,6 +127,11 @@ void DestroyScalers(){ */ void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) { + // Spot the case when it can all be done in 1 hit + if (((int)srcPitch == 2 * width) && ((int)dstPitch == 2 * width)) { + width *= height; + height = 1; + } while (height--) { memcpy(dstPtr, srcPtr, 2 * width); srcPtr += srcPitch; diff --git a/graphics/scaler/intern.h b/graphics/scaler/intern.h index ff515530dd..6fc0d6919b 100644 --- a/graphics/scaler/intern.h +++ b/graphics/scaler/intern.h @@ -30,12 +30,12 @@ #include "graphics/colormasks.h" -#define highBits ColorMasks<bitFormat>::highBits -#define lowBits ColorMasks<bitFormat>::lowBits -#define qhighBits ColorMasks<bitFormat>::qhighBits -#define qlowBits ColorMasks<bitFormat>::qlowBits -#define redblueMask ColorMasks<bitFormat>::kRedBlueMask -#define greenMask ColorMasks<bitFormat>::kGreenMask +#define highBits Graphics::ColorMasks<bitFormat>::highBits +#define lowBits Graphics::ColorMasks<bitFormat>::lowBits +#define qhighBits Graphics::ColorMasks<bitFormat>::qhighBits +#define qlowBits Graphics::ColorMasks<bitFormat>::qlowBits +#define redblueMask Graphics::ColorMasks<bitFormat>::kRedBlueMask +#define greenMask Graphics::ColorMasks<bitFormat>::kGreenMask /** diff --git a/graphics/scaler/scalebit.cpp b/graphics/scaler/scalebit.cpp index 2210697d71..a6fd8df58f 100644 --- a/graphics/scaler/scalebit.cpp +++ b/graphics/scaler/scalebit.cpp @@ -131,7 +131,7 @@ static void scale2x(void* void_dst, unsigned dst_slice, const void* void_src, un } /** - * Apply the Scale32x effect on a bitmap. + * Apply the Scale3x effect on a bitmap. * The destination bitmap is filled with the scaled version of the source bitmap. * The source bitmap isn't modified. * The destination bitmap must be manually allocated before calling the function, diff --git a/graphics/scaler/thumbnail_intern.cpp b/graphics/scaler/thumbnail_intern.cpp index bdfa0ff5f6..52547f47f1 100644 --- a/graphics/scaler/thumbnail_intern.cpp +++ b/graphics/scaler/thumbnail_intern.cpp @@ -118,7 +118,7 @@ static bool grabScreen565(Graphics::Surface *surf) { g = palette[((uint8*)screen->pixels)[y * screen->pitch + x] * 4 + 1]; b = palette[((uint8*)screen->pixels)[y * screen->pitch + x] * 4 + 2]; - ((uint16*)surf->pixels)[y * surf->w + x] = RGBToColor<ColorMasks<565> >(r, g, b); + ((uint16*)surf->pixels)[y * surf->w + x] = Graphics::RGBToColor<Graphics::ColorMasks<565> >(r, g, b); } } @@ -209,7 +209,7 @@ bool createThumbnail(Graphics::Surface *surf, const uint8 *pixels, int w, int h, g = palette[pixels[y * w + x] * 3 + 1]; b = palette[pixels[y * w + x] * 3 + 2]; - ((uint16 *)screen.pixels)[y * screen.w + x] = RGBToColor<ColorMasks<565> >(r, g, b); + ((uint16 *)screen.pixels)[y * screen.w + x] = Graphics::RGBToColor<Graphics::ColorMasks<565> >(r, g, b); } } diff --git a/graphics/surface-keycolored.cpp b/graphics/surface-keycolored.cpp index 86873a3944..64ddf63395 100644 --- a/graphics/surface-keycolored.cpp +++ b/graphics/surface-keycolored.cpp @@ -1,3 +1,26 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ #include "graphics/surface-keycolored.h" namespace Graphics { diff --git a/graphics/surface-keycolored.h b/graphics/surface-keycolored.h index 43d5413275..8877852328 100644 --- a/graphics/surface-keycolored.h +++ b/graphics/surface-keycolored.h @@ -1,3 +1,26 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ #ifndef GRAPHICS_SURFACE_KEYCOLORED_H #define GRAPHICS_SURFACE_KEYCOLORED_H diff --git a/graphics/surface.h b/graphics/surface.h index 747bda9a26..20ab816236 100644 --- a/graphics/surface.h +++ b/graphics/surface.h @@ -35,10 +35,10 @@ namespace Graphics { * operations, font rendering, etc. */ struct Surface { - /** - * ARM code relies on the layout of the first 3 of these fields. Do - * not change them. - */ + /** + * ARM code relies on the layout of the first 3 of these fields. Do + * not change them. + */ uint16 w; uint16 h; uint16 pitch; @@ -47,12 +47,11 @@ struct Surface { Surface() : w(0), h(0), pitch(0), pixels(0), bytesPerPixel(0) {} inline const void *getBasePtr(int x, int y) const { - // SumthinWicked says: I was getting a typecast error here from GCC/UIQ: might need an #ifdef __SYMBIAN32__ - return static_cast<const void *>(static_cast<byte *>(pixels) + y * pitch + x * bytesPerPixel); + return (const byte *)(pixels) + y * pitch + x * bytesPerPixel; } inline void *getBasePtr(int x, int y) { - return static_cast<void *>(static_cast<byte *>(pixels) + y * pitch + x * bytesPerPixel); + return static_cast<byte *>(pixels) + y * pitch + x * bytesPerPixel; } /** diff --git a/graphics/thumbnail.cpp b/graphics/thumbnail.cpp index 905fea3d93..1d88dc5147 100644 --- a/graphics/thumbnail.cpp +++ b/graphics/thumbnail.cpp @@ -42,12 +42,6 @@ struct ThumbnailHeader { #define ThumbnailHeaderSize (4+4+1+2+2+1) -inline void colorToRGB(uint16 color, uint8 &r, uint8 &g, uint8 &b) { - r = (((color >> 11) & 0x1F) << 3); - g = (((color >> 5) & 0x3F) << 2); - b = ((color&0x1F) << 3); -} - bool loadHeader(Common::SeekableReadStream &in, ThumbnailHeader &header, bool outputWarnings) { header.type = in.readUint32BE(); // We also accept the bad 'BMHT' header here, for the sake of compatibility @@ -81,7 +75,7 @@ bool checkThumbnailHeader(Common::SeekableReadStream &in) { ThumbnailHeader header; bool hasHeader = loadHeader(in, header, false); - + in.seek(position, SEEK_SET); return hasHeader; @@ -114,13 +108,14 @@ bool loadThumbnail(Common::SeekableReadStream &in, Graphics::Surface &to) { to.create(header.width, header.height, sizeof(OverlayColor)); OverlayColor *pixels = (OverlayColor *)to.pixels; + Graphics::PixelFormat format = g_system->getOverlayFormat(); for (int y = 0; y < to.h; ++y) { for (int x = 0; x < to.w; ++x) { uint8 r, g, b; - colorToRGB(in.readUint16BE(), r, g, b); + colorToRGB<ColorMasks<565> >(in.readUint16BE(), r, g, b); // converting to current OSystem Color - *pixels++ = g_system->RGBToColor(r, g, b); + *pixels++ = Graphics::RGBToColor(r, g, b, format); } } diff --git a/graphics/dxa_player.cpp b/graphics/video/dxa_player.cpp index f4c93a51f1..e228510e6b 100644 --- a/graphics/dxa_player.cpp +++ b/graphics/video/dxa_player.cpp @@ -24,23 +24,24 @@ */ #include "common/endian.h" -#include "common/file.h" -#include "graphics/dxa_player.h" +#include "common/archive.h" #include "common/util.h" +#include "graphics/video/dxa_player.h" + #ifdef USE_ZLIB -#include <zlib.h> + #include "common/zlib.h" #endif namespace Graphics { DXAPlayer::DXAPlayer() { - _fd = 0; + _fileStream = 0; _frameBuffer1 = 0; _frameBuffer2 = 0; _scaledBuffer = 0; - _drawBuffer = 0; + _videoFrameBuffer = 0; _inBuffer = 0; _inBufferSize = 0; @@ -48,104 +49,81 @@ DXAPlayer::DXAPlayer() { _decompBuffer = 0; _decompBufferSize = 0; - _width = 0; - _height = 0; + _videoInfo.width = 0; + _videoInfo.height = 0; _frameSize = 0; - _framesCount = 0; - _frameNum = 0; - _framesPerSec = 0; - _frameSkipped = 0; - _frameTicks = 0; + _videoInfo.frameCount = 0; + _videoInfo.currentFrame = 0; + _videoInfo.frameRate = 0; + _videoInfo.frameDelay = 0; _scaleMode = S_NONE; } DXAPlayer::~DXAPlayer() { + closeFile(); } -int DXAPlayer::getWidth() { - if (!_fd) - return 0; - return _width; -} - -int DXAPlayer::getHeight() { - if (!_fd) - return 0; - return _height; -} - -int DXAPlayer::getCurFrame() { - if (!_fd) - return -1; - return _frameNum; -} - -int DXAPlayer::getFrameCount() { - if (!_fd) - return 0; - return _framesCount; -} - -bool DXAPlayer::loadFile(const char *filename) { +bool DXAPlayer::loadFile(const char *fileName) { uint32 tag; int32 frameRate; - Common::File *file = new Common::File(); - if (!file->open(filename)) { - return 0; - } + closeFile(); - _fd = file; + _fileStream = SearchMan.openFile(fileName); + if (!_fileStream) + return false; - tag = _fd->readUint32BE(); + tag = _fileStream->readUint32BE(); assert(tag == MKID_BE('DEXA')); - uint8 flags = _fd->readByte(); - _framesCount = _fd->readUint16BE(); - frameRate = _fd->readUint32BE(); - - if (frameRate > 0) - _framesPerSec = 1000 / frameRate; - else if (frameRate < 0) - _framesPerSec = 100000 / (-frameRate); - else - _framesPerSec = 10; + uint8 flags = _fileStream->readByte(); + _videoInfo.frameCount = _fileStream->readUint16BE(); + frameRate = _fileStream->readSint32BE(); - if (frameRate < 0) - _frameTicks = -frameRate / 100; - else - _frameTicks = frameRate; + if (frameRate > 0) { + _videoInfo.frameRate = 1000 / frameRate; + _videoInfo.frameDelay = frameRate * 100; + } else if (frameRate < 0) { + _videoInfo.frameRate = 100000 / (-frameRate); + _videoInfo.frameDelay = -frameRate; + } else { + _videoInfo.frameRate = 10; + _videoInfo.frameDelay = 10000; + } - _width = _fd->readUint16BE(); - _height = _fd->readUint16BE(); + _videoInfo.width = _fileStream->readUint16BE(); + _videoInfo.height = _fileStream->readUint16BE(); if (flags & 0x80) { _scaleMode = S_INTERLACED; - _curHeight = _height / 2; + _curHeight = _videoInfo.height / 2; } else if (flags & 0x40) { _scaleMode = S_DOUBLE; - _curHeight = _height / 2; + _curHeight = _videoInfo.height / 2; } else { _scaleMode = S_NONE; - _curHeight = _height; + _curHeight = _videoInfo.height; } - debug(2, "flags 0x0%x framesCount %d width %d height %d rate %d ticks %d", flags, _framesCount, _width, _height, _framesPerSec, _frameTicks); + debug(2, "flags 0x0%x framesCount %d width %d height %d rate %d ticks %d", flags, getFrameCount(), getWidth(), getHeight(), getFrameRate(), getFrameDelay()); - _frameSize = _width * _height; + _frameSize = _videoInfo.width * _videoInfo.height; _decompBufferSize = _frameSize; _frameBuffer1 = (uint8 *)malloc(_frameSize); + memset(_frameBuffer1, 0, _frameSize); _frameBuffer2 = (uint8 *)malloc(_frameSize); + memset(_frameBuffer2, 0, _frameSize); if (!_frameBuffer1 || !_frameBuffer2) - error("DXAPlayer: Error allocating frame buffers (size %d)", _frameSize); + error("DXAPlayer: Error allocating frame buffers (size %u)", _frameSize); _scaledBuffer = 0; if (_scaleMode != S_NONE) { _scaledBuffer = (uint8 *)malloc(_frameSize); + memset(_scaledBuffer, 0, _frameSize); if (!_scaledBuffer) - error("Error allocating scale buffer (size %d)", _frameSize); + error("Error allocating scale buffer (size %u)", _frameSize); } #ifdef DXA_EXPERIMENT_MAXD @@ -154,20 +132,20 @@ bool DXAPlayer::loadFile(const char *filename) { uint32 size; do { - tag = _fd->readUint32BE(); + tag = _fileStream->readUint32BE(); if (tag != 0) { - size = _fd->readUint32BE(); + size = _fileStream->readUint32BE(); } switch (tag) { case 0: // No more tags break; case MKID_BE('MAXD'): assert(size == 4); - _decompBufferSize = _fd->readUint32BE(); + _decompBufferSize = _fileStream->readUint32BE(); break; default: // Unknown tag - skip it. while (size > 0) { - byte dummy = _fd->readByte(); + byte dummy = _fileStream->readByte(); size--; } break; @@ -175,17 +153,17 @@ bool DXAPlayer::loadFile(const char *filename) { } while (tag != 0); } #endif - _frameNum = 0; - _frameSkipped = 0; + _videoInfo.currentFrame = 0; return true; } void DXAPlayer::closeFile() { - if (!_fd) + if (!_fileStream) return; - delete _fd; + delete _fileStream; + _fileStream = 0; free(_frameBuffer1); free(_frameBuffer2); @@ -193,39 +171,14 @@ void DXAPlayer::closeFile() { free(_inBuffer); free(_decompBuffer); - _fd = 0; _inBuffer = 0; _decompBuffer = 0; } -void DXAPlayer::copyFrameToBuffer(byte *dst, uint x, uint y, uint pitch) { - uint h = _height; - uint w = _width; - - byte *src = _drawBuffer; - dst += y * pitch + x; - - do { - memcpy(dst, src, w); - dst += pitch; - src += _width; - } while (--h); -} - void DXAPlayer::decodeZlib(byte *data, int size, int totalSize) { #ifdef USE_ZLIB - z_stream _d_stream; - _d_stream.zalloc = (alloc_func)0; - _d_stream.zfree = (free_func)0; - _d_stream.opaque = (voidpf)0; - _d_stream.next_in = _inBuffer; - _d_stream.avail_in = size; - _d_stream.total_in = size; - _d_stream.next_out = data; - _d_stream.avail_out = totalSize; - inflateInit(&_d_stream); - inflate(&_d_stream, Z_FINISH); - inflateEnd(&_d_stream); + unsigned long dstLen = totalSize; + Common::uncompress(data, &dstLen, _inBuffer, size); #endif } @@ -236,8 +189,9 @@ void DXAPlayer::decode12(int size) { #ifdef USE_ZLIB if (_decompBuffer == NULL) { _decompBuffer = (byte *)malloc(_decompBufferSize); + memset(_decompBuffer, 0, _decompBufferSize); if (_decompBuffer == NULL) - error("Error allocating decomp buffer (size %d)", _decompBufferSize); + error("Error allocating decomp buffer (size %u)", _decompBufferSize); } /* decompress the input data */ decodeZlib(_decompBuffer, size, _decompBufferSize); @@ -246,10 +200,10 @@ void DXAPlayer::decode12(int size) { memcpy(_frameBuffer2, _frameBuffer1, _frameSize); - for (int by = 0; by < _height; by += BLOCKH) { - for (int bx = 0; bx < _width; bx += BLOCKW) { + for (uint32 by = 0; by < _videoInfo.height; by += BLOCKH) { + for (uint32 bx = 0; bx < _videoInfo.width; bx += BLOCKW) { byte type = *dat++; - byte *b2 = _frameBuffer1 + bx + by * _width; + byte *b2 = _frameBuffer1 + bx + by * _videoInfo.width; switch (type) { case 0: @@ -281,7 +235,7 @@ void DXAPlayer::decode12(int size) { } diffMap <<= 1; } - b2 += _width; + b2 += _videoInfo.width; } break; } @@ -292,7 +246,7 @@ void DXAPlayer::decode12(int size) { for (int xc = 0; xc < BLOCKW; xc++) { b2[xc] = color; } - b2 += _width; + b2 += _videoInfo.width; } break; } @@ -301,7 +255,7 @@ void DXAPlayer::decode12(int size) { for (int xc = 0; xc < BLOCKW; xc++) { b2[xc] = *dat++; } - b2 += _width; + b2 += _videoInfo.width; } break; } @@ -313,11 +267,11 @@ void DXAPlayer::decode12(int size) { int my = mbyte & 0x07; if (mbyte & 0x08) my = -my; - byte *b1 = _frameBuffer2 + (bx+mx) + (by+my) * _width; + byte *b1 = _frameBuffer2 + (bx+mx) + (by+my) * _videoInfo.width; for (int yc = 0; yc < BLOCKH; yc++) { memcpy(b2, b1, BLOCKW); - b1 += _width; - b2 += _width; + b1 += _videoInfo.width; + b2 += _videoInfo.width; } break; } @@ -337,8 +291,9 @@ void DXAPlayer::decode13(int size) { if (_decompBuffer == NULL) { _decompBuffer = (byte *)malloc(_decompBufferSize); + memset(_decompBuffer, 0, _decompBufferSize); if (_decompBuffer == NULL) - error("Error allocating decomp buffer (size %d)", _decompBufferSize); + error("Error allocating decomp buffer (size %u)", _decompBufferSize); } /* decompress the input data */ @@ -346,7 +301,7 @@ void DXAPlayer::decode13(int size) { memcpy(_frameBuffer2, _frameBuffer1, _frameSize); - int codeSize = _width * _curHeight / 16; + int codeSize = _videoInfo.width * _curHeight / 16; int dataSize, motSize, maskSize; dataSize = READ_BE_UINT32(&_decompBuffer[0]); @@ -358,10 +313,10 @@ void DXAPlayer::decode13(int size) { motBuf = &dataBuf[dataSize]; maskBuf = &motBuf[motSize]; - for (int by = 0; by < _curHeight; by += BLOCKH) { - for (int bx = 0; bx < _width; bx += BLOCKW) { + for (uint32 by = 0; by < _curHeight; by += BLOCKH) { + for (uint32 bx = 0; bx < _videoInfo.width; bx += BLOCKW) { uint8 type = *codeBuf++; - uint8 *b2 = (uint8*)_frameBuffer1 + bx + by * _width; + uint8 *b2 = (uint8*)_frameBuffer1 + bx + by * _videoInfo.width; switch (type) { case 0: @@ -378,7 +333,7 @@ void DXAPlayer::decode13(int size) { } diffMap <<= 1; } - b2 += _width; + b2 += _videoInfo.width; } break; } @@ -389,7 +344,7 @@ void DXAPlayer::decode13(int size) { for (int xc = 0; xc < BLOCKW; xc++) { b2[xc] = color; } - b2 += _width; + b2 += _videoInfo.width; } break; } @@ -398,7 +353,7 @@ void DXAPlayer::decode13(int size) { for (int xc = 0; xc < BLOCKW; xc++) { b2[xc] = *dataBuf++; } - b2 += _width; + b2 += _videoInfo.width; } break; } @@ -412,11 +367,11 @@ void DXAPlayer::decode13(int size) { if (mbyte & 0x08) my = -my; - uint8 *b1 = (uint8*)_frameBuffer2 + (bx+mx) + (by+my) * _width; + uint8 *b1 = (uint8*)_frameBuffer2 + (bx+mx) + (by+my) * _videoInfo.width; for (int yc = 0; yc < BLOCKH; yc++) { memcpy(b2, b1, BLOCKW); - b1 += _width; - b2 += _width; + b1 += _videoInfo.width; + b2 += _videoInfo.width; } break; } @@ -428,7 +383,7 @@ void DXAPlayer::decode13(int size) { for (int subBlock = 0; subBlock < 4; subBlock++) { int sx = bx + subX[subBlock], sy = by + subY[subBlock]; - b2 = (uint8*)_frameBuffer1 + sx + sy * _width; + b2 = (uint8*)_frameBuffer1 + sx + sy * _videoInfo.width; switch (subMask & 0xC0) { // 00: skip case 0x00: @@ -440,7 +395,7 @@ void DXAPlayer::decode13(int size) { for (int xc = 0; xc < BLOCKW / 2; xc++) { b2[xc] = subColor; } - b2 += _width; + b2 += _videoInfo.width; } break; } @@ -456,11 +411,11 @@ void DXAPlayer::decode13(int size) { if (mbyte & 0x08) my = -my; - uint8 *b1 = (uint8*)_frameBuffer2 + (sx+mx) + (sy+my) * _width; + uint8 *b1 = (uint8*)_frameBuffer2 + (sx+mx) + (sy+my) * _videoInfo.width; for (int yc = 0; yc < BLOCKH / 2; yc++) { memcpy(b2, b1, BLOCKW / 2); - b1 += _width; - b2 += _width; + b1 += _videoInfo.width; + b2 += _videoInfo.width; } break; } @@ -470,7 +425,7 @@ void DXAPlayer::decode13(int size) { for (int xc = 0; xc < BLOCKW / 2; xc++) { b2[xc] = *dataBuf++; } - b2 += _width; + b2 += _videoInfo.width; } break; } @@ -495,7 +450,7 @@ void DXAPlayer::decode13(int size) { b2[xc] = pixels[code & 1]; code >>= 1; } - b2 += _width; + b2 += _videoInfo.width; } } else { uint32 code = READ_BE_UINT32(maskBuf); @@ -505,7 +460,7 @@ void DXAPlayer::decode13(int size) { b2[xc] = pixels[code & 3]; code >>= 2; } - b2 += _width; + b2 += _videoInfo.width; } } break; @@ -518,30 +473,31 @@ void DXAPlayer::decode13(int size) { #endif } -void DXAPlayer::decodeNextFrame() { +bool DXAPlayer::decodeNextFrame() { uint32 tag; - tag = _fd->readUint32BE(); + tag = _fileStream->readUint32BE(); if (tag == MKID_BE('CMAP')) { byte rgb[768]; - _fd->read(rgb, ARRAYSIZE(rgb)); + _fileStream->read(rgb, ARRAYSIZE(rgb)); setPalette(rgb); } - tag = _fd->readUint32BE(); + tag = _fileStream->readUint32BE(); if (tag == MKID_BE('FRAM')) { - byte type = _fd->readByte(); - uint32 size = _fd->readUint32BE(); + byte type = _fileStream->readByte(); + uint32 size = _fileStream->readUint32BE(); if ((_inBuffer == NULL) || (_inBufferSize < size)) { free(_inBuffer); _inBuffer = (byte *)malloc(size); + memset(_inBuffer, 0, size); if (_inBuffer == NULL) - error("Error allocating input buffer (size %d)", size); + error("Error allocating input buffer (size %u)", size); _inBufferSize = size; } - _fd->read(_inBuffer, size); + _fileStream->read(_inBuffer, size); switch (type) { case 2: @@ -561,9 +517,9 @@ void DXAPlayer::decodeNextFrame() { } if (type == 3) { - for (int j = 0; j < _curHeight; ++j) { - for (int i = 0; i < _width; ++i) { - const int offs = j * _width + i; + for (uint32 j = 0; j < _curHeight; ++j) { + for (uint32 i = 0; i < _videoInfo.width; ++i) { + const int offs = j * _videoInfo.width + i; _frameBuffer1[offs] ^= _frameBuffer2[offs]; } } @@ -573,22 +529,24 @@ void DXAPlayer::decodeNextFrame() { switch (_scaleMode) { case S_INTERLACED: for (int cy = 0; cy < _curHeight; cy++) { - memcpy(&_scaledBuffer[2 * cy * _width], &_frameBuffer1[cy * _width], _width); - memset(&_scaledBuffer[((2 * cy) + 1) * _width], 0, _width); + memcpy(&_scaledBuffer[2 * cy * _videoInfo.width], &_frameBuffer1[cy * _videoInfo.width], _videoInfo.width); + memset(&_scaledBuffer[((2 * cy) + 1) * _videoInfo.width], 0, _videoInfo.width); } - _drawBuffer = _scaledBuffer; + _videoFrameBuffer = _scaledBuffer; break; case S_DOUBLE: for (int cy = 0; cy < _curHeight; cy++) { - memcpy(&_scaledBuffer[2 * cy * _width], &_frameBuffer1[cy * _width], _width); - memcpy(&_scaledBuffer[((2 * cy) + 1) * _width], &_frameBuffer1[cy * _width], _width); + memcpy(&_scaledBuffer[2 * cy * _videoInfo.width], &_frameBuffer1[cy * _videoInfo.width], _videoInfo.width); + memcpy(&_scaledBuffer[((2 * cy) + 1) * _videoInfo.width], &_frameBuffer1[cy * _videoInfo.width], _videoInfo.width); } - _drawBuffer = _scaledBuffer; + _videoFrameBuffer = _scaledBuffer; break; case S_NONE: - _drawBuffer = _frameBuffer1; + _videoFrameBuffer = _frameBuffer1; break; } + + return ++_videoInfo.currentFrame < _videoInfo.frameCount; } } // End of namespace Graphics diff --git a/graphics/video/dxa_player.h b/graphics/video/dxa_player.h new file mode 100644 index 0000000000..fb5645c089 --- /dev/null +++ b/graphics/video/dxa_player.h @@ -0,0 +1,76 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef GRAPHICS_VIDEO_DXA_PLAYER_H +#define GRAPHICS_VIDEO_DXA_PLAYER_H + +#include "graphics/video/video_player.h" + +namespace Graphics { + +class DXAPlayer : public VideoPlayer { +public: + DXAPlayer(); + virtual ~DXAPlayer(); + + /** + * Load a DXA encoded video file + * @param filename the filename to load + */ + bool loadFile(const char *fileName); + + /** + * Close a DXA encoded video file + */ + void closeFile(); + + bool decodeNextFrame(); + +private: + void decodeZlib(byte *data, int size, int totalSize); + void decode12(int size); + void decode13(int size); + + enum ScaleMode { + S_NONE, + S_INTERLACED, + S_DOUBLE + }; + + byte *_frameBuffer1; + byte *_frameBuffer2; + byte *_scaledBuffer; + byte *_inBuffer; + uint32 _inBufferSize; + byte *_decompBuffer; + uint32 _decompBufferSize; + uint16 _curHeight; + uint32 _frameSize; + ScaleMode _scaleMode; +}; + +} // End of namespace Graphics + +#endif diff --git a/graphics/video/flic_player.cpp b/graphics/video/flic_player.cpp new file mode 100644 index 0000000000..b928981ef3 --- /dev/null +++ b/graphics/video/flic_player.cpp @@ -0,0 +1,324 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#include "graphics/video/flic_player.h" +#include "common/archive.h" +#include "common/stream.h" +#include "common/endian.h" + +namespace Graphics { + +FlicPlayer::FlicPlayer() + : _paletteDirty(false), _offscreen(0), _currFrame(0), _fileStream(0) { + memset(&_flicInfo, 0, sizeof(_flicInfo)); +} + +FlicPlayer::~FlicPlayer() { + closeFile(); +} + +int FlicPlayer::getWidth() { + if (!_fileStream) + return 0; + return _flicInfo.width; +} + +int FlicPlayer::getHeight() { + if (!_fileStream) + return 0; + return _flicInfo.height; +} + +int32 FlicPlayer::getCurFrame() { + if (!_fileStream) + return -1; + return _currFrame; +} + +int32 FlicPlayer::getFrameCount() { + if (!_fileStream) + return 0; + return _flicInfo.numFrames; +} + +bool FlicPlayer::loadFile(const char *fileName) { + closeFile(); + + _fileStream = SearchMan.openFile(fileName); + if (!_fileStream) + return false; + + _flicInfo.size = _fileStream->readUint32LE(); + _flicInfo.type = _fileStream->readUint16LE(); + + // Check FLC magic number + if (_flicInfo.type != 0xAF12) { + warning("FlicPlayer::FlicPlayer(): attempted to load non-FLC data (type = 0x%04X)", _flicInfo.type); + delete _fileStream; + return false; + } + + _flicInfo.numFrames = _fileStream->readUint16LE(); + _flicInfo.width = _fileStream->readUint16LE(); + _flicInfo.height = _fileStream->readUint16LE(); + _fileStream->skip(4); + _flicInfo.speed = _fileStream->readUint32LE(); + + _fileStream->seek(80); + _flicInfo.offsetFrame1 = _fileStream->readUint32LE(); + _flicInfo.offsetFrame2 = _fileStream->readUint32LE(); + + _offscreen = new uint8[_flicInfo.width * _flicInfo.height]; + memset(_palette, 0, sizeof(_palette)); + _paletteDirty = false; + + // Seek to the first frame + _currFrame = 0; + _fileStream->seek(_flicInfo.offsetFrame1); + return true; +} + +void FlicPlayer::closeFile() { + if (!_fileStream) + return; + + delete _fileStream; + _fileStream = 0; + + delete[] _offscreen; + _offscreen = 0; +} + +void FlicPlayer::redraw() { + _dirtyRects.clear(); + _dirtyRects.push_back(Common::Rect(0, 0, _flicInfo.width, _flicInfo.height)); +} + +ChunkHeader FlicPlayer::readChunkHeader() { + ChunkHeader head; + + head.size = _fileStream->readUint32LE(); + head.type = _fileStream->readUint16LE(); + + return head; +} + +FrameTypeChunkHeader FlicPlayer::readFrameTypeChunkHeader(ChunkHeader chunkHead) { + FrameTypeChunkHeader head; + + head.header = chunkHead; + head.numChunks = _fileStream->readUint16LE(); + head.delay = _fileStream->readUint16LE(); + head.reserved = _fileStream->readUint16LE(); + head.widthOverride = _fileStream->readUint16LE(); + head.heightOverride = _fileStream->readUint16LE(); + + return head; +} + +void FlicPlayer::decodeByteRun(uint8 *data) { + uint8 *ptr = (uint8 *)_offscreen; + while ((ptr - _offscreen) < (_flicInfo.width * _flicInfo.height)) { + int chunks = *data++; + while (chunks--) { + int count = (int8)*data++; + if (count > 0) { + memset(ptr, *data++, count); + } else { + count = -count; + memcpy(ptr, data, count); + data += count; + } + ptr += count; + } + } + + redraw(); +} + +#define OP_PACKETCOUNT 0 +#define OP_UNDEFINED 1 +#define OP_LASTPIXEL 2 +#define OP_LINESKIPCOUNT 3 + +void FlicPlayer::decodeDeltaFLC(uint8 *data) { + uint16 linesInChunk = READ_LE_UINT16(data); data += 2; + uint16 currentLine = 0; + uint16 packetCount = 0; + + while (linesInChunk--) { + uint16 opcode; + + // First process all the opcodes. + do { + opcode = READ_LE_UINT16(data); data += 2; + + switch ((opcode >> 14) & 3) { + case OP_PACKETCOUNT: + packetCount = opcode; + break; + case OP_UNDEFINED: + break; + case OP_LASTPIXEL: + _offscreen[currentLine * _flicInfo.width + _flicInfo.width - 1] = (opcode & 0xFF); + _dirtyRects.push_back(Common::Rect(_flicInfo.width - 1, currentLine, _flicInfo.width, currentLine + 1)); + break; + case OP_LINESKIPCOUNT: + currentLine += -(int16)opcode; + break; + } + } while (((opcode >> 14) & 3) != OP_PACKETCOUNT); + + uint16 column = 0; + + // Now interpret the RLE data + while (packetCount--) { + column += *data++; + int rleCount = (int8)*data++; + if (rleCount > 0) { + memcpy(_offscreen + (currentLine * _flicInfo.width) + column, data, rleCount * 2); + data += rleCount * 2; + _dirtyRects.push_back(Common::Rect(column, currentLine, column + rleCount * 2, currentLine + 1)); + } else if (rleCount < 0) { + rleCount = -rleCount; + uint16 dataWord = READ_UINT16(data); data += 2; + for (int i = 0; i < rleCount; ++i) { + WRITE_UINT16(_offscreen + currentLine * _flicInfo.width + column + i * 2, dataWord); + } + _dirtyRects.push_back(Common::Rect(column, currentLine, column + rleCount * 2, currentLine + 1)); + } else { // End of cutscene ? + return; + } + column += rleCount * 2; + } + + currentLine++; + } +} + +#define FLI_SETPAL 4 +#define FLI_SS2 7 +#define FLI_BRUN 15 +#define PSTAMP 18 +#define FRAME_TYPE 0xF1FA + +void FlicPlayer::decodeNextFrame() { + FrameTypeChunkHeader frameHeader; + + // Read chunk + ChunkHeader cHeader = readChunkHeader(); + switch (cHeader.type) { + case FRAME_TYPE: + frameHeader = readFrameTypeChunkHeader(cHeader); + _currFrame++; + break; + default: + error("FlicPlayer::decodeFrame(): unknown main chunk type (type = 0x%02X)", cHeader.type); + break; + } + + // Read subchunks + if (cHeader.type == FRAME_TYPE) { + for (int i = 0; i < frameHeader.numChunks; ++i) { + cHeader = readChunkHeader(); + uint8 *data = new uint8[cHeader.size - 6]; + _fileStream->read(data, cHeader.size - 6); + switch (cHeader.type) { + case FLI_SETPAL: + setPalette(data); + _paletteDirty = true; + break; + case FLI_SS2: + decodeDeltaFLC(data); + break; + case FLI_BRUN: + decodeByteRun(data); + break; + case PSTAMP: + /* PSTAMP - skip for now */ + break; + default: + error("FlicPlayer::decodeFrame(): unknown subchunk type (type = 0x%02X)", cHeader.type); + break; + } + + delete[] data; + } + } + + // If we just processed the ring frame, set the next frame + if (_currFrame == _flicInfo.numFrames + 1) { + _currFrame = 1; + _fileStream->seek(_flicInfo.offsetFrame2); + } +} + +void FlicPlayer::reset() { + _currFrame = 0; + _fileStream->seek(_flicInfo.offsetFrame1); +} + +void FlicPlayer::setPalette(uint8 *mem) { + uint16 numPackets = READ_LE_UINT16(mem); mem += 2; + + if (0 == READ_LE_UINT16(mem)) { //special case + mem += 2; + for (int i = 0; i < 256; ++i) { + memcpy(_palette + i * 4, mem + i * 3, 3); + _palette[i * 4 + 3] = 0; + } + } else { + uint8 palPos = 0; + + while (numPackets--) { + palPos += *mem++; + uint8 change = *mem++; + + for (int i = 0; i < change; ++i) { + memcpy(_palette + (palPos + i) * 4, mem + i * 3, 3); + _palette[(palPos + i) * 4 + 3] = 0; + } + + palPos += change; + mem += (change * 3); + } + } +} + +void FlicPlayer::copyFrameToBuffer(byte *dst, uint x, uint y, uint pitch) { + uint h = _flicInfo.height; + uint w = _flicInfo.width; + + byte *src = (byte *)_offscreen; + dst += y * pitch + x; + + do { + memcpy(dst, src, w); + dst += pitch; + src += w; + } while (--h); +} + +} // End of namespace Graphics diff --git a/graphics/dxa_player.h b/graphics/video/flic_player.h index dbe39bbcee..43f3775bdd 100644 --- a/graphics/dxa_player.h +++ b/graphics/video/flic_player.h @@ -23,45 +23,37 @@ * */ -#ifndef GRAPHICS_DXA_PLAYER_H -#define GRAPHICS_DXA_PLAYER_H +#ifndef GRAPHICS_VIDEO_FLICPLAYER_H +#define GRAPHICS_VIDEO_FLICPLAYER_H #include "common/scummsys.h" -#include "common/stream.h" +#include "common/list.h" +#include "common/rect.h" + +namespace Common { + class SeekableReadStream; +} namespace Graphics { -enum ScaleMode { - S_NONE, - S_INTERLACED, - S_DOUBLE +struct ChunkHeader { + uint32 size; + uint16 type; }; -class DXAPlayer { -protected: - byte *_frameBuffer1; - byte *_frameBuffer2; - byte *_scaledBuffer; - byte *_drawBuffer; - byte *_inBuffer; - uint32 _inBufferSize; - byte *_decompBuffer; - uint32 _decompBufferSize; - uint16 _width; - uint16 _height, _curHeight; - uint16 _framesCount; - uint32 _framesPerSec; - uint16 _frameNum; - uint32 _frameSize; - uint16 _frameSkipped; - uint32 _frameTicks; - ScaleMode _scaleMode; +struct FrameTypeChunkHeader { + ChunkHeader header; + uint16 numChunks; + uint16 delay; + uint16 reserved; // always zero + uint16 widthOverride; + uint16 heightOverride; +}; +class FlicPlayer { public: - DXAPlayer(); - virtual ~DXAPlayer(); - - Common::SeekableReadStream *_fd; + FlicPlayer(); + ~FlicPlayer(); /** * Returns the width of the video @@ -79,31 +71,39 @@ public: * Returns the current frame number of the video * @return the current frame number of the video */ - int getCurFrame(); + int32 getCurFrame(); /** * Returns the amount of frames in the video * @return the amount of frames in the video */ - int getFrameCount(); + int32 getFrameCount(); /** - * Load a DXA encoded video file + * Load a FLIC encoded video file * @param filename the filename to load */ - bool loadFile(const char *filename); + bool loadFile(const char *fileName); /** - * Close a DXA encoded video file + * Close a FLIC encoded video file */ void closeFile(); -protected: /** - * Set palette, based on current frame - * @param pal the palette data + * Decode the next frame */ - virtual void setPalette(byte *pal) = 0; + void decodeNextFrame(); + + bool isLastFrame() const { return _currFrame == _flicInfo.numFrames; } + uint32 getSpeed() const { return _flicInfo.speed; } + bool isPaletteDirty() const { return _paletteDirty; } + const uint8 *getPalette() { _paletteDirty = false; return _palette; } + const uint8 *getOffscreen() const { return _offscreen; } + const Common::List<Common::Rect> *getDirtyRects() const { return &_dirtyRects; } + void clearDirtyRects() { _dirtyRects.clear(); } + void redraw(); + void reset(); /** * Copy current frame into the specified position of the destination @@ -115,14 +115,31 @@ protected: */ void copyFrameToBuffer(byte *dst, uint x, uint y, uint pitch); - /** - * Decode the next frame - */ - void decodeNextFrame(); - - void decodeZlib(byte *data, int size, int totalSize); - void decode12(int size); - void decode13(int size); +private: + struct FlicHeader { + uint32 size; + uint16 type; + uint16 numFrames; + uint16 width; + uint16 height; + uint32 speed; + uint16 offsetFrame1; + uint16 offsetFrame2; + }; + + ChunkHeader readChunkHeader(); + FrameTypeChunkHeader readFrameTypeChunkHeader(ChunkHeader chunkHead); + void decodeByteRun(uint8 *data); + void decodeDeltaFLC(uint8 *data); + void setPalette(uint8 *mem); + + Common::SeekableReadStream *_fileStream; + bool _paletteDirty; + uint8 *_offscreen; + uint8 _palette[256 * 4]; + FlicHeader _flicInfo; + uint16 _currFrame; + Common::List<Common::Rect> _dirtyRects; }; } // End of namespace Graphics diff --git a/graphics/mpeg_player.cpp b/graphics/video/mpeg_player.cpp index 3c0fd08cb3..964f4cd94d 100644 --- a/graphics/mpeg_player.cpp +++ b/graphics/video/mpeg_player.cpp @@ -23,7 +23,7 @@ * */ -#include "graphics/mpeg_player.h" +#include "graphics/video/mpeg_player.h" #include "common/file.h" #include "common/system.h" #include "common/util.h" @@ -389,10 +389,11 @@ void BaseAnimationState::buildLookup() { } // Set up entries 0-255 in rgb-to-pixel value tables. + Graphics::PixelFormat format = _sys->getOverlayFormat(); for (i = 0; i < 256; i++) { - r_2_pix_alloc[i + 256] = _sys->RGBToColor(i, 0, 0); - g_2_pix_alloc[i + 256] = _sys->RGBToColor(0, i, 0); - b_2_pix_alloc[i + 256] = _sys->RGBToColor(0, 0, i); + r_2_pix_alloc[i + 256] = Graphics::RGBToColor(i, 0, 0, format); + g_2_pix_alloc[i + 256] = Graphics::RGBToColor(0, i, 0, format); + b_2_pix_alloc[i + 256] = Graphics::RGBToColor(0, 0, i, format); } // Spread out the values we have to the rest of the array so that we do diff --git a/graphics/mpeg_player.h b/graphics/video/mpeg_player.h index 1c975825a8..7d42618926 100644 --- a/graphics/mpeg_player.h +++ b/graphics/video/mpeg_player.h @@ -23,8 +23,8 @@ * */ -#ifndef GRAPHICS_MPEG_PLAYER_H -#define GRAPHICS_MPEG_PLAYER_H +#ifndef GRAPHICS_VIDEO_MPEG_PLAYER_H +#define GRAPHICS_VIDEO_MPEG_PLAYER_H #include "common/scummsys.h" diff --git a/graphics/video/smk_player.cpp b/graphics/video/smk_player.cpp new file mode 100644 index 0000000000..81c18e94b4 --- /dev/null +++ b/graphics/video/smk_player.cpp @@ -0,0 +1,856 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +// Based on http://wiki.multimedia.cx/index.php?title=Smacker +// and the FFmpeg Smacker decoder (libavcodec/smacker.c), revision 16143 +// http://svn.ffmpeg.org/ffmpeg/trunk/libavcodec/smacker.c?revision=16143&view=markup + +#include "graphics/video/smk_player.h" + +#include "common/archive.h" +#include "common/array.h" +#include "common/endian.h" +#include "common/util.h" +#include "common/stream.h" +#include "common/system.h" + +#include "sound/mixer.h" +#include "sound/audiostream.h" + +namespace Graphics { + +enum SmkBlockTypes { + SMK_BLOCK_MONO = 0, + SMK_BLOCK_FULL = 1, + SMK_BLOCK_SKIP = 2, + SMK_BLOCK_FILL = 3 +}; + +/* + * class BitStream + * Keeps a two-byte lookahead, so overallocate buf by 2 bytes if + * you want to avoid OOB reads. + */ + +class BitStream { +public: + BitStream(byte *buf, uint32 length) + : _buf(buf), _end(buf+length), _curBit(8) { + _curBytes = *_buf++; + _curBytes |= *_buf++ << 8; + } + + bool getBit(); + byte getBits8(); + + byte peek8() const; + void skip(int n); + +private: + byte *_buf; + byte *_end; + uint16 _curBytes; + byte _curBit; +}; + +bool BitStream::getBit() { + bool v = _curBytes & 1; + + _curBytes >>= 1; + + if (--_curBit == 0) { + _curBytes |= *_buf++ << 8; + _curBit = 8; + } + + return v; +} + +byte BitStream::getBits8() { + byte v = _curBytes & 0xff; + _curBytes >>= 8; + _curBytes |= *_buf++ << _curBit; + return v; +} + +byte BitStream::peek8() const { + return _curBytes & 0xff; +} + +void BitStream::skip(int n) { + assert(n <= 8); + _curBytes >>= n; + + if (_curBit > n) { + _curBit -= n; + } else { + _curBit = _curBit + 8 - n; + _curBytes |= *_buf++ << _curBit; + } +} + +/* + * class SmallHuffmanTree + * A Huffman-tree to hold 8-bit values. + * Unoptimized since it's only used during smk initialization. + */ + +class SmallHuffmanTree { +public: + SmallHuffmanTree(BitStream &bs) : _bs(bs) { + uint32 bit = _bs.getBit(); + assert(bit); + + decodeTree(0); + + bit = _bs.getBit(); + assert(!bit); + } + + uint16 getCode(BitStream &bs); +private: + enum { + SMK_NODE = 0x8000 + }; + + int decodeTree(int length); + + Common::Array<uint16> _tree; + BitStream &_bs; +}; + +int SmallHuffmanTree::decodeTree(int length) { + if (!_bs.getBit()) { // Leaf + uint16 v = _bs.getBits8(); + + _tree.push_back(v); + return 1; + } + + _tree.push_back(0); // placeholder for r1 + int t = _tree.size() - 1; + + int r1 = decodeTree(length + 1); + + _tree[t] = (SMK_NODE | r1); + + int r2 = decodeTree(length + 1); + + return r1+r2+1; +} + +uint16 SmallHuffmanTree::getCode(BitStream &bs) { + uint16 *p = &_tree[0]; + + while (*p & SMK_NODE) { + if (bs.getBit()) + p += *p & ~SMK_NODE; + p++; + } + + return *p; +} + +/* + * class BigHuffmanTree + * A Huffman-tree to hold 16-bit values. + * Contains the beginnings of an optimization. + */ + +class BigHuffmanTree { +public: + BigHuffmanTree(BitStream &bs); + + void reset(); + uint32 getCode(BitStream &bs); +private: + enum { + SMK_NODE = 0x80000000 + }; + + int decodeTree(uint32 prefix, int length); + + Common::Array<uint32> _tree; + uint32 _last[3]; + + int _prefixtree[256]; + int _prefixlength[256]; + + /* Used during construction */ + BitStream &_bs; + uint32 _markers[3]; + SmallHuffmanTree *_loBytes; + SmallHuffmanTree *_hiBytes; +}; + +BigHuffmanTree::BigHuffmanTree(BitStream &bs) + : _bs(bs) { + uint32 bit = _bs.getBit(); + if (!bit) { + _tree.push_back(0); + _last[0] = _last[1] = _last[2] = 0; + return; + } + + int i; + for (i = 0; i < 256; ++i) + _prefixtree[i] = 0; + + _loBytes = new SmallHuffmanTree(_bs); + _hiBytes = new SmallHuffmanTree(_bs); + + _markers[0] = _bs.getBits8() | (_bs.getBits8() << 8); + _markers[1] = _bs.getBits8() | (_bs.getBits8() << 8); + _markers[2] = _bs.getBits8() | (_bs.getBits8() << 8); + + _last[0] = _last[1] = _last[2] = 0xffffffff; + + decodeTree(0, 0); + bit = _bs.getBit(); + assert(!bit); + + for (i = 0; i < 3; ++i) { + if (_last[i] == 0xffffffff) { + _tree.push_back(0); + _last[i] = _tree.size() - 1; + } + } + + delete _loBytes; + delete _hiBytes; +} + +void BigHuffmanTree::reset() { + _tree[_last[0]] = _tree[_last[1]] = _tree[_last[2]] = 0; +} + +int BigHuffmanTree::decodeTree(uint32 prefix, int length) { + uint32 bit = _bs.getBit(); + + if (!bit) { // Leaf + uint32 lo = _loBytes->getCode(_bs); + uint32 hi = _hiBytes->getCode(_bs); + + uint32 v = (hi << 8) | lo; + _tree.push_back(v); + + int t = _tree.size() - 1; + + if (length <= 8) { + uint32 i; + for (i = 0; i < 256; i += (1 << length)) { + _prefixtree[prefix | i] = t; + _prefixlength[prefix | i] = length; + } + } + + int i; + for (i = 0; i < 3; ++i) { + if (_markers[i] == v) { + _last[i] = t; + _tree[t] = 0; + } + } + + return 1; + } + + _tree.push_back(0); // placeholder for r1 + int t = _tree.size() - 1; + + if (length == 8) { + _prefixtree[prefix] = t; + _prefixlength[prefix] = 8; + } + + int r1 = decodeTree(prefix, length + 1); + + _tree[t] = SMK_NODE | r1; + + int r2 = decodeTree(prefix | (1 << length), length + 1); + return r1+r2+1; +} + +uint32 BigHuffmanTree::getCode(BitStream &bs) { + uint32 *p = &_tree[0]; + + byte peek = bs.peek8(); + p = &_tree[_prefixtree[peek]]; + bs.skip(_prefixlength[peek]); + + while (*p & SMK_NODE) { + if (bs.getBit()) + p += (*p) & ~SMK_NODE; + p++; + } + + uint32 v = *p; + if (v != _tree[_last[0]]) { + _tree[_last[2]] = _tree[_last[1]]; + _tree[_last[1]] = _tree[_last[0]]; + _tree[_last[0]] = v; + } + + return v; +} + +SMKPlayer::SMKPlayer(Audio::Mixer *mixer) + : _audioStarted(false), _audioStream(0), _mixer(mixer) { +} + +SMKPlayer::~SMKPlayer() { +} + +int SMKPlayer::getHeight() { + if (!_fileStream) + return 0; + return (_header.flags ? 2 : 1) * _videoInfo.height; +} + +int32 SMKPlayer::getAudioLag() { + if (!_fileStream) + return 0; + + int32 frameDelay = getFrameDelay(); + int32 videoTime = _videoInfo.currentFrame * frameDelay; + int32 audioTime; + + if (!_audioStream) { + /* No audio. + Calculate the lag by how much time has gone by since the first frame + and how much time *should* have passed. + */ + + audioTime = (g_system->getMillis() - _videoInfo.startTime) * 100; + } else + audioTime = (((int32) _mixer->getSoundElapsedTime(_audioHandle)) * 100); + + return videoTime - audioTime; +} + +bool SMKPlayer::loadFile(const char *fileName) { + int32 frameRate; + + closeFile(); + + _fileStream = SearchMan.openFile(fileName); + if (!_fileStream) + return false; + + // Seek to the first frame + _videoInfo.currentFrame = 0; + _header.signature = _fileStream->readUint32BE(); + + // No BINK support available + if (_header.signature == MKID_BE('BIKi')) { + delete _fileStream; + _fileStream = 0; + return false; + } + + assert(_header.signature == MKID_BE('SMK2') || _header.signature == MKID_BE('SMK4')); + + _videoInfo.width = _fileStream->readUint32LE(); + _videoInfo.height = _fileStream->readUint32LE(); + _videoInfo.frameCount = _fileStream->readUint32LE(); + frameRate = _fileStream->readSint32LE(); + + if (frameRate > 0) { + _videoInfo.frameRate = 1000 / frameRate; + _videoInfo.frameDelay = frameRate * 100; + } else if (frameRate < 0) { + _videoInfo.frameRate = 100000 / (-frameRate); + _videoInfo.frameDelay = -frameRate; + } else { + _videoInfo.frameRate = 10; + _videoInfo.frameDelay = 10000; + } + + // Flags are determined by which bit is set, which can be one of the following: + // 0 - set to 1 if file contains a ring frame. + // 1 - set to 1 if file is Y-interlaced + // 2 - set to 1 if file is Y-doubled + // If bits 1 or 2 are set, the frame should be scaled to twice its height + // before it is displayed. + _header.flags = _fileStream->readUint32LE(); + + // TODO: should we do any extra processing for Smacker files with ring frames? + + // TODO: should we do any extra processing for Y-doubled videos? Are they the + // same as Y-interlaced videos? + + uint32 i; + for (i = 0; i < 7; ++i) + _header.audioSize[i] = _fileStream->readUint32LE(); + + _header.treesSize = _fileStream->readUint32LE(); + _header.mMapSize = _fileStream->readUint32LE(); + _header.mClrSize = _fileStream->readUint32LE(); + _header.fullSize = _fileStream->readUint32LE(); + _header.typeSize = _fileStream->readUint32LE(); + + uint32 audioInfo; + for (i = 0; i < 7; ++i) { + // AudioRate - Frequency and format information for each sound track, up to 7 audio tracks. + // The 32 constituent bits have the following meaning: + // * bit 31 - data is compressed + // * bit 30 - indicates that audio data is present for this track + // * bit 29 - 1 = 16-bit audio; 0 = 8-bit audio + // * bit 28 - 1 = stereo audio; 0 = mono audio + // * bits 27-26 - if both set to zero - use v2 sound decompression + // * bits 25-24 - unused + // * bits 23-0 - audio sample rate + audioInfo = _fileStream->readUint32LE(); + _header.audioInfo[i].isCompressed = audioInfo & 0x80000000; + _header.audioInfo[i].hasAudio = audioInfo & 0x40000000; + _header.audioInfo[i].is16Bits = audioInfo & 0x20000000; + _header.audioInfo[i].isStereo = audioInfo & 0x10000000; + _header.audioInfo[i].hasV2Compression = !(audioInfo & 0x8000000) && + !(audioInfo & 0x4000000); + _header.audioInfo[i].sampleRate = audioInfo & 0xFFFFFF; + + if (_header.audioInfo[i].hasAudio && i == 0) { + byte flags = 0; + + if (_header.audioInfo[i].is16Bits) + flags = flags | Audio::Mixer::FLAG_16BITS; + + if (_header.audioInfo[i].isStereo) + flags = flags | Audio::Mixer::FLAG_STEREO; + + _audioStream = Audio::makeAppendableAudioStream(_header.audioInfo[i].sampleRate, flags); + } + } + + _header.dummy = _fileStream->readUint32LE(); + + _frameSizes = (uint32 *)malloc(_videoInfo.frameCount * sizeof(uint32)); + for (i = 0; i < _videoInfo.frameCount; ++i) + _frameSizes[i] = _fileStream->readUint32LE(); + + _frameTypes = (byte *)malloc(_videoInfo.frameCount); + for (i = 0; i < _videoInfo.frameCount; ++i) + _frameTypes[i] = _fileStream->readByte(); + + Common::Array<byte> huffmanTrees; + huffmanTrees.resize(_header.treesSize + 2); + _fileStream->read(&huffmanTrees[0], _header.treesSize); + huffmanTrees[_header.treesSize] = 0; + huffmanTrees[_header.treesSize + 1] = 0; + + BitStream bs(&huffmanTrees[0], _header.treesSize); + + _MMapTree = new BigHuffmanTree(bs); + _MClrTree = new BigHuffmanTree(bs); + _FullTree = new BigHuffmanTree(bs); + _TypeTree = new BigHuffmanTree(bs); + + _videoFrameBuffer = (byte *)malloc(2 * _videoInfo.width * _videoInfo.height); + memset(_videoFrameBuffer, 0, 2 * _videoInfo.width * _videoInfo.height); + _palette = (byte *)malloc(3 * 256); + memset(_palette, 0, 3 * 256); + + return true; +} + +void SMKPlayer::closeFile() { + if (!_fileStream) + return; + + if (_audioStarted && _audioStream) { + _mixer->stopHandle(_audioHandle); + _audioStream = 0; + _audioStarted = false; + } + + delete _fileStream; + _fileStream = 0; + + delete _MMapTree; + delete _MClrTree; + delete _FullTree; + delete _TypeTree; + + free(_frameSizes); + free(_frameTypes); + free(_videoFrameBuffer); + free(_palette); +} + +bool SMKPlayer::decodeNextFrame() { + uint i; + uint32 chunkSize = 0; + uint32 dataSizeUnpacked = 0; + + uint32 startPos = _fileStream->pos(); + + if (_videoInfo.currentFrame == 0) + _videoInfo.startTime = g_system->getMillis(); + + // Check if we got a frame with palette data, and + // call back the virtual setPalette function to set + // the current palette + if (_frameTypes[_videoInfo.currentFrame] & 1) { + unpackPalette(); + setPalette(_palette); + } + + // Load audio tracks + for (i = 0; i < 7; ++i) { + if (!(_frameTypes[_videoInfo.currentFrame] & (2 << i))) + continue; + + chunkSize = _fileStream->readUint32LE(); + chunkSize -= 4; // subtract the first 4 bytes (chunk size) + + if (_header.audioInfo[i].isCompressed) { + dataSizeUnpacked = _fileStream->readUint32LE(); + chunkSize -= 4; // subtract the next 4 bytes (unpacked data size) + } else { + dataSizeUnpacked = 0; + } + + if (_header.audioInfo[i].hasAudio && chunkSize > 0 && i == 0) { + // If it's track 0, play the audio data + byte *soundBuffer = new byte[chunkSize + 2]; + + _fileStream->read(soundBuffer, chunkSize); + soundBuffer[chunkSize] = 0; + soundBuffer[chunkSize + 1] = 0; + + if (_header.audioInfo[i].isCompressed) { + // Compressed audio (Huffman DPCM encoded) + queueCompressedBuffer(soundBuffer, chunkSize, dataSizeUnpacked, i); + delete[] soundBuffer; + } else { + // Uncompressed audio (PCM) + _audioStream->queueBuffer(soundBuffer, chunkSize); + // The sound buffer will be deleted by AppendableAudioStream + } + + if (!_audioStarted) { + _mixer->playInputStream(Audio::Mixer::kPlainSoundType, &_audioHandle, _audioStream, -1, 255); + _audioStarted = true; + } + } else { + // Ignore the rest of the audio tracks, if they exist + // TODO: Are there any Smacker videos with more than one audio stream? + // If yes, we should play the rest of the audio streams as well + if (chunkSize > 0) + _fileStream->skip(chunkSize); + } + } + + uint32 frameSize = _frameSizes[_videoInfo.currentFrame] & ~3; + + if (_fileStream->pos() - startPos > frameSize) + exit(1); + + uint32 frameDataSize = frameSize - (_fileStream->pos() - startPos); + + _frameData = (byte *)malloc(frameDataSize + 2); + _fileStream->read(_frameData, frameDataSize); + _frameData[frameDataSize] = 0; + _frameData[frameDataSize + 1] = 0; + + BitStream bs(_frameData, frameDataSize); + + _MMapTree->reset(); + _MClrTree->reset(); + _FullTree->reset(); + _TypeTree->reset(); + + uint bw = _videoInfo.width / 4; + uint bh = _videoInfo.height / 4; + uint stride = _videoInfo.width; + uint block = 0, blocks = bw*bh; + + uint doubleY = _header.flags ? 2 : 1; + + byte *out; + uint type, run, j, mode; + uint32 p1, p2, clr, map; + byte hi, lo; + + while (block < blocks) { + type = _TypeTree->getCode(bs); + run = getBlockRun((type >> 2) & 0x3f); + + switch (type & 3) { + case SMK_BLOCK_MONO: + while (run-- && block < blocks) { + clr = _MClrTree->getCode(bs); + map = _MMapTree->getCode(bs); + out = _videoFrameBuffer + (block / bw) * (stride * 4 * doubleY) + (block % bw) * 4; + hi = clr >> 8; + lo = clr & 0xff; + for (i = 0; i < 4; i++) { + for (j = 0; j < doubleY; j++) { + out[0] = (map & 1) ? hi : lo; + out[1] = (map & 2) ? hi : lo; + out[2] = (map & 4) ? hi : lo; + out[3] = (map & 8) ? hi : lo; + out += stride; + } + map >>= 4; + } + ++block; + } + break; + case SMK_BLOCK_FULL: + // Smacker v2 has one mode, Smacker v4 has three + if (_header.signature == MKID_BE('SMK2')) { + mode = 0; + } else { + // 00 - mode 0 + // 10 - mode 1 + // 01 - mode 2 + mode = 0; + if (bs.getBit()) { + mode = 1; + } else if (bs.getBit()) { + mode = 2; + } + } + + while (run-- && block < blocks) { + out = _videoFrameBuffer + (block / bw) * (stride * 4 * doubleY) + (block % bw) * 4; + switch (mode) { + case 0: + for (i = 0; i < 4; ++i) { + p1 = _FullTree->getCode(bs); + p2 = _FullTree->getCode(bs); + for (j = 0; j < doubleY; ++j) { + out[2] = p1 & 0xff; + out[3] = p1 >> 8; + out[0] = p2 & 0xff; + out[1] = p2 >> 8; + out += stride; + } + } + break; + case 1: + p1 = _FullTree->getCode(bs); + out[0] = out[1] = p1 & 0xFF; + out[2] = out[3] = p1 >> 8; + out += stride; + out[0] = out[1] = p1 & 0xFF; + out[2] = out[3] = p1 >> 8; + out += stride; + p2 = _FullTree->getCode(bs); + out[0] = out[1] = p2 & 0xFF; + out[2] = out[3] = p2 >> 8; + out += stride; + out[0] = out[1] = p2 & 0xFF; + out[2] = out[3] = p2 >> 8; + out += stride; + break; + case 2: + for(i = 0; i < 2; i++) { + // We first get p2 and then p1 + // Check ffmpeg thread "[PATCH] Smacker video decoder bug fix" + // http://article.gmane.org/gmane.comp.video.ffmpeg.devel/78768 + p2 = _FullTree->getCode(bs); + p1 = _FullTree->getCode(bs); + for (j = 0; j < doubleY; ++j) { + out[0] = p1 & 0xff; + out[1] = p1 >> 8; + out[2] = p2 & 0xff; + out[3] = p2 >> 8; + out += stride; + } + for (j = 0; j < doubleY; ++j) { + out[0] = p1 & 0xff; + out[1] = p1 >> 8; + out[2] = p2 & 0xff; + out[3] = p2 >> 8; + out += stride; + } + } + break; + } + ++block; + } + break; + case SMK_BLOCK_SKIP: + while (run-- && block < blocks) + block++; + break; + case SMK_BLOCK_FILL: + uint32 col; + mode = type >> 8; + while (run-- && block < blocks) { + out = _videoFrameBuffer + (block / bw) * (stride * 4 * doubleY) + (block % bw) * 4; + col = mode * 0x01010101; + for (i = 0; i < 4 * doubleY; ++i) { + out[0] = out[1] = out[2] = out[3] = col; + out += stride; + } + ++block; + } + break; + } + } + + _fileStream->seek(startPos + frameSize); + + free(_frameData); + + return ++_videoInfo.currentFrame < _videoInfo.frameCount; +} + +void SMKPlayer::queueCompressedBuffer(byte *buffer, uint32 bufferSize, + uint32 unpackedSize, int streamNum) { + + BitStream audioBS(buffer, bufferSize); + bool dataPresent = audioBS.getBit(); + + if (!dataPresent) + return; + + bool isStereo = audioBS.getBit(); + assert(isStereo == _header.audioInfo[streamNum].isStereo); + bool is16Bits = audioBS.getBit(); + assert(is16Bits == _header.audioInfo[streamNum].is16Bits); + + int numBytes = 1 * (isStereo ? 2 : 1) * (is16Bits ? 2 : 1); + + byte *unpackedBuffer = new byte[unpackedSize]; + byte *curPointer = unpackedBuffer; + uint32 curPos = 0; + + SmallHuffmanTree *audioTrees[4]; + for (int k = 0; k < numBytes; k++) + audioTrees[k] = new SmallHuffmanTree(audioBS); + + // Base values, stored as big endian + + int32 bases[2]; + + if (isStereo) + bases[1] = (!is16Bits) ? audioBS.getBits8() : + ((int16) (((audioBS.getBits8() << 8) | audioBS.getBits8()))); + + bases[0] = (!is16Bits) ? audioBS.getBits8() : + ((int16) (((audioBS.getBits8() << 8) | audioBS.getBits8()))); + + + // The bases are the first samples, too + for (int i = 0; i < (isStereo ? 2 : 1); i++, curPointer += (is16Bits ? 2 : 1), curPos += (is16Bits ? 2 : 1)) { + if (is16Bits) + WRITE_BE_UINT16(curPointer, bases[i]); + else + *curPointer = (bases[i] & 0xFF) ^ 0x80; + } + + // Next follow the deltas, which are added to the corresponding base values and + // are stored as little endian + // We store the unpacked bytes in big endian format + + while (curPos < unpackedSize) { + // If the sample is stereo, the data is stored for the left and right channel, respectively + // (the exact opposite to the base values) + if (!is16Bits) { + + for (int k = 0; k < (isStereo ? 2 : 1); k++) { + bases[k] += (int8) ((int16) audioTrees[k]->getCode(audioBS)); + *curPointer++ = CLIP<int>(bases[k], 0, 255) ^ 0x80; + curPos++; + } + + } else { + + for (int k = 0; k < (isStereo ? 2 : 1); k++) { + bases[k] += (int16) (audioTrees[k * 2]->getCode(audioBS) | + (audioTrees[k * 2 + 1]->getCode(audioBS) << 8)); + + WRITE_BE_UINT16(curPointer, CLIP<int32>(bases[k], -32768, 32767)); + curPointer += 2; + curPos += 2; + } + } + + } + + for (int k = 0; k < numBytes; k++) + delete audioTrees[k]; + + _audioStream->queueBuffer(unpackedBuffer, unpackedSize); + // unpackedBuffer will be deleted by AppendableAudioStream +} + +void SMKPlayer::unpackPalette() { + uint startPos = _fileStream->pos(); + uint32 len = 4 * _fileStream->readByte(); + + byte *chunk = (byte *)malloc(len); + _fileStream->read(&chunk[0], len); + byte *p = &chunk[0]; + + byte oldPalette[3*256]; + memcpy(oldPalette, _palette, 3 * 256); + + byte *pal = _palette; + + int sz = 0; + byte b0; + while (sz < 256) { + b0 = *p++; + if (b0 & 0x80) { // if top bit is 1 (0x80 = 10000000) + sz += (b0 & 0x7f) + 1; // get lower 7 bits + 1 (0x7f = 01111111) + pal += 3 * ((b0 & 0x7f) + 1); + } else if (b0 & 0x40) { // if top 2 bits are 01 (0x40 = 01000000) + byte c = (b0 & 0x3f) + 1; // get lower 6 bits + 1 (0x3f = 00111111) + uint s = 3 * *p++; + sz += c; + + while (c--) { + *pal++ = oldPalette[s + 0]; + *pal++ = oldPalette[s + 1]; + *pal++ = oldPalette[s + 2]; + s += 3; + } + } else { // top 2 bits are 00 + sz++; + // get the lower 6 bits for each component (0x3f = 00111111) + byte b = b0 & 0x3f; + byte g = (*p++) & 0x3f; + byte r = (*p++) & 0x3f; + + assert(g < 0xc0 && b < 0xc0); + + // upscale to full 8-bit color values by multiplying by 4 + *pal++ = b * 4; + *pal++ = g * 4; + *pal++ = r * 4; + } + } + + _fileStream->seek(startPos + len); + + free(chunk); +} + +} // End of namespace Graphics diff --git a/graphics/video/smk_player.h b/graphics/video/smk_player.h new file mode 100644 index 0000000000..512eb57c20 --- /dev/null +++ b/graphics/video/smk_player.h @@ -0,0 +1,115 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +// Based on http://wiki.multimedia.cx/index.php?title=Smacker +// and the FFmpeg Smacker decoder (libavcodec/smacker.c), revision 16143 +// http://svn.ffmpeg.org/ffmpeg/trunk/libavcodec/smacker.c?revision=16143&view=markup + +#ifndef GRAPHICS_VIDEO_SMK_PLAYER_H +#define GRAPHICS_VIDEO_SMK_PLAYER_H + +#include "graphics/video/video_player.h" +#include "sound/mixer.h" + +namespace Audio { + class AppendableAudioStream; +} + +namespace Graphics { + +class BigHuffmanTree; + +/** + * Implementation of a Smacker v2/v4 video decoder + */ +class SMKPlayer : public Graphics::VideoPlayer { +public: + SMKPlayer(Audio::Mixer *mixer); + virtual ~SMKPlayer(); + + int getHeight(); + int32 getAudioLag(); + + /** + * Load an SMK encoded video file + * @param filename the filename to load + */ + bool loadFile(const char *filename); + void closeFile(); + + bool decodeNextFrame(); + +private: + void unpackPalette(); + // Possible runs of blocks + uint getBlockRun(int index) { return (index <= 58) ? index + 1 : 128 << (index - 59); } + void queueCompressedBuffer(byte *buffer, uint32 bufferSize, uint32 unpackedSize, int streamNum); + + struct AudioInfo { + bool isCompressed; + bool hasAudio; + bool is16Bits; + bool isStereo; + bool hasV2Compression; + uint32 sampleRate; + }; + + struct { + uint32 signature; + uint32 flags; + uint32 audioSize[7]; + uint32 treesSize; + uint32 mMapSize; + uint32 mClrSize; + uint32 fullSize; + uint32 typeSize; + AudioInfo audioInfo[7]; + uint32 dummy; + } _header; + + uint32 *_frameSizes; + // The FrameTypes section of a Smacker file contains an array of bytes, where + // the 8 bits of each byte describe the contents of the corresponding frame. + // The highest 7 bits correspond to audio frames (bit 7 is track 6, bit 6 track 5 + // and so on), so there can be up to 7 different audio tracks. When the lowest bit + // (bit 0) is set, it denotes a frame that contains a palette record + byte *_frameTypes; + byte *_frameData; + byte *_palette; + + Audio::Mixer *_mixer; + bool _audioStarted; + Audio::AppendableAudioStream *_audioStream; + Audio::SoundHandle _audioHandle; + + BigHuffmanTree *_MMapTree; + BigHuffmanTree *_MClrTree; + BigHuffmanTree *_FullTree; + BigHuffmanTree *_TypeTree; +}; + +} // End of namespace Graphics + +#endif diff --git a/graphics/video/video_player.cpp b/graphics/video/video_player.cpp new file mode 100644 index 0000000000..6bf7a5e39d --- /dev/null +++ b/graphics/video/video_player.cpp @@ -0,0 +1,220 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#include "common/archive.h" +#include "common/events.h" +#include "common/system.h" +#include "common/util.h" +#include "common/array.h" +#include "common/endian.h" + +#include "graphics/video/video_player.h" +#include "graphics/surface.h" + +namespace Graphics { + +VideoPlayer::VideoPlayer() : _fileStream(0), _skipVideo(false) { +} + +VideoPlayer::~VideoPlayer() { + closeFile(); +} + +int VideoPlayer::getWidth() { + if (!_fileStream) + return 0; + return _videoInfo.width; +} + +int VideoPlayer::getHeight() { + if (!_fileStream) + return 0; + return _videoInfo.height; +} + +int32 VideoPlayer::getCurFrame() { + if (!_fileStream) + return -1; + return _videoInfo.currentFrame; +} + +int32 VideoPlayer::getFrameCount() { + if (!_fileStream) + return 0; + return _videoInfo.frameCount; +} + +int32 VideoPlayer::getFrameRate() { + if (!_fileStream) + return 0; + return _videoInfo.frameRate; +} + +int32 VideoPlayer::getFrameDelay() { + if (!_fileStream) + return 0; + return _videoInfo.frameDelay; +} + +int32 VideoPlayer::getAudioLag() { + if (!_fileStream) + return 0; + + /* No audio. + Calculate the lag by how much time has gone by since the first frame + and how much time *should* have passed. + */ + int32 audioTime = (g_system->getMillis() - _videoInfo.startTime) * 100; + int32 videoTime = _videoInfo.currentFrame * getFrameDelay(); + + return videoTime - audioTime; +} + +uint32 VideoPlayer::getFrameWaitTime() { + int32 waitTime = (getFrameDelay() + getAudioLag()) / 100; + + if (waitTime < 0) + return 0; + + return waitTime; +} + +bool VideoPlayer::loadFile(const char *fileName) { + return false; +} + +void VideoPlayer::closeFile() { +} + +void VideoPlayer::copyFrameToBuffer(byte *dst, uint x, uint y, uint pitch) { + uint h = getHeight(); + uint w = getWidth(); + + byte *src = _videoFrameBuffer; + dst += y * pitch + x; + + do { + memcpy(dst, src, w); + dst += pitch; + src += w; + } while (--h); +} + +void VideoPlayer::setPalette(byte *pal) { + byte videoPalette[256 * 4]; + + for (int i = 0; i < 256; i++) { + videoPalette[i * 4 + 0] = *pal++; + videoPalette[i * 4 + 1] = *pal++; + videoPalette[i * 4 + 2] = *pal++; + videoPalette[i * 4 + 3] = 0; + } + + g_system->setPalette(videoPalette, 0, 256); +} + +bool VideoPlayer::decodeNextFrame() { + return false; +} + +void VideoPlayer::performPostProcessing(byte *screen) { +} + +void VideoPlayer::processVideoEvents(Common::List<Common::Event> *stopEvents) { + Common::Event curEvent; + Common::EventManager *eventMan = g_system->getEventManager(); + + // Process events, and skip video if esc is pressed + while (eventMan->pollEvent(curEvent)) { + if (curEvent.type == Common::EVENT_RTL || curEvent.type == Common::EVENT_QUIT) { + _skipVideo = true; + } + + for (Common::List<Common::Event>::const_iterator iter = stopEvents->begin(); iter != stopEvents->end(); iter++) { + if (curEvent.type == iter->type) { + if (iter->type == Common::EVENT_KEYDOWN || iter->type == Common::EVENT_KEYUP) { + if (curEvent.kbd.keycode == iter->kbd.keycode) { + _skipVideo = true; + break; + } + } else { + _skipVideo = true; + break; + } + } + } + } +} + +bool VideoPlayer::playVideo(const char *filename, Common::List<Common::Event> *stopEvents) { + _skipVideo = false; + debug(0, "Playing video %s", filename); + + if (!loadFile(filename)) { + warning("Failed to load video file %s", filename); + return false; + } + + g_system->clearScreen(); + + while (getCurFrame() < getFrameCount() && !_skipVideo) { + processVideoEvents(stopEvents); + + uint32 startTime = 0; + decodeNextFrame(); + + Graphics::Surface *screen = g_system->lockScreen(); + copyFrameToBuffer((byte *)screen->pixels, + (g_system->getWidth() - getWidth()) / 2, + (g_system->getHeight() - getHeight()) / 2, + g_system->getWidth()); + performPostProcessing((byte *)screen->pixels); + g_system->unlockScreen(); + + uint32 waitTime = getFrameWaitTime(); + + if (!waitTime) { + warning("dropped frame %i", getCurFrame()); + continue; + } + + // Update the screen + g_system->updateScreen(); + + startTime = g_system->getMillis(); + + // Wait before showing the next frame + while (g_system->getMillis() < startTime + waitTime && !_skipVideo) { + processVideoEvents(stopEvents); + g_system->delayMillis(10); + } + } + + closeFile(); + + return true; +} + +} // End of namespace Graphics diff --git a/graphics/video/video_player.h b/graphics/video/video_player.h new file mode 100644 index 0000000000..4a44349552 --- /dev/null +++ b/graphics/video/video_player.h @@ -0,0 +1,170 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef GRAPHICS_VIDEO_PLAYER_H +#define GRAPHICS_VIDEO_PLAYER_H + +#include "common/scummsys.h" +#include "common/events.h" +#include "common/list.h" + +namespace Common { + class SeekableReadStream; +} + +namespace Graphics { + +/** + * Implementation of a generic video decoder + */ +class VideoPlayer { +public: + VideoPlayer(); + virtual ~VideoPlayer(); + + /** + * Returns the width of the video + * @return the width of the video + */ + virtual int getWidth(); + + /** + * Returns the height of the video + * @return the height of the video + */ + virtual int getHeight(); + + /** + * Returns the current frame number of the video + * @return the current frame number of the video + */ + virtual int32 getCurFrame(); + + /** + * Returns the amount of frames in the video + * @return the amount of frames in the video + */ + virtual int32 getFrameCount(); + + /** + * Returns the frame rate of the video + * @return the frame rate of the video + */ + virtual int32 getFrameRate(); + + /** + * Returns the time to wait for each frame in 1/100 ms + * @return the time to wait for each frame in 1/100 ms + */ + virtual int32 getFrameDelay(); + + /** + * Returns the current A/V lag in 1/100 ms + * If > 0, audio lags behind + * If < 0, video lags behind + * @return the current A/V lag in 1/100 ms + */ + virtual int32 getAudioLag(); + + /** + * Returns the time to wait until the next frame in ms, minding any lag + * @return the time to wait until the next frame in ms + */ + virtual uint32 getFrameWaitTime(); + + /** + * Load a video file + * @param filename the filename to load + */ + virtual bool loadFile(const char *filename); + + /** + * Close a video file + */ + virtual void closeFile(); + + /** + * Returns if a video file is loaded or not + */ + bool isVideoLoaded() { return (_fileStream != NULL); } + + /** + * Set RGB palette, based on current frame + * @param pal the RGB palette data + */ + virtual void setPalette(byte *pal); + + /** + * Copy current frame into the specified position of the destination + * buffer. + * @param dst the buffer + * @param x the x position of the buffer + * @param y the y position of the buffer + * @param pitch the pitch of buffer + */ + void copyFrameToBuffer(byte *dst, uint x, uint y, uint pitch); + + /** + * Decode the next frame + */ + virtual bool decodeNextFrame(); + + /** + * A default implementation of a video player + * Plays a non-interactive full screen video till it's stopped by a + * specific event + * @param filename the name of the file to play + * @param stopEvents a list of events that can stop the video + */ + bool playVideo(const char *filename, Common::List<Common::Event> *stopEvents); + + /** + * Perform postprocessing once the frame data is copied to the screen, + * right before the frame is drawn. Called from playVideo() + */ + virtual void performPostProcessing(byte *screen); + +protected: + struct { + uint32 width; + uint32 height; + uint32 frameCount; + int32 frameRate; + int32 frameDelay; + uint32 currentFrame; + uint32 startTime; + } _videoInfo; + + Common::SeekableReadStream *_fileStream; + byte *_videoFrameBuffer; + bool _skipVideo; + +private: + void processVideoEvents(Common::List<Common::Event> *stopEvents); +}; + +} // End of namespace Graphics + +#endif |