From 4fe80cd66954db753df3efdad343772583ddb062 Mon Sep 17 00:00:00 2001 From: Pawel Kolodziejski Date: Sun, 19 Apr 2015 07:43:34 +0200 Subject: [PATCH] ALL: synced with ScummVM --- Makefile | 1 + audio/decoders/mp3.cpp | 17 + audio/decoders/quicktime.cpp | 9 + backends/audiocd/sdl/sdl-audiocd.cpp | 7 +- backends/audiocd/sdl/sdl-audiocd.h | 4 + backends/events/sdl/sdl-events.cpp | 204 ++++- backends/events/sdl/sdl-events.h | 31 +- backends/fs/amigaos4/amigaos4-fs.cpp | 8 +- backends/graphics/sdl/sdl-graphics.cpp | 39 +- backends/graphics/sdl/sdl-graphics.h | 34 +- .../surfacesdl/surfacesdl-graphics.cpp | 19 +- .../graphics/surfacesdl/surfacesdl-graphics.h | 7 +- backends/log/log.cpp | 4 +- .../doublebuffersdl/doublebuffersdl-mixer.cpp | 4 + backends/mixer/sdl/sdl-mixer.cpp | 4 + backends/module.mk | 2 + backends/platform/sdl/macosx/macosx.cpp | 7 +- backends/platform/sdl/macosx/macosx.h | 1 - backends/platform/sdl/module.mk | 4 +- backends/platform/sdl/sdl-sys.h | 117 +++ backends/platform/sdl/sdl-window.cpp | 223 +++++ backends/platform/sdl/sdl-window.h | 112 +++ backends/platform/sdl/sdl.cpp | 84 +- backends/platform/sdl/sdl.h | 11 +- backends/platform/sdl/win32/win32-main.cpp | 2 + backends/platform/sdl/win32/win32-window.cpp | 59 ++ backends/platform/sdl/win32/win32-window.h | 37 + backends/platform/sdl/win32/win32.cpp | 30 +- backends/platform/sdl/win32/win32.h | 1 - backends/taskbar/win32/win32-taskbar.cpp | 18 +- backends/taskbar/win32/win32-taskbar.h | 6 +- base/main.cpp | 3 + common/fft.cpp | 4 + common/fft.h | 2 + configure | 9 +- devtools/create_project/create_project.cpp | 14 +- dists/codeblocks/readme.txt | 2 +- graphics/fonts/ttf.cpp | 6 +- gui/Tooltip.cpp | 2 + gui/Tooltip.h | 33 +- gui/dialog.cpp | 2 +- image/codecs/cinepak.cpp | 577 ++++++++++--- image/codecs/cinepak.h | 18 + image/codecs/cinepak_tables.h | 780 ++++++++++++++++++ image/codecs/codec.cpp | 148 ++++ image/codecs/codec.h | 29 + image/codecs/qtrle.cpp | 164 +++- image/codecs/qtrle.h | 15 +- image/codecs/rpza.cpp | 268 ++++-- image/codecs/rpza.h | 14 +- ports.mk | 4 +- video/avi_decoder.cpp | 24 + video/avi_decoder.h | 7 +- video/qt_decoder.cpp | 120 +++ video/qt_decoder.h | 10 +- video/video_decoder.cpp | 20 + video/video_decoder.h | 32 + 57 files changed, 3047 insertions(+), 366 deletions(-) create mode 100644 backends/platform/sdl/sdl-window.cpp create mode 100644 backends/platform/sdl/sdl-window.h create mode 100644 backends/platform/sdl/win32/win32-window.cpp create mode 100644 backends/platform/sdl/win32/win32-window.h create mode 100644 image/codecs/cinepak_tables.h diff --git a/Makefile b/Makefile index 4968f80e4bf..4a95f62fab4 100644 --- a/Makefile +++ b/Makefile @@ -91,6 +91,7 @@ ifeq "$(findstring config.mk,$(MAKEFILE_LIST))" "config.mk" LDFLAGS="$(SAVED_LDFLAGS)" CXX="$(SAVED_CXX)" \ CXXFLAGS="$(SAVED_CXXFLAGS)" CPPFLAGS="$(SAVED_CPPFLAGS)" \ ASFLAGS="$(SAVED_ASFLAGS)" WINDRESFLAGS="$(SAVED_WINDRESFLAGS)" \ + SDL_CONFIG="$(SAVED_SDL_CONFIG)" \ $(srcdir)/configure $(SAVED_CONFIGFLAGS) else $(error You need to run $(srcdir)/configure before you can run make. Check $(srcdir)/configure --help for a list of parameters) diff --git a/audio/decoders/mp3.cpp b/audio/decoders/mp3.cpp index c1b3faaeb11..feb531f5ae9 100644 --- a/audio/decoders/mp3.cpp +++ b/audio/decoders/mp3.cpp @@ -246,6 +246,23 @@ void MP3Stream::initStream() { _inStream->seek(0, SEEK_SET); _curTime = mad_timer_zero; _posInFrame = 0; + + // Skip ID3 TAG if any + // ID3v1 (beginning with with 'TAG') is located at the end of files. So we can ignore those. + // ID3v2 can be located at the start of files and begins with a 10 bytes header, the first 3 bytes being 'ID3'. + // The tag size is coded on the last 4 bytes of the 10 bytes header as a 32 bit synchsafe integer. + // See http://id3.org/id3v2.4.0-structure for details. + char data[10]; + _inStream->read(data, 10); + if (data[0] == 'I' && data[1] == 'D' && data[2] == '3') { + uint32 size = data[9] + 128 * (data[8] + 128 * (data[7] + 128 * data[6])); + // This size does not include an optional 10 bytes footer. Check if it is present. + if (data[5] & 0x10) + size += 10; + debug("Skipping ID3 TAG (%d bytes)", size + 10); + _inStream->seek(size, SEEK_CUR); + } else + _inStream->seek(0, SEEK_SET); // Update state _state = MP3_STATE_READY; diff --git a/audio/decoders/quicktime.cpp b/audio/decoders/quicktime.cpp index 331c850b1a4..ff87e7a9f8c 100644 --- a/audio/decoders/quicktime.cpp +++ b/audio/decoders/quicktime.cpp @@ -241,6 +241,15 @@ void QuickTimeAudioDecoder::QuickTimeAudioTrack::queueAudio(const Timestamp &len // If we have any samples that we need to skip (ie. we seeked into // the middle of a chunk), skip them here. if (_skipSamples != Timestamp()) { + if (_skipSamples > chunkLength) { + // If the amount we need to skip is greater than the size + // of the chunk, just skip it altogether. + _curMediaPos = _curMediaPos + chunkLength; + _skipSamples = _skipSamples - chunkLength; + delete stream; + continue; + } + skipSamples(_skipSamples, stream); _curMediaPos = _curMediaPos + _skipSamples; chunkLength = chunkLength - _skipSamples; diff --git a/backends/audiocd/sdl/sdl-audiocd.cpp b/backends/audiocd/sdl/sdl-audiocd.cpp index 5093c03a1c6..c7b089af09e 100644 --- a/backends/audiocd/sdl/sdl-audiocd.cpp +++ b/backends/audiocd/sdl/sdl-audiocd.cpp @@ -24,9 +24,12 @@ #if defined(SDL_BACKEND) -#include "common/textconsole.h" #include "backends/audiocd/sdl/sdl-audiocd.h" +#if !SDL_VERSION_ATLEAST(1, 3, 0) + +#include "common/textconsole.h" + SdlAudioCDManager::SdlAudioCDManager() : _cdrom(0), @@ -133,4 +136,6 @@ void SdlAudioCDManager::updateCD() { } } +#endif // !SDL_VERSION_ATLEAST(1, 3, 0) + #endif diff --git a/backends/audiocd/sdl/sdl-audiocd.h b/backends/audiocd/sdl/sdl-audiocd.h index ff98fcdd776..783d4fe0f04 100644 --- a/backends/audiocd/sdl/sdl-audiocd.h +++ b/backends/audiocd/sdl/sdl-audiocd.h @@ -27,6 +27,8 @@ #include "backends/platform/sdl/sdl-sys.h" +#if !SDL_VERSION_ATLEAST(1, 3, 0) + /** * The SDL audio cd manager. Implements real audio cd playback. */ @@ -47,4 +49,6 @@ protected: uint32 _cdEndTime, _cdStopTime; }; +#endif // !SDL_VERSION_ATLEAST(1, 3, 0) + #endif diff --git a/backends/events/sdl/sdl-events.cpp b/backends/events/sdl/sdl-events.cpp index ea5e37a18b0..2a8f2e3bcb6 100644 --- a/backends/events/sdl/sdl-events.cpp +++ b/backends/events/sdl/sdl-events.cpp @@ -59,8 +59,33 @@ #define JOY_BUT_SPACE 4 #define JOY_BUT_F5 5 +#if SDL_VERSION_ATLEAST(2, 0, 0) +static uint32 convUTF8ToUTF32(const char *src) { + uint32 utf32 = 0; + + char *dst = SDL_iconv_string( +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + "UTF-32BE", +#else + "UTF-32LE", +#endif + "UTF-8", src, SDL_strlen(src) + 1); + + if (dst) { + utf32 = *((uint32 *)dst); + SDL_free(dst); + } + + return utf32; +} +#endif + SdlEventSource::SdlEventSource() - : EventSource(), _scrollLock(false), _joystick(0), _lastScreenID(0), _graphicsManager(0) { + : EventSource(), _scrollLock(false), _joystick(0), _lastScreenID(0), _graphicsManager(0) +#if SDL_VERSION_ATLEAST(2, 0, 0) + , _queuedFakeKeyUp(false), _fakeKeyUp() +#endif + { // Reset mouse state memset(&_km, 0, sizeof(_km)); @@ -73,8 +98,14 @@ SdlEventSource::SdlEventSource() // Enable joystick if (SDL_NumJoysticks() > joystick_num) { - debug("Using joystick: %s", SDL_JoystickName(joystick_num)); _joystick = SDL_JoystickOpen(joystick_num); + debug("Using joystick: %s", +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_JoystickName(_joystick) +#else + SDL_JoystickName(joystick_num) +#endif + ); } else { warning("Invalid joystick: %d", joystick_num); } @@ -86,21 +117,24 @@ SdlEventSource::~SdlEventSource() { SDL_JoystickClose(_joystick); } -int SdlEventSource::mapKey(SDLKey key, SDLMod mod, Uint16 unicode) { - if (key >= SDLK_F1 && key <= SDLK_F9) { +int SdlEventSource::mapKey(SDLKey sdlKey, SDLMod mod, Uint16 unicode) { + Common::KeyCode key = SDLToOSystemKeycode(sdlKey); + + if (key >= Common::KEYCODE_F1 && key <= Common::KEYCODE_F9) { return key - SDLK_F1 + Common::ASCII_F1; - } else if (key >= SDLK_KP0 && key <= SDLK_KP9) { - return key - SDLK_KP0 + '0'; - } else if (key >= SDLK_UP && key <= SDLK_PAGEDOWN) { + } else if (key >= Common::KEYCODE_KP0 && key <= Common::KEYCODE_KP9) { + return key - Common::KEYCODE_KP0 + '0'; + } else if (key >= Common::KEYCODE_UP && key <= Common::KEYCODE_PAGEDOWN) { return key; } else if (unicode) { return unicode; } else if (key >= 'a' && key <= 'z' && (mod & KMOD_SHIFT)) { return key & ~0x20; - } else if (key >= SDLK_NUMLOCK && key <= SDLK_EURO) { + } else if (key >= Common::KEYCODE_NUMLOCK && key <= Common::KEYCODE_EURO) { return 0; + } else { + return key; } - return key; } // ResidualVM specific relMouse x,y @@ -187,7 +221,9 @@ void SdlEventSource::handleKbdMouse() { } // ResidualVM: disable wrap mouse for now, it's really annoying - //SDL_WarpMouse((Uint16)_km.x, (Uint16)_km.y); + if (_graphicsManager) { + //_graphicsManager->getWindow()->warpMouseInWindow((Uint16)_km.x, (Uint16)_km.y); + } } } } @@ -354,7 +390,9 @@ Common::KeyCode SdlEventSource::SDLToOSystemKeycode(const SDLKey key) { case SDLK_HELP: return Common::KEYCODE_HELP; case SDLK_PRINT: return Common::KEYCODE_PRINT; case SDLK_SYSREQ: return Common::KEYCODE_SYSREQ; +#if !SDL_VERSION_ATLEAST(2, 0, 0) case SDLK_BREAK: return Common::KEYCODE_BREAK; +#endif case SDLK_MENU: return Common::KEYCODE_MENU; case SDLK_POWER: return Common::KEYCODE_POWER; case SDLK_UNDO: return Common::KEYCODE_UNDO; @@ -365,6 +403,16 @@ Common::KeyCode SdlEventSource::SDLToOSystemKeycode(const SDLKey key) { bool SdlEventSource::pollEvent(Common::Event &event) { handleKbdMouse(); +#if SDL_VERSION_ATLEAST(2, 0, 0) + // In case we still need to send a key up event for a key down from a + // TEXTINPUT event we do this immediately. + if (_queuedFakeKeyUp) { + event = _fakeKeyUp; + _queuedFakeKeyUp = false; + return true; + } +#endif + // If the screen changed, send an Common::EVENT_SCREEN_CHANGED int screenID = ((OSystem_SDL *)g_system)->getGraphicsManager()->getScreenChangeID(); if (screenID != _lastScreenID) { @@ -401,24 +449,73 @@ bool SdlEventSource::dispatchSDLEvent(SDL_Event &ev, Common::Event &event) { case SDL_JOYAXISMOTION: return handleJoyAxisMotion(ev, event); +#if SDL_VERSION_ATLEAST(2, 0, 0) + case SDL_MOUSEWHEEL: { + Sint32 yDir = ev.wheel.y; +#if SDL_VERSION_ATLEAST(2, 0, 4) + if (ev.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) { + yDir *= -1; + } +#endif + // HACK: It seems we want the mouse coordinates supplied + // with a mouse wheel event. However, SDL2 does not supply + // these, thus we use whatever we got last time. It seems + // these are always stored in _km.x, _km.y. + processMouseEvent(event, _km.x, _km.y); + if (yDir < 0) { + event.type = Common::EVENT_WHEELDOWN; + return true; + } else if (yDir > 0) { + event.type = Common::EVENT_WHEELUP; + return true; + } else { + return false; + } + } + + case SDL_TEXTINPUT: { + // When we get a TEXTINPUT event it means we got some user input for + // which no KEYDOWN exists. SDL 1.2 introduces a "fake" key down+up + // in such cases. We will do the same to mimic it's behavior. + event.type = Common::EVENT_KEYDOWN; + + event.kbd = Common::KeyState(Common::KEYCODE_INVALID, convUTF8ToUTF32(ev.text.text), 0); + + SDLModToOSystemKeyFlags(SDL_GetModState(), event); + // Set the scroll lock sticky flag + if (_scrollLock) + event.kbd.flags |= Common::KBD_SCRL; + + // Fake a key up when we have a proper ascii value. + _queuedFakeKeyUp = (event.kbd.ascii != 0); + _fakeKeyUp = event; + _fakeKeyUp.type = Common::EVENT_KEYUP; + + return _queuedFakeKeyUp; + } + + case SDL_WINDOWEVENT: + switch (ev.window.event) { + case SDL_WINDOWEVENT_EXPOSED: + if (_graphicsManager) + _graphicsManager->notifyVideoExpose(); + return false; + + case SDL_WINDOWEVENT_RESIZED: + return handleResizeEvent(event, ev.window.data1, ev.window.data2); + + default: + return false; + } +#else case SDL_VIDEOEXPOSE: if (_graphicsManager) _graphicsManager->notifyVideoExpose(); return false; case SDL_VIDEORESIZE: - if (_graphicsManager) { - _graphicsManager->notifyResize(ev.resize.w, ev.resize.h); - - // If the screen changed, send an Common::EVENT_SCREEN_CHANGED - int screenID = ((OSystem_SDL *)g_system)->getGraphicsManager()->getScreenChangeID(); - if (screenID != _lastScreenID) { - _lastScreenID = screenID; - event.type = Common::EVENT_SCREEN_CHANGED; - return true; - } - } - return false; + return handleResizeEvent(event, ev.resize.w, ev.resize.h); +#endif case SDL_QUIT: event.type = Common::EVENT_QUIT; @@ -443,7 +540,9 @@ bool SdlEventSource::handleKeyDown(SDL_Event &ev, Common::Event &event) { // Ctrl-m toggles mouse capture if (event.kbd.hasFlags(Common::KBD_CTRL) && ev.key.keysym.sym == 'm') { - toggleMouseGrab(); + if (_graphicsManager) { + _graphicsManager->getWindow()->toggleMouseGrab(); + } return false; } @@ -486,7 +585,7 @@ bool SdlEventSource::handleKeyDown(SDL_Event &ev, Common::Event &event) { event.type = Common::EVENT_KEYDOWN; event.kbd.keycode = SDLToOSystemKeycode(ev.key.keysym.sym); - event.kbd.ascii = mapKey(ev.key.keysym.sym, (SDLMod)ev.key.keysym.mod, (Uint16)ev.key.keysym.unicode); + event.kbd.ascii = mapKey(ev.key.keysym.sym, (SDLMod)ev.key.keysym.mod, obtainUnicode(ev.key.keysym)); return true; } @@ -530,7 +629,7 @@ bool SdlEventSource::handleKeyUp(SDL_Event &ev, Common::Event &event) { event.type = Common::EVENT_KEYUP; event.kbd.keycode = SDLToOSystemKeycode(ev.key.keysym.sym); - event.kbd.ascii = mapKey(ev.key.keysym.sym, (SDLMod)ev.key.keysym.mod, (Uint16)ev.key.keysym.unicode); + event.kbd.ascii = mapKey(ev.key.keysym.sym, (SDLMod)ev.key.keysym.mod, 0); // Ctrl-Alt- will change the GFX mode SDLModToOSystemKeyFlags(mod, event); @@ -766,13 +865,6 @@ bool SdlEventSource::remapKey(SDL_Event &ev, Common::Event &event) { return false; } -void SdlEventSource::toggleMouseGrab() { - if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) - SDL_WM_GrabInput(SDL_GRAB_ON); - else - SDL_WM_GrabInput(SDL_GRAB_OFF); -} - void SdlEventSource::resetKeyboadEmulation(int16 x_max, int16 y_max) { _km.x_max = x_max; _km.y_max = y_max; @@ -780,4 +872,52 @@ void SdlEventSource::resetKeyboadEmulation(int16 x_max, int16 y_max) { _km.last_time = 0; } +bool SdlEventSource::handleResizeEvent(Common::Event &event, int w, int h) { + if (_graphicsManager) { + _graphicsManager->notifyResize(w, h); + + // If the screen changed, send an Common::EVENT_SCREEN_CHANGED + int screenID = ((OSystem_SDL *)g_system)->getGraphicsManager()->getScreenChangeID(); + if (screenID != _lastScreenID) { + _lastScreenID = screenID; + event.type = Common::EVENT_SCREEN_CHANGED; + return true; + } + } + + return false; +} + +uint32 SdlEventSource::obtainUnicode(const SDL_keysym keySym) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_Event events[2]; + + // In SDL2, the unicode field has been removed from the keysym struct. + // Instead a SDL_TEXTINPUT event is generated on key combinations that + // generates unicode. + // Here we peek into the event queue for the event to see if it exists. + int n = SDL_PeepEvents(events, 2, SDL_PEEKEVENT, SDL_KEYDOWN, SDL_TEXTINPUT); + // Make sure that the TEXTINPUT event belongs to this KEYDOWN + // event and not another pending one. + if ((n > 0 && events[0].type == SDL_TEXTINPUT) + || (n > 1 && events[0].type != SDL_KEYDOWN && events[1].type == SDL_TEXTINPUT)) { + // Remove the text input event we associate with the key press. This + // makes sure we never get any SDL_TEXTINPUT events which do "belong" + // to SDL_KEYDOWN events. + n = SDL_PeepEvents(events, 1, SDL_GETEVENT, SDL_TEXTINPUT, SDL_TEXTINPUT); + // This is basically a paranoia safety check because we know there + // must be a text input event in the queue. + if (n > 0) { + return convUTF8ToUTF32(events[0].text.text); + } else { + return 0; + } + } else { + return 0; + } +#else + return keySym.unicode; +#endif +} + #endif diff --git a/backends/events/sdl/sdl-events.h b/backends/events/sdl/sdl-events.h index fc31881e6e9..118e5a37833 100644 --- a/backends/events/sdl/sdl-events.h +++ b/backends/events/sdl/sdl-events.h @@ -49,11 +49,6 @@ public: */ virtual void resetKeyboadEmulation(int16 x_max, int16 y_max); - /** - * Toggles mouse input grab - */ - virtual void toggleMouseGrab(); - protected: /** @name Keyboard mouse emulation * Disabled by fingolfin 2004-12-18. @@ -132,7 +127,7 @@ protected: /** * Maps the ASCII value of key */ - virtual int mapKey(SDLKey key, SDLMod mod, Uint16 unicode); + int mapKey(SDLKey key, SDLMod mod, Uint16 unicode); /** * Configures the key modifiers flags status @@ -143,6 +138,30 @@ protected: * Translates SDL key codes to OSystem key codes */ Common::KeyCode SDLToOSystemKeycode(const SDLKey key); + + /** + * Notify graphics manager of a resize request. + */ + bool handleResizeEvent(Common::Event &event, int w, int h); + + /** + * Extracts unicode information for the specific key sym. + * May only be used for key down events. + */ + uint32 obtainUnicode(const SDL_keysym keySym); + +#if SDL_VERSION_ATLEAST(2, 0, 0) + /** + * Whether _fakeKeyUp contains an event we need to send. + */ + bool _queuedFakeKeyUp; + + /** + * A fake key up event when we receive a TEXTINPUT without any previous + * KEYDOWN event. + */ + Common::Event _fakeKeyUp; +#endif }; #endif diff --git a/backends/fs/amigaos4/amigaos4-fs.cpp b/backends/fs/amigaos4/amigaos4-fs.cpp index 7bebdf8ce64..6d5b0997367 100644 --- a/backends/fs/amigaos4/amigaos4-fs.cpp +++ b/backends/fs/amigaos4/amigaos4-fs.cpp @@ -381,15 +381,17 @@ AbstractFSList AmigaOSFilesystemNode::listVolumes() const { dosList = IDOS->NextDosEntry(dosList, LDF_VOLUMES); while (dosList) { if (dosList->dol_Type == DLT_VOLUME && - dosList->dol_Name) { + dosList->dol_Name && + dosList->dol_Port) { // The original line was //if (dosList->dol_Type == DLT_VOLUME && //dosList->dol_Name && //dosList->dol_Task) { // which errored using SDK 53.24 with a 'struct dosList' has no member called 'dol_Task' - // I removed dol_Task because it's not used anywhere else - // and it neither brought up further errors nor crashes or regressions + // The reason for that was that + // 1) dol_Task wasn't a task pointer, it is a message port instead + // 2) It was redefined to be dol_Port in dos/obsolete.h in afore mentioned SDK // Copy name to buffer IDOS->CopyStringBSTRToC(dosList->dol_Name, buffer, MAXPATHLEN); diff --git a/backends/graphics/sdl/sdl-graphics.cpp b/backends/graphics/sdl/sdl-graphics.cpp index b5e49fa397e..a13ca45477b 100644 --- a/backends/graphics/sdl/sdl-graphics.cpp +++ b/backends/graphics/sdl/sdl-graphics.cpp @@ -22,10 +22,12 @@ #include "backends/graphics/sdl/sdl-graphics.h" +#include "backends/platform/sdl/sdl-sys.h" #include "backends/events/sdl/sdl-events.h" +#include "common/textconsole.h" -SdlGraphicsManager::SdlGraphicsManager(SdlEventSource *source) - : _eventSource(source) { +SdlGraphicsManager::SdlGraphicsManager(SdlEventSource *source, SdlWindow *window) + : _eventSource(source), _window(window) { } SdlGraphicsManager::~SdlGraphicsManager() { @@ -38,3 +40,36 @@ void SdlGraphicsManager::activateManager() { void SdlGraphicsManager::deactivateManager() { _eventSource->setGraphicsManager(0); } + +SdlGraphicsManager::State SdlGraphicsManager::getState() { + State state; + + state.screenWidth = getWidth(); + state.screenHeight = getHeight(); + state.aspectRatio = getFeatureState(OSystem::kFeatureAspectRatioCorrection); + state.fullscreen = getFeatureState(OSystem::kFeatureFullscreenMode); + state.cursorPalette = getFeatureState(OSystem::kFeatureCursorPalette); +#ifdef USE_RGB_COLOR + state.pixelFormat = getScreenFormat(); +#endif + return state; +} + +bool SdlGraphicsManager::setState(const State &state) { + beginGFXTransaction(); +#ifdef USE_RGB_COLOR + initSize(state.screenWidth, state.screenHeight, &state.pixelFormat); +#else + initSize(state.screenWidth, state.screenHeight, 0); +#endif + setFeatureState(OSystem::kFeatureAspectRatioCorrection, state.aspectRatio); + setFeatureState(OSystem::kFeatureFullscreenMode, state.fullscreen); + setFeatureState(OSystem::kFeatureCursorPalette, state.cursorPalette); + + if (endGFXTransaction() != OSystem::kTransactionSuccess) { + return false; + } else { + return true; + } +} + diff --git a/backends/graphics/sdl/sdl-graphics.h b/backends/graphics/sdl/sdl-graphics.h index 3ef540708a2..7f8790a9b47 100644 --- a/backends/graphics/sdl/sdl-graphics.h +++ b/backends/graphics/sdl/sdl-graphics.h @@ -24,6 +24,7 @@ #define BACKENDS_GRAPHICS_SDL_SDLGRAPHICS_H #include "backends/graphics/graphics.h" +#include "backends/platform/sdl/sdl-window.h" #include "common/rect.h" @@ -36,7 +37,7 @@ class SdlEventSource; */ class SdlGraphicsManager : virtual public GraphicsManager { public: - SdlGraphicsManager(SdlEventSource *source); + SdlGraphicsManager(SdlEventSource *source, SdlWindow *window); virtual ~SdlGraphicsManager(); /** @@ -91,8 +92,39 @@ public: */ virtual void notifyMousePos(Common::Point mouse) = 0; + /** + * A (subset) of the graphic manager's state. This is used when switching + * between different SDL graphic managers on runtime. + */ + struct State { + int screenWidth, screenHeight; + bool aspectRatio; + bool fullscreen; + bool cursorPalette; + +#ifdef USE_RGB_COLOR + Graphics::PixelFormat pixelFormat; +#endif + }; + + /** + * Queries the current state of the graphic manager. + */ + State getState(); + + /** + * Setup a basic state of the graphic manager. + */ + bool setState(const State &state); + + /** + * Queries the SDL window. + */ + SdlWindow *getWindow() const { return _window; } + protected: SdlEventSource *_eventSource; + SdlWindow *_window; }; #endif diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp index 51db48b4c02..fc1e71d2b06 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp @@ -52,9 +52,12 @@ static const OSystem::GraphicsMode s_supportedGraphicsModes[] = { {0, 0, 0} }; -SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSource) +SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSource, SdlWindow *window) : - SdlGraphicsManager(sdlEventSource), + SdlGraphicsManager(sdlEventSource, window), +#if SDL_VERSION_ATLEAST(2, 0, 0) + _renderer(nullptr), _screenTexture(nullptr), +#endif _screen(0), _subScreen(0), _overlayVisible(false), @@ -1120,4 +1123,16 @@ void SurfaceSdlGraphicsManager::notifyMousePos(Common::Point mouse) { //setMousePos(mouse.x, mouse.y); } +#if SDL_VERSION_ATLEAST(2, 0, 0) +void SurfaceSdlGraphicsManager::deinitializeRenderer() { + SDL_DestroyTexture(_screenTexture); + _screenTexture = nullptr; + + SDL_DestroyRenderer(_renderer); + _renderer = nullptr; + + _window->destroyWindow(); +} +#endif // SDL_VERSION_ATLEAST(2, 0, 0) + #endif diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h index 21efca5b239..71d3da20b20 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.h +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h @@ -56,7 +56,7 @@ */ class SurfaceSdlGraphicsManager : public SdlGraphicsManager, public Common::EventObserver { public: - SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSource); + SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSource, SdlWindow *window); virtual ~SurfaceSdlGraphicsManager(); virtual void activateManager(); @@ -133,6 +133,11 @@ public: protected: +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_Renderer *_renderer; + SDL_Texture *_screenTexture; + void deinitializeRenderer(); +#endif SDL_Surface *_screen; SDL_Surface *_subScreen; diff --git a/backends/log/log.cpp b/backends/log/log.cpp index 693399bae5b..e37296aada3 100644 --- a/backends/log/log.cpp +++ b/backends/log/log.cpp @@ -93,10 +93,12 @@ void Log::print(const char *message, const bool printTime) { void Log::printTimeStamp() { TimeDate date; + int curMonth; _system->getTimeAndDate(date); + curMonth = date.tm_mon + 1; // month is base 0, we need base 1 (1 = january and so on) _stream->writeString(Common::String::format("[%d-%02d-%02d %02d:%02d:%02d] ", - date.tm_year + 1900, date.tm_mon, date.tm_mday, + date.tm_year + 1900, curMonth, date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec)); } diff --git a/backends/mixer/doublebuffersdl/doublebuffersdl-mixer.cpp b/backends/mixer/doublebuffersdl/doublebuffersdl-mixer.cpp index d59b0ebdfc7..cf5e59b64fb 100644 --- a/backends/mixer/doublebuffersdl/doublebuffersdl-mixer.cpp +++ b/backends/mixer/doublebuffersdl/doublebuffersdl-mixer.cpp @@ -53,7 +53,11 @@ void DoubleBufferSDLMixerManager::startAudio() { _soundThreadIsRunning = true; // Finally start the thread +#if SDL_VERSION_ATLEAST(2, 0, 0) + _soundThread = SDL_CreateThread(mixerProducerThreadEntry, "ResidualVM Double Buffer Mixer", this); +#else _soundThread = SDL_CreateThread(mixerProducerThreadEntry, this); +#endif SdlMixerManager::startAudio(); } diff --git a/backends/mixer/sdl/sdl-mixer.cpp b/backends/mixer/sdl/sdl-mixer.cpp index e3b15b8c59d..dc0c8538086 100644 --- a/backends/mixer/sdl/sdl-mixer.cpp +++ b/backends/mixer/sdl/sdl-mixer.cpp @@ -57,10 +57,14 @@ void SdlMixerManager::init() { error("Could not initialize SDL: %s", SDL_GetError()); } +#if SDL_VERSION_ATLEAST(2, 0, 0) + const char *sdlDriverName = SDL_GetCurrentAudioDriver(); +#else const int maxNameLen = 20; char sdlDriverName[maxNameLen]; sdlDriverName[0] = '\0'; SDL_AudioDriverName(sdlDriverName, maxNameLen); +#endif debug(1, "Using SDL Audio Driver \"%s\"", sdlDriverName); // Get the desired audio specs diff --git a/backends/module.mk b/backends/module.mk index 6179905c700..cf9b94bfa50 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -66,10 +66,12 @@ MODULE_OBJS += \ # SDL 1.3 removed audio CD support ifndef USE_SDL13 +ifndef USE_SDL2 MODULE_OBJS += \ audiocd/sdl/sdl-audiocd.o endif endif +endif ifdef POSIX MODULE_OBJS += \ diff --git a/backends/platform/sdl/macosx/macosx.cpp b/backends/platform/sdl/macosx/macosx.cpp index 2fcf240a79e..cec5c9799c8 100644 --- a/backends/platform/sdl/macosx/macosx.cpp +++ b/backends/platform/sdl/macosx/macosx.cpp @@ -47,6 +47,9 @@ OSystem_MacOSX::OSystem_MacOSX() } void OSystem_MacOSX::init() { + // Use an iconless window on OS X, as we use a nicer external icon there. + _window = new SdlIconlessWindow(); + #if defined(USE_TASKBAR) // Initialize taskbar manager _taskbarManager = new MacOSXTaskbarManager(); @@ -101,10 +104,6 @@ void OSystem_MacOSX::addSysArchivesToSearchSet(Common::SearchSet &s, int priorit } } -void OSystem_MacOSX::setupIcon() { - // Don't set icon on OS X, as we use a nicer external icon there. -} - bool OSystem_MacOSX::hasFeature(Feature f) { if (f == kFeatureDisplayLogFile) return true; diff --git a/backends/platform/sdl/macosx/macosx.h b/backends/platform/sdl/macosx/macosx.h index 50cef60353d..c8b4beaeeca 100644 --- a/backends/platform/sdl/macosx/macosx.h +++ b/backends/platform/sdl/macosx/macosx.h @@ -38,7 +38,6 @@ public: virtual void init(); virtual void initBackend(); virtual void addSysArchivesToSearchSet(Common::SearchSet &s, int priority = 0); - virtual void setupIcon(); }; #endif diff --git a/backends/platform/sdl/module.mk b/backends/platform/sdl/module.mk index a17a3268890..74dd506d316 100644 --- a/backends/platform/sdl/module.mk +++ b/backends/platform/sdl/module.mk @@ -1,7 +1,8 @@ MODULE := backends/platform/sdl MODULE_OBJS := \ - sdl.o + sdl.o \ + sdl-window.o ifdef POSIX MODULE_OBJS += \ @@ -19,6 +20,7 @@ endif ifdef WIN32 MODULE_OBJS += \ win32/win32-main.o \ + win32/win32-window.o \ win32/win32.o endif diff --git a/backends/platform/sdl/sdl-sys.h b/backends/platform/sdl/sdl-sys.h index eec3741ed68..67ad84efd3a 100644 --- a/backends/platform/sdl/sdl-sys.h +++ b/backends/platform/sdl/sdl-sys.h @@ -52,12 +52,84 @@ typedef struct { int FAKE; } FAKE_FILE; #define strncasecmp FAKE_strncasecmp #endif +// HACK: SDL might include windows.h which defines its own ARRAYSIZE. +// However, we want to use the version from common/util.h. Thus, we make sure +// that we actually have this definition after including the SDL headers. +#if defined(ARRAYSIZE) && defined(COMMON_UTIL_H) +#define HACK_REDEFINE_ARRAYSIZE +#undef ARRAYSIZE +#endif + +// HACK to fix compilation with SDL 2.0 in MSVC. +// In SDL 2.0, intrin.h is now included in SDL_cpuinfo.h, which includes +// setjmp.h. SDL_cpuinfo.h is included from SDL.h and SDL_syswm.h. +// Thus, we remove the exceptions for setjmp and longjmp before these two +// includes. Unfortunately, we can't use SDL_VERSION_ATLEAST here, as SDL.h +// hasn't been included yet at this point. +#if !defined(FORBIDDEN_SYMBOL_ALLOW_ALL) && defined(_MSC_VER) +// We unset any fake definitions of setjmp/longjmp here + +#ifndef FORBIDDEN_SYMBOL_EXCEPTION_setjmp +#undef setjmp +#endif + +#ifndef FORBIDDEN_SYMBOL_EXCEPTION_longjmp +#undef longjmp +#endif + +#endif + #if defined(__SYMBIAN32__) #include #else #include #endif +#include + +// Restore the forbidden exceptions from the hack above +#if !defined(FORBIDDEN_SYMBOL_ALLOW_ALL) && defined(_MSC_VER) + +#ifndef FORBIDDEN_SYMBOL_EXCEPTION_setjmp +#undef setjmp +#define setjmp(a) FORBIDDEN_SYMBOL_REPLACEMENT +#endif + +#ifndef FORBIDDEN_SYMBOL_EXCEPTION_longjmp +#undef longjmp +#define longjmp(a,b) FORBIDDEN_SYMBOL_REPLACEMENT +#endif + +#endif + +// SDL_syswm.h will include windows.h on Win32. We need to undefine its +// ARRAYSIZE definition because we supply our own. +#undef ARRAYSIZE + +#ifdef HACK_REDEFINE_ARRAYSIZE +#undef HACK_REDEFINE_ARRAYSIZE +#define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0]))) +#endif + +// In a moment of brilliance Xlib.h included by SDL_syswm.h #defines the +// following names. In a moment of mental breakdown, which occured upon +// gazing at Xlib.h, LordHoto decided to undefine them to prevent havoc. +#ifdef Status +#undef Status +#endif + +#ifdef Bool +#undef Bool +#endif + +#ifdef True +#undef True +#endif + +#ifdef False +#undef False +#endif + // Finally forbid FILE again (if it was forbidden to start with) #if !defined(FORBIDDEN_SYMBOL_ALLOW_ALL) && !defined(FORBIDDEN_SYMBOL_EXCEPTION_FILE) #undef FILE @@ -74,5 +146,50 @@ typedef struct { int FAKE; } FAKE_FILE; #define strncasecmp FORBIDDEN_SYMBOL_REPLACEMENT #endif +// SDL 2 has major API changes. We redefine constants which got renamed to +// ease the transition. This is sometimes dangerous because the values changed +// too! +#if SDL_VERSION_ATLEAST(2, 0, 0) + +// Type names which changed between SDL 1.2 and SDL 2. +#define SDLKey SDL_Keycode +#define SDLMod SDL_Keymod +#define SDL_keysym SDL_Keysym + +// Key code constants which got renamed. +#define SDLK_SCROLLOCK SDLK_SCROLLLOCK +#define SDLK_NUMLOCK SDLK_NUMLOCKCLEAR +#define SDLK_LSUPER SDLK_LGUI +#define SDLK_RSUPER SDLK_RGUI +#define SDLK_PRINT SDLK_PRINTSCREEN +#define SDLK_COMPOSE SDLK_APPLICATION +#define SDLK_KP0 SDLK_KP_0 +#define SDLK_KP1 SDLK_KP_1 +#define SDLK_KP2 SDLK_KP_2 +#define SDLK_KP3 SDLK_KP_3 +#define SDLK_KP4 SDLK_KP_4 +#define SDLK_KP5 SDLK_KP_5 +#define SDLK_KP6 SDLK_KP_6 +#define SDLK_KP7 SDLK_KP_7 +#define SDLK_KP8 SDLK_KP_8 +#define SDLK_KP9 SDLK_KP_9 + +// Meta key constants which got renamed. +#define KMOD_META KMOD_GUI + +// SDL surface flags which got removed. +#define SDL_SRCCOLORKEY 0 +#define SDL_SRCALPHA 0 +#define SDL_FULLSCREEN 0x40000000 + +// Compatibility implementations for removed functionality. +int SDL_SetColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors); +int SDL_SetAlpha(SDL_Surface *surface, Uint32 flag, Uint8 alpha); + +#define SDL_SetColorKey SDL_SetColorKey_replacement +int SDL_SetColorKey_replacement(SDL_Surface *surface, Uint32 flag, Uint32 key); + +#endif + #endif diff --git a/backends/platform/sdl/sdl-window.cpp b/backends/platform/sdl/sdl-window.cpp new file mode 100644 index 00000000000..10fe63f94fa --- /dev/null +++ b/backends/platform/sdl/sdl-window.cpp @@ -0,0 +1,223 @@ +/* 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. + * + */ + +#define FORBIDDEN_SYMBOL_ALLOW_ALL + +#include "backends/platform/sdl/sdl-window.h" + +#include "common/textconsole.h" + +#include "icons/residualvm.xpm" + +SdlWindow::SdlWindow() +#if SDL_VERSION_ATLEAST(2, 0, 0) + : _window(nullptr), _inputGrabState(false), _windowCaption("ResidualVM") +#endif + { +} + +SdlWindow::~SdlWindow() { +#if SDL_VERSION_ATLEAST(2, 0, 0) + destroyWindow(); +#endif +} + +void SdlWindow::setupIcon() { + int x, y, w, h, ncols, nbytes, i; + unsigned int rgba[256]; + unsigned int *icon; + + if (sscanf(scummvm_icon[0], "%d %d %d %d", &w, &h, &ncols, &nbytes) != 4) { + warning("Wrong format of scummvm_icon[0] (%s)", scummvm_icon[0]); + + return; + } + if ((w > 512) || (h > 512) || (ncols > 255) || (nbytes > 1)) { + warning("Could not load the built-in icon (%d %d %d %d)", w, h, ncols, nbytes); + return; + } + icon = (unsigned int*)malloc(w*h*sizeof(unsigned int)); + if (!icon) { + warning("Could not allocate temp storage for the built-in icon"); + return; + } + + for (i = 0; i < ncols; i++) { + unsigned char code; + char color[32]; + memset(color, 0, sizeof(color)); + unsigned int col; + if (sscanf(scummvm_icon[1 + i], "%c c %s", &code, color) != 2) { + warning("Wrong format of scummvm_icon[%d] (%s)", 1 + i, scummvm_icon[1 + i]); + } + if (!strcmp(color, "None")) + col = 0x00000000; + else if (!strcmp(color, "black")) + col = 0xFF000000; + else if (color[0] == '#') { + if (sscanf(color + 1, "%06x", &col) != 1) { + warning("Wrong format of color (%s)", color + 1); + } + col |= 0xFF000000; + } else { + warning("Could not load the built-in icon (%d %s - %s) ", code, color, scummvm_icon[1 + i]); + free(icon); + return; + } + + rgba[code] = col; + } + for (y = 0; y < h; y++) { + const char *line = scummvm_icon[1 + ncols + y]; + for (x = 0; x < w; x++) { + icon[x + w * y] = rgba[(int)line[x]]; + } + } + + SDL_Surface *sdl_surf = SDL_CreateRGBSurfaceFrom(icon, w, h, 32, w * 4, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF000000); + if (!sdl_surf) { + warning("SDL_CreateRGBSurfaceFrom(icon) failed"); + } + +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (_window) { + SDL_SetWindowIcon(_window, sdl_surf); + } +#else + SDL_WM_SetIcon(sdl_surf, NULL); +#endif + + SDL_FreeSurface(sdl_surf); + free(icon); +} + +void SdlWindow::setWindowCaption(const Common::String &caption) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + _windowCaption = caption; + if (_window) { + SDL_SetWindowTitle(_window, caption.c_str()); + } +#else + SDL_WM_SetCaption(caption.c_str(), caption.c_str()); +#endif +} + +void SdlWindow::toggleMouseGrab() { +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (_window) { + _inputGrabState = !(SDL_GetWindowGrab(_window) == SDL_TRUE); + SDL_SetWindowGrab(_window, _inputGrabState ? SDL_TRUE : SDL_FALSE); + } +#else + if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) { + SDL_WM_GrabInput(SDL_GRAB_ON); + } else { + SDL_WM_GrabInput(SDL_GRAB_OFF); + } +#endif +} + +bool SdlWindow::hasMouseFocus() const { +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (_window) { + return (SDL_GetWindowFlags(_window) & SDL_WINDOW_MOUSE_FOCUS); + } else { + return false; + } +#else + return (SDL_GetAppState() & SDL_APPMOUSEFOCUS); +#endif +} + +void SdlWindow::warpMouseInWindow(uint x, uint y) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (_window) { + SDL_WarpMouseInWindow(_window, x, y); + } +#else + SDL_WarpMouse(x, y); +#endif +} + +void SdlWindow::iconifyWindow() { +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (_window) { + SDL_MinimizeWindow(_window); + } +#else + SDL_WM_IconifyWindow(); +#endif +} + +bool SdlWindow::getSDLWMInformation(SDL_SysWMinfo *info) const { + SDL_VERSION(&info->version); +#if SDL_VERSION_ATLEAST(2, 0, 0) + return SDL_GetWindowWMInfo(_window, info); +#else + return SDL_GetWMInfo(info); +#endif +} + +#if SDL_VERSION_ATLEAST(2, 0, 0) +SDL_Surface *copySDLSurface(SDL_Surface *src) { + const bool locked = SDL_MUSTLOCK(src) == SDL_TRUE; + + if (locked) { + if (SDL_LockSurface(src) != 0) { + return nullptr; + } + } + + SDL_Surface *res = SDL_CreateRGBSurfaceFrom(src->pixels, + src->w, src->h, src->format->BitsPerPixel, + src->pitch, src->format->Rmask, src->format->Gmask, + src->format->Bmask, src->format->Amask); + + if (locked) { + SDL_UnlockSurface(src); + } + + return res; +} + +bool SdlWindow::createWindow(int width, int height, uint32 flags) { + destroyWindow(); + + if (_inputGrabState) { + flags |= SDL_WINDOW_INPUT_GRABBED; + } + + _window = SDL_CreateWindow(_windowCaption.c_str(), SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, width, height, flags); + if (!_window) { + return false; + } + setupIcon(); + + return true; +} + +void SdlWindow::destroyWindow() { + SDL_DestroyWindow(_window); + _window = nullptr; +} +#endif diff --git a/backends/platform/sdl/sdl-window.h b/backends/platform/sdl/sdl-window.h new file mode 100644 index 00000000000..58b898f824a --- /dev/null +++ b/backends/platform/sdl/sdl-window.h @@ -0,0 +1,112 @@ +/* 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. + * + */ + +#ifndef BACKENDS_PLATFORM_SDL_WINDOW_H +#define BACKENDS_PLATFORM_SDL_WINDOW_H + +#include "backends/platform/sdl/sdl-sys.h" + +#include "common/str.h" + +class SdlWindow { +public: + SdlWindow(); + virtual ~SdlWindow(); + + /** + * Setup the window icon. + */ + virtual void setupIcon(); + + /** + * Change the caption of the window. + * + * @param caption New window caption in UTF-8 encoding. + */ + void setWindowCaption(const Common::String &caption); + + /** + * Toggle mouse grab state. This decides whether the cursor can leave the + * window or not. + */ + void toggleMouseGrab(); + + /** + * Check whether the application has mouse focus. + */ + bool hasMouseFocus() const; + + /** + * Warp the mouse to the specified position in window coordinates. + */ + void warpMouseInWindow(uint x, uint y); + + /** + * Iconifies the window. + */ + void iconifyWindow(); + + /** + * Query platform specific SDL window manager information. + * + * Since this is an SDL internal structure clients are responsible + * for accessing it in a version safe manner. + */ + bool getSDLWMInformation(SDL_SysWMinfo *info) const; + +#if SDL_VERSION_ATLEAST(2, 0, 0) +public: + /** + * @return The window ScummVM has setup with SDL. + */ + SDL_Window *getSDLWindow() const { return _window; } + + /** + * Creates a new SDL window (and destroies the old one). + * + * @param width Width of the window. + * @param height Height of the window. + * @param flags SDL flags passed to SDL_CreateWindow + * @return true on success, false otherwise + */ + bool createWindow(int width, int height, uint32 flags); + + /** + * Destroies the current SDL window. + */ + void destroyWindow(); + +protected: + SDL_Window *_window; + +private: + bool _inputGrabState; + Common::String _windowCaption; +#endif +}; + +class SdlIconlessWindow : public SdlWindow { +public: + virtual void setupIcon() {} +}; + +#endif diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp index aa08b8c0f51..8d02420046f 100644 --- a/backends/platform/sdl/sdl.cpp +++ b/backends/platform/sdl/sdl.cpp @@ -49,8 +49,6 @@ #include "backends/timer/sdl/sdl-timer.h" #include "backends/graphics/surfacesdl/surfacesdl-graphics.h" -#include "icons/residualvm.xpm" - #include // for getTimeAndDate() #ifdef USE_DETECTLANG @@ -65,7 +63,8 @@ OSystem_SDL::OSystem_SDL() _initedSDL(false), _logger(0), _mixerManager(0), - _eventSource(0) { + _eventSource(0), + _window(0) { } @@ -83,6 +82,8 @@ OSystem_SDL::~OSystem_SDL() { } delete _graphicsManager; _graphicsManager = 0; + delete _window; + _window = 0; delete _eventManager; _eventManager = 0; delete _eventSource; @@ -114,8 +115,10 @@ void OSystem_SDL::init() { // Initialize SDL initSDL(); +#if !SDL_VERSION_ATLEAST(2, 0, 0) // Enable unicode support if possible SDL_EnableUNICODE(1); +#endif // Disable OS cursor SDL_ShowCursor(SDL_DISABLE); @@ -135,6 +138,9 @@ void OSystem_SDL::init() { if (_mutexManager == 0) _mutexManager = new SdlMutexManager(); + if (_window == 0) + _window = new SdlWindow(); + #if defined(USE_TASKBAR) if (_taskbarManager == 0) _taskbarManager = new Common::TaskbarManager(); @@ -146,10 +152,14 @@ void OSystem_SDL::initBackend() { // Check if backend has not been initialized assert(!_inited); +#if SDL_VERSION_ATLEAST(2, 0, 0) + const char *sdlDriverName = SDL_GetCurrentVideoDriver(); +#else const int maxNameLen = 20; char sdlDriverName[maxNameLen]; sdlDriverName[0] = '\0'; SDL_VideoDriverName(sdlDriverName, maxNameLen); +#endif // Using printf rather than debug() here as debug()/logging // is not active by this point. debug(1, "Using SDL Video Driver \"%s\"", sdlDriverName); @@ -161,7 +171,7 @@ void OSystem_SDL::initBackend() { if (_graphicsManager == 0) { if (_graphicsManager == 0) { - _graphicsManager = new SurfaceSdlGraphicsManager(_eventSource); + _graphicsManager = new SurfaceSdlGraphicsManager(_eventSource, _window); } } @@ -194,7 +204,7 @@ void OSystem_SDL::initBackend() { } // Setup a custom program icon. - setupIcon(); + _window->setupIcon(); _inited = true; @@ -270,7 +280,7 @@ void OSystem_SDL::setWindowCaption(const char *caption) { } } - SDL_WM_SetCaption(cap.c_str(), cap.c_str()); + _window->setWindowCaption(cap); } void OSystem_SDL::quit() { @@ -377,68 +387,6 @@ Common::String OSystem_SDL::getSystemLanguage() const { #endif // USE_DETECTLANG } -void OSystem_SDL::setupIcon() { - int x, y, w, h, ncols, nbytes, i; - unsigned int rgba[256]; - unsigned int *icon; - - if (sscanf(scummvm_icon[0], "%d %d %d %d", &w, &h, &ncols, &nbytes) != 4) { - warning("Wrong format of scummvm_icon[0] (%s)", scummvm_icon[0]); - - return; - } - if ((w > 512) || (h > 512) || (ncols > 255) || (nbytes > 1)) { - warning("Could not load the built-in icon (%d %d %d %d)", w, h, ncols, nbytes); - return; - } - icon = (unsigned int*)malloc(w*h*sizeof(unsigned int)); - if (!icon) { - warning("Could not allocate temp storage for the built-in icon"); - return; - } - - for (i = 0; i < ncols; i++) { - unsigned char code; - char color[32]; - memset(color, 0, sizeof(color)); - unsigned int col; - if (sscanf(scummvm_icon[1 + i], "%c c %s", &code, color) != 2) { - warning("Wrong format of scummvm_icon[%d] (%s)", 1 + i, scummvm_icon[1 + i]); - } - if (!strcmp(color, "None")) - col = 0x00000000; - else if (!strcmp(color, "black")) - col = 0xFF000000; - else if (color[0] == '#') { - if (sscanf(color + 1, "%06x", &col) != 1) { - warning("Wrong format of color (%s)", color + 1); - } - col |= 0xFF000000; - } else { - warning("Could not load the built-in icon (%d %s - %s) ", code, color, scummvm_icon[1 + i]); - free(icon); - return; - } - - rgba[code] = col; - } - for (y = 0; y < h; y++) { - const char *line = scummvm_icon[1 + ncols + y]; - for (x = 0; x < w; x++) { - icon[x + w * y] = rgba[(int)line[x]]; - } - } - - SDL_Surface *sdl_surf = SDL_CreateRGBSurfaceFrom(icon, w, h, 32, w * 4, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF000000); - if (!sdl_surf) { - warning("SDL_CreateRGBSurfaceFrom(icon) failed"); - } - SDL_WM_SetIcon(sdl_surf, NULL); - SDL_FreeSurface(sdl_surf); - free(icon); -} - - uint32 OSystem_SDL::getMillis(bool skipRecord) { uint32 millis = SDL_GetTicks(); diff --git a/backends/platform/sdl/sdl.h b/backends/platform/sdl/sdl.h index ddbb3aac2eb..9d6fbe006d5 100644 --- a/backends/platform/sdl/sdl.h +++ b/backends/platform/sdl/sdl.h @@ -29,6 +29,7 @@ #include "backends/mixer/sdl/sdl-mixer.h" #include "backends/events/sdl/sdl-events.h" #include "backends/log/log.h" +#include "backends/platform/sdl/sdl-window.h" #include "common/array.h" @@ -91,6 +92,11 @@ protected: */ SdlEventSource *_eventSource; + /** + * The SDL output window. + */ + SdlWindow *_window; + virtual Common::EventSource *getDefaultEventSource() { return _eventSource; } /** @@ -98,11 +104,6 @@ protected: */ virtual void initSDL(); - /** - * Setup the window icon. - */ - virtual void setupIcon(); - // Logging virtual Common::WriteStream *createLogFile() { return 0; } Backends::Log::Log *_logger; diff --git a/backends/platform/sdl/win32/win32-main.cpp b/backends/platform/sdl/win32/win32-main.cpp index e5b26c3ff02..c6c15c00e8a 100644 --- a/backends/platform/sdl/win32/win32-main.cpp +++ b/backends/platform/sdl/win32/win32-main.cpp @@ -40,7 +40,9 @@ #include "base/main.h" int __stdcall WinMain(HINSTANCE /*hInst*/, HINSTANCE /*hPrevInst*/, LPSTR /*lpCmdLine*/, int /*iShowCmd*/) { +#if !SDL_VERSION_ATLEAST(2, 0, 0) SDL_SetModuleHandle(GetModuleHandle(NULL)); +#endif return main(__argc, __argv); } diff --git a/backends/platform/sdl/win32/win32-window.cpp b/backends/platform/sdl/win32/win32-window.cpp new file mode 100644 index 00000000000..de10be6b579 --- /dev/null +++ b/backends/platform/sdl/win32/win32-window.cpp @@ -0,0 +1,59 @@ +/* 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. + * + */ + +// Disable symbol overrides so that we can use system headers. +#define FORBIDDEN_SYMBOL_ALLOW_ALL + +#ifdef WIN32 + +#include "backends/platform/sdl/win32/win32-window.h" + +#define WIN32_LEAN_AND_MEAN +#include +#undef ARRAYSIZE // winnt.h defines ARRAYSIZE, but we want our own one... + +void SdlWindow_Win32::setupIcon() { + HMODULE handle = GetModuleHandle(NULL); + HICON ico = LoadIcon(handle, MAKEINTRESOURCE(1001 /* IDI_ICON */)); + if (ico) { + SDL_SysWMinfo wminfo; + if (getSDLWMInformation(&wminfo)) { + // Replace the handle to the icon associated with the window class by our custom icon +#if SDL_VERSION_ATLEAST(2, 0, 0) + SetClassLongPtr(wminfo.info.win.window, GCLP_HICON, (ULONG_PTR)ico); +#else + SetClassLongPtr(wminfo.window, GCLP_HICON, (ULONG_PTR)ico); +#endif + + // Since there wasn't any default icon, we can't use the return value from SetClassLong + // to check for errors (it would be 0 in both cases: error or no previous value for the + // icon handle). Instead we check for the last-error code value. + if (GetLastError() == ERROR_SUCCESS) + return; + } + } + + // If no icon has been set, fallback to default path + SdlWindow::setupIcon(); +} + +#endif diff --git a/backends/platform/sdl/win32/win32-window.h b/backends/platform/sdl/win32/win32-window.h new file mode 100644 index 00000000000..3bda697bc7a --- /dev/null +++ b/backends/platform/sdl/win32/win32-window.h @@ -0,0 +1,37 @@ +/* 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. + * + */ + +#ifndef BACKENDS_PLATFORM_SDL_WIN32_WIN32_WINDOW_H +#define BACKENDS_PLATFORM_SDL_WIN32_WIN32_WINDOW_H + +#ifdef WIN32 + +#include "backends/platform/sdl/sdl-window.h" + +class SdlWindow_Win32 : public SdlWindow { +public: + virtual void setupIcon(); +}; + +#endif + +#endif diff --git a/backends/platform/sdl/win32/win32.cpp b/backends/platform/sdl/win32/win32.cpp index 1875f237885..85bfaf23f21 100644 --- a/backends/platform/sdl/win32/win32.cpp +++ b/backends/platform/sdl/win32/win32.cpp @@ -35,9 +35,8 @@ #include "common/error.h" #include "common/textconsole.h" -#include // For setting the icon - #include "backends/platform/sdl/win32/win32.h" +#include "backends/platform/sdl/win32/win32-window.h" #include "backends/saves/windows/windows-saves.h" #include "backends/fs/windows/windows-fs-factory.h" #include "backends/taskbar/win32/win32-taskbar.h" @@ -50,9 +49,12 @@ void OSystem_Win32::init() { // Initialize File System Factory _fsFactory = new WindowsFilesystemFactory(); + // Create Win32 specific window + _window = new SdlWindow_Win32(); + #if defined(USE_TASKBAR) // Initialize taskbar manager - _taskbarManager = new Win32TaskbarManager(); + _taskbarManager = new Win32TaskbarManager(_window); #endif // Invoke parent implementation of this method @@ -126,28 +128,6 @@ bool OSystem_Win32::displayLogFile() { return false; } -void OSystem_Win32::setupIcon() { - HMODULE handle = GetModuleHandle(NULL); - HICON ico = LoadIcon(handle, MAKEINTRESOURCE(1001 /* IDI_ICON */)); - if (ico) { - SDL_SysWMinfo wminfo; - SDL_VERSION(&wminfo.version); - if (SDL_GetWMInfo(&wminfo)) { - // Replace the handle to the icon associated with the window class by our custom icon - SetClassLongPtr(wminfo.window, GCLP_HICON, (ULONG_PTR)ico); - - // Since there wasn't any default icon, we can't use the return value from SetClassLong - // to check for errors (it would be 0 in both cases: error or no previous value for the - // icon handle). Instead we check for the last-error code value. - if (GetLastError() == ERROR_SUCCESS) - return; - } - } - - // If no icon has been set, fallback to default path - OSystem_SDL::setupIcon(); -} - Common::String OSystem_Win32::getDefaultConfigFileName() { char configFile[MAXPATHLEN]; diff --git a/backends/platform/sdl/win32/win32.h b/backends/platform/sdl/win32/win32.h index d72d80bc268..473e78ff0b1 100644 --- a/backends/platform/sdl/win32/win32.h +++ b/backends/platform/sdl/win32/win32.h @@ -47,7 +47,6 @@ protected: */ Common::String _logFilePath; - virtual void setupIcon(); virtual Common::String getDefaultConfigFileName(); virtual Common::WriteStream *createLogFile(); }; diff --git a/backends/taskbar/win32/win32-taskbar.cpp b/backends/taskbar/win32/win32-taskbar.cpp index 5c9105b0eb6..d45253c6767 100644 --- a/backends/taskbar/win32/win32-taskbar.cpp +++ b/backends/taskbar/win32/win32-taskbar.cpp @@ -48,9 +48,6 @@ #include -// For HWND -#include - #include "common/scummsys.h" #include "backends/taskbar/win32/win32-taskbar.h" @@ -62,7 +59,7 @@ // System.Title property key, values taken from http://msdn.microsoft.com/en-us/library/bb787584.aspx const PROPERTYKEY PKEY_Title = { /* fmtid = */ { 0xF29F85E0, 0x4FF9, 0x1068, { 0xAB, 0x91, 0x08, 0x00, 0x2B, 0x27, 0xB3, 0xD9 } }, /* propID = */ 2 }; -Win32TaskbarManager::Win32TaskbarManager() : _taskbar(NULL), _count(0), _icon(NULL) { +Win32TaskbarManager::Win32TaskbarManager(SdlWindow *window) : _window(window), _taskbar(NULL), _count(0), _icon(NULL) { // Do nothing if not running on Windows 7 or later if (!isWin7OrLater()) return; @@ -408,12 +405,15 @@ LPWSTR Win32TaskbarManager::ansiToUnicode(const char *s) { HWND Win32TaskbarManager::getHwnd() { SDL_SysWMinfo wmi; - SDL_VERSION(&wmi.version); - - if(!SDL_GetWMInfo(&wmi)) + if (_window->getSDLWMInformation(&wmi)) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + return wmi.info.win.window; +#else + return wmi.window; +#endif + } else { return NULL; - - return wmi.window; + } } #endif diff --git a/backends/taskbar/win32/win32-taskbar.h b/backends/taskbar/win32/win32-taskbar.h index 36415c1c577..a6d1b492139 100644 --- a/backends/taskbar/win32/win32-taskbar.h +++ b/backends/taskbar/win32/win32-taskbar.h @@ -25,6 +25,8 @@ #if defined(WIN32) && defined(USE_TASKBAR) +#include "backends/platform/sdl/sdl-window.h" + #include "common/str.h" #include "common/taskbar.h" @@ -32,7 +34,7 @@ struct ITaskbarList3; class Win32TaskbarManager : public Common::TaskbarManager { public: - Win32TaskbarManager(); + Win32TaskbarManager(SdlWindow *window); virtual ~Win32TaskbarManager(); virtual void setOverlayIcon(const Common::String &name, const Common::String &description); @@ -44,6 +46,8 @@ public: virtual void clearError(); private: + SdlWindow *_window; + ITaskbarList3 *_taskbar; // Count handling diff --git a/base/main.cpp b/base/main.cpp index 2ab6ffce6b0..aa8344558ef 100644 --- a/base/main.cpp +++ b/base/main.cpp @@ -540,6 +540,9 @@ extern "C" int scummvm_main(int argc, const char * const argv[]) { PluginManager::instance().loadAllPlugins(); // only for cached manager } else { GUI::displayErrorDialog(_("Could not find any engine capable of running the selected game")); + + // Clear the active domain + ConfMan.setActiveDomain(""); } // reset the graphics to default diff --git a/common/fft.cpp b/common/fft.cpp index ac7386083f7..477f7aca627 100644 --- a/common/fft.cpp +++ b/common/fft.cpp @@ -61,6 +61,10 @@ FFT::~FFT() { delete[] _tmpBuf; } +const uint16 *FFT::getRevTab() const { + return _revTab; +} + void FFT::permute(Complex *z) { int np = 1 << _bits; diff --git a/common/fft.h b/common/fft.h index 6eb72c3f846..ed66d32b717 100644 --- a/common/fft.h +++ b/common/fft.h @@ -47,6 +47,8 @@ public: FFT(int bits, int inverse); ~FFT(); + const uint16 *getRevTab() const; + /** Do the permutation needed BEFORE calling calc(). */ void permute(Complex *z); diff --git a/configure b/configure index a7cc8234393..0333303f591 100755 --- a/configure +++ b/configure @@ -35,6 +35,7 @@ SAVED_CXXFLAGS=$CXXFLAGS SAVED_CPPFLAGS=$CPPFLAGS SAVED_ASFLAGS=$ASFLAGS SAVED_WINDRESFLAGS=$WINDRESFLAGS +SAVED_SDL_CONFIG=$SDL_CONFIG # Use environment vars if set CXXFLAGS="$CXXFLAGS $CPPFLAGS" @@ -361,7 +362,7 @@ define_in_config_if_yes() { # TODO: small bit of code to test sdl usability find_sdlconfig() { echo_n "Looking for sdl-config... " - sdlconfigs="$_sdlconfig:sdl-config:sdl11-config:sdl12-config" + sdlconfigs="$SDL_CONFIG:$_sdlconfig:sdl-config:sdl11-config:sdl12-config" _sdlconfig= IFS="${IFS= }"; ac_save_ifs="$IFS"; IFS="$SEPARATOR" @@ -2193,6 +2194,8 @@ case $_host_os in DEFINES="$DEFINES -DMACOSX" LIBS="$LIBS -framework AudioUnit -framework AudioToolbox -framework Carbon -framework CoreMIDI" + # SDL2 doesn't seem to add Cocoa for us. + LIBS="$LIBS -framework Cocoa" add_line_to_config_mk 'MACOSX = 1' # Now we may have MacPorts or Fink installed @@ -3068,6 +3071,9 @@ case $_backend in 1.3.*) add_line_to_config_mk "USE_SDL13 = 1" ;; + 2.0.*) + add_line_to_config_mk "USE_SDL2 = 1" + ;; *) ;; esac @@ -4742,6 +4748,7 @@ SAVED_CXXFLAGS := $SAVED_CXXFLAGS SAVED_CPPFLAGS := $SAVED_CPPFLAGS SAVED_ASFLAGS := $SAVED_ASFLAGS SAVED_WINDRESFLAGS := $SAVED_WINDRESFLAGS +SAVED_SDL_CONFIG := $SAVED_SDL_CONFIG EOF # diff --git a/devtools/create_project/create_project.cpp b/devtools/create_project/create_project.cpp index 0f2e1917751..55669fd9f9c 100644 --- a/devtools/create_project/create_project.cpp +++ b/devtools/create_project/create_project.cpp @@ -125,6 +125,7 @@ int main(int argc, char *argv[]) { ProjectType projectType = kProjectNone; int msvcVersion = 9; + bool useSDL2 = false; // Parse command line arguments using std::cout; @@ -267,6 +268,8 @@ int main(int argc, char *argv[]) { setup.devTools = true; } else if (!std::strcmp(argv[i], "--tests")) { setup.tests = true; + } else if (!std::strcmp(argv[i], "--sdl2")) { + useSDL2 = true; } else { std::cerr << "ERROR: Unknown parameter \"" << argv[i] << "\"\n"; return -1; @@ -335,7 +338,13 @@ int main(int argc, char *argv[]) { // Windows only has support for the SDL backend, so we hardcode it here (along with winmm) setup.defines.push_back("WIN32"); setup.defines.push_back("SDL_BACKEND"); - setup.libraries.push_back("sdl"); + if (!useSDL2) { + cout << "\nLinking to SDL 1.2\n\n"; + setup.libraries.push_back("sdl"); + } else { + cout << "\nLinking to SDL 2.0\n\n"; + setup.libraries.push_back("sdl2"); + } setup.libraries.push_back("winmm"); // Add additional project-specific library @@ -645,6 +654,9 @@ void displayHelp(const char *exe) { "Optional features settings:\n" " --enable- enable inclusion of the feature \"name\"\n" " --disable- disable inclusion of the feature \"name\"\n" + "\n" + "SDL settings:\n" + " --sdl2 link to SDL 2.0, instead of SDL 1.2\n" "\n" " There are the following features available:\n" "\n"; diff --git a/dists/codeblocks/readme.txt b/dists/codeblocks/readme.txt index b0456aee139..1be60262c61 100644 --- a/dists/codeblocks/readme.txt +++ b/dists/codeblocks/readme.txt @@ -1,5 +1,5 @@ The Code::Blocks project files can now be created automatically from the GCC -files using the create_project tool inside the /tools/create_project folder. +files using the create_project tool inside the /devtools/create_project folder. To create the default project files, build create_project.exe, copy it inside this folder and run the create_codeblocks.bat file for a default build. You can diff --git a/graphics/fonts/ttf.cpp b/graphics/fonts/ttf.cpp index ba576136014..dc7335f1c28 100644 --- a/graphics/fonts/ttf.cpp +++ b/graphics/fonts/ttf.cpp @@ -474,11 +474,11 @@ bool TTFFont::cacheGlyph(Glyph &glyph, uint32 chr) const { switch (bitmap.pixel_mode) { case FT_PIXEL_MODE_MONO: - for (int y = 0; y < bitmap.rows; ++y) { + for (int y = 0; y < (int)bitmap.rows; ++y) { const uint8 *curSrc = src; uint8 mask = 0; - for (int x = 0; x < bitmap.width; ++x) { + for (int x = 0; x < (int)bitmap.width; ++x) { if ((x % 8) == 0) mask = *curSrc++; @@ -494,7 +494,7 @@ bool TTFFont::cacheGlyph(Glyph &glyph, uint32 chr) const { break; case FT_PIXEL_MODE_GRAY: - for (int y = 0; y < bitmap.rows; ++y) { + for (int y = 0; y < (int)bitmap.rows; ++y) { memcpy(dst, src, bitmap.width); dst += glyph.image.pitch; src += srcPitch; diff --git a/gui/Tooltip.cpp b/gui/Tooltip.cpp index e5f06bcafec..ba313ee34fd 100644 --- a/gui/Tooltip.cpp +++ b/gui/Tooltip.cpp @@ -40,6 +40,8 @@ Tooltip::Tooltip() : void Tooltip::setup(Dialog *parent, Widget *widget, int x, int y) { assert(widget->hasTooltip()); + _parent = parent; + _maxWidth = g_gui.xmlEval()->getVar("Globals.Tooltip.MaxWidth", 100); _xdelta = g_gui.xmlEval()->getVar("Globals.Tooltip.XDelta", 0); _ydelta = g_gui.xmlEval()->getVar("Globals.Tooltip.YDelta", 0); diff --git a/gui/Tooltip.h b/gui/Tooltip.h index f83fc409663..58b6d8a4298 100644 --- a/gui/Tooltip.h +++ b/gui/Tooltip.h @@ -32,6 +32,9 @@ namespace GUI { class Widget; class Tooltip : public Dialog { +private: + Dialog *_parent; + public: Tooltip(); @@ -39,12 +42,30 @@ public: void drawDialog(); protected: - virtual void handleMouseDown(int x, int y, int button, int clickCount) { close(); } - virtual void handleMouseUp(int x, int y, int button, int clickCount) { close(); } - virtual void handleMouseWheel(int x, int y, int direction) { close(); } - virtual void handleKeyDown(Common::KeyState state) { close(); } - virtual void handleKeyUp(Common::KeyState state) { close(); } - virtual void handleMouseMoved(int x, int y, int button) { close(); } + virtual void handleMouseDown(int x, int y, int button, int clickCount) { + close(); + _parent->handleMouseDown(x + (getAbsX() - _parent->getAbsX()), y + (getAbsY() - _parent->getAbsY()), button, clickCount); + } + virtual void handleMouseUp(int x, int y, int button, int clickCount) { + close(); + _parent->handleMouseUp(x + (getAbsX() - _parent->getAbsX()), y + (getAbsY() - _parent->getAbsY()), button, clickCount); + } + virtual void handleMouseWheel(int x, int y, int direction) { + close(); + _parent->handleMouseWheel(x + (getAbsX() - _parent->getAbsX()), y + (getAbsX() - _parent->getAbsX()), direction); + } + virtual void handleKeyDown(Common::KeyState state) { + close(); + _parent->handleKeyDown(state); + } + virtual void handleKeyUp(Common::KeyState state) { + close(); + _parent->handleKeyUp(state); + } + virtual void handleMouseMoved(int x, int y, int button) { + close(); + _parent->handleMouseMoved(x + (getAbsX() - _parent->getAbsX()), y + (getAbsY() - _parent->getAbsY()), button); + } int _maxWidth; int _xdelta, _ydelta; diff --git a/gui/dialog.cpp b/gui/dialog.cpp index fa4e5084947..315c24e9bf1 100644 --- a/gui/dialog.cpp +++ b/gui/dialog.cpp @@ -219,7 +219,7 @@ void Dialog::handleMouseWheel(int x, int y, int direction) { if (!w) w = _focusedWidget; if (w) - w->handleMouseWheel(x, y, direction); + w->handleMouseWheel(x - (w->getAbsX() - _x), y - (w->getAbsY() - _y), direction); } void Dialog::handleKeyDown(Common::KeyState state) { diff --git a/image/codecs/cinepak.cpp b/image/codecs/cinepak.cpp index 8464aa3889a..32f6be2cd54 100644 --- a/image/codecs/cinepak.cpp +++ b/image/codecs/cinepak.cpp @@ -21,6 +21,7 @@ */ #include "image/codecs/cinepak.h" +#include "image/codecs/cinepak_tables.h" #include "common/debug.h" #include "common/stream.h" @@ -34,23 +35,328 @@ namespace Image { -#define PUT_PIXEL(offset, lum, u, v) \ - if (_pixelFormat.bytesPerPixel != 1) { \ - byte r = _clipTable[lum + (v << 1)]; \ - byte g = _clipTable[lum - (u >> 1) - v]; \ - byte b = _clipTable[lum + (u << 1)]; \ - \ - if (_pixelFormat.bytesPerPixel == 2) \ - *((uint16 *)_curFrame.surface->getPixels() + offset) = _pixelFormat.RGBToColor(r, g, b); \ - else \ - *((uint32 *)_curFrame.surface->getPixels() + offset) = _pixelFormat.RGBToColor(r, g, b); \ - } else \ - *((byte *)_curFrame.surface->getPixels() + offset) = lum +namespace { -CinepakDecoder::CinepakDecoder(int bitsPerPixel) : Codec() { - _curFrame.surface = NULL; - _curFrame.strips = NULL; +inline void convertYUVToRGB(const byte *clipTable, byte y, int8 u, int8 v, byte &r, byte &g, byte &b) { + r = clipTable[y + (v << 1)]; + g = clipTable[y - (u >> 1) - v]; + b = clipTable[y + (u << 1)]; +} + +inline uint32 convertYUVToColor(const byte *clipTable, const Graphics::PixelFormat &format, byte y, byte u, byte v) { + byte r, g, b; + convertYUVToRGB(clipTable, y, u, v, r, g, b); + return format.RGBToColor(r, g, b); +} + +inline uint16 createDitherTableIndex(const byte *clipTable, byte y, int8 u, int8 v) { + byte r, g, b; + convertYUVToRGB(clipTable, y, u, v, r, g, b); + return ((r & 0xF8) << 6) | + ((g & 0xF8) << 1) | + ((b & 0xF0) >> 4); +} + +/** + * Put a raw pixel to the destination surface + */ +template +inline void putPixelRaw(PixelInt *dst, const byte *clipTable, const Graphics::PixelFormat &format, byte y, byte u, byte v) { + *dst = convertYUVToColor(clipTable, format, y, u, v); +} + +/** + * Specialized putPixelRaw for palettized 8bpp output + */ +template<> +inline void putPixelRaw(byte *dst, const byte *clipTable, const Graphics::PixelFormat &format, byte y, byte u, byte v) { + *dst = y; +} + +/** + * The default codebook converter: raw output. + */ +struct CodebookConverterRaw { + template + static inline void decodeBlock1(byte codebookIndex, const CinepakStrip &strip, PixelInt *(&rows)[4], const byte *clipTable, const byte *colorMap, const Graphics::PixelFormat &format) { + const CinepakCodebook &codebook = strip.v1_codebook[codebookIndex]; + putPixelRaw(rows[0] + 0, clipTable, format, codebook.y[0], codebook.u, codebook.v); + putPixelRaw(rows[0] + 1, clipTable, format, codebook.y[0], codebook.u, codebook.v); + putPixelRaw(rows[1] + 0, clipTable, format, codebook.y[0], codebook.u, codebook.v); + putPixelRaw(rows[1] + 1, clipTable, format, codebook.y[0], codebook.u, codebook.v); + + putPixelRaw(rows[0] + 2, clipTable, format, codebook.y[1], codebook.u, codebook.v); + putPixelRaw(rows[0] + 3, clipTable, format, codebook.y[1], codebook.u, codebook.v); + putPixelRaw(rows[1] + 2, clipTable, format, codebook.y[1], codebook.u, codebook.v); + putPixelRaw(rows[1] + 3, clipTable, format, codebook.y[1], codebook.u, codebook.v); + + putPixelRaw(rows[2] + 0, clipTable, format, codebook.y[2], codebook.u, codebook.v); + putPixelRaw(rows[2] + 1, clipTable, format, codebook.y[2], codebook.u, codebook.v); + putPixelRaw(rows[3] + 0, clipTable, format, codebook.y[2], codebook.u, codebook.v); + putPixelRaw(rows[3] + 1, clipTable, format, codebook.y[2], codebook.u, codebook.v); + + putPixelRaw(rows[2] + 2, clipTable, format, codebook.y[3], codebook.u, codebook.v); + putPixelRaw(rows[2] + 3, clipTable, format, codebook.y[3], codebook.u, codebook.v); + putPixelRaw(rows[3] + 2, clipTable, format, codebook.y[3], codebook.u, codebook.v); + putPixelRaw(rows[3] + 3, clipTable, format, codebook.y[3], codebook.u, codebook.v); + } + + template + static inline void decodeBlock4(const byte (&codebookIndex)[4], const CinepakStrip &strip, PixelInt *(&rows)[4], const byte *clipTable, const byte *colorMap, const Graphics::PixelFormat &format) { + const CinepakCodebook &codebook1 = strip.v4_codebook[codebookIndex[0]]; + putPixelRaw(rows[0] + 0, clipTable, format, codebook1.y[0], codebook1.u, codebook1.v); + putPixelRaw(rows[0] + 1, clipTable, format, codebook1.y[1], codebook1.u, codebook1.v); + putPixelRaw(rows[1] + 0, clipTable, format, codebook1.y[2], codebook1.u, codebook1.v); + putPixelRaw(rows[1] + 1, clipTable, format, codebook1.y[3], codebook1.u, codebook1.v); + + const CinepakCodebook &codebook2 = strip.v4_codebook[codebookIndex[1]]; + putPixelRaw(rows[0] + 2, clipTable, format, codebook2.y[0], codebook2.u, codebook2.v); + putPixelRaw(rows[0] + 3, clipTable, format, codebook2.y[1], codebook2.u, codebook2.v); + putPixelRaw(rows[1] + 2, clipTable, format, codebook2.y[2], codebook2.u, codebook2.v); + putPixelRaw(rows[1] + 3, clipTable, format, codebook2.y[3], codebook2.u, codebook2.v); + + const CinepakCodebook &codebook3 = strip.v4_codebook[codebookIndex[2]]; + putPixelRaw(rows[2] + 0, clipTable, format, codebook3.y[0], codebook3.u, codebook3.v); + putPixelRaw(rows[2] + 1, clipTable, format, codebook3.y[1], codebook3.u, codebook3.v); + putPixelRaw(rows[3] + 0, clipTable, format, codebook3.y[2], codebook3.u, codebook3.v); + putPixelRaw(rows[3] + 1, clipTable, format, codebook3.y[3], codebook3.u, codebook3.v); + + const CinepakCodebook &codebook4 = strip.v4_codebook[codebookIndex[3]]; + putPixelRaw(rows[2] + 2, clipTable, format, codebook4.y[0], codebook4.u, codebook4.v); + putPixelRaw(rows[2] + 3, clipTable, format, codebook4.y[1], codebook4.u, codebook4.v); + putPixelRaw(rows[3] + 2, clipTable, format, codebook4.y[2], codebook4.u, codebook4.v); + putPixelRaw(rows[3] + 3, clipTable, format, codebook4.y[3], codebook4.u, codebook4.v); + } +}; + +/** + * Codebook converter that dithers in VFW-style + */ +struct CodebookConverterDitherVFW { + static inline void decodeBlock1(byte codebookIndex, const CinepakStrip &strip, byte *(&rows)[4], const byte *clipTable, const byte *colorMap, const Graphics::PixelFormat &format) { + const CinepakCodebook &codebook = strip.v1_codebook[codebookIndex]; + byte blockBuffer[16]; + ditherCodebookSmooth(codebook, blockBuffer, colorMap); + rows[0][0] = blockBuffer[0]; + rows[0][1] = blockBuffer[1]; + rows[0][2] = blockBuffer[2]; + rows[0][3] = blockBuffer[3]; + rows[1][0] = blockBuffer[4]; + rows[1][1] = blockBuffer[5]; + rows[1][2] = blockBuffer[6]; + rows[1][3] = blockBuffer[7]; + rows[2][0] = blockBuffer[8]; + rows[2][1] = blockBuffer[9]; + rows[2][2] = blockBuffer[10]; + rows[2][3] = blockBuffer[11]; + rows[3][0] = blockBuffer[12]; + rows[3][1] = blockBuffer[13]; + rows[3][2] = blockBuffer[14]; + rows[3][3] = blockBuffer[15]; + } + + static inline void decodeBlock4(const byte (&codebookIndex)[4], const CinepakStrip &strip, byte *(&rows)[4], const byte *clipTable, const byte *colorMap, const Graphics::PixelFormat &format) { + byte blockBuffer[16]; + ditherCodebookDetail(strip.v4_codebook[codebookIndex[0]], blockBuffer, colorMap); + rows[0][0] = blockBuffer[0]; + rows[0][1] = blockBuffer[1]; + rows[1][0] = blockBuffer[4]; + rows[1][1] = blockBuffer[5]; + + ditherCodebookDetail(strip.v4_codebook[codebookIndex[1]], blockBuffer, colorMap); + rows[0][2] = blockBuffer[2]; + rows[0][3] = blockBuffer[3]; + rows[1][2] = blockBuffer[6]; + rows[1][3] = blockBuffer[7]; + + ditherCodebookDetail(strip.v4_codebook[codebookIndex[2]], blockBuffer, colorMap); + rows[2][0] = blockBuffer[8]; + rows[2][1] = blockBuffer[9]; + rows[3][0] = blockBuffer[12]; + rows[3][1] = blockBuffer[13]; + + ditherCodebookDetail(strip.v4_codebook[codebookIndex[3]], blockBuffer, colorMap); + rows[2][2] = blockBuffer[10]; + rows[2][3] = blockBuffer[11]; + rows[3][2] = blockBuffer[14]; + rows[3][3] = blockBuffer[15]; + } + +private: + static inline void ditherCodebookDetail(const CinepakCodebook &codebook, byte *dst, const byte *colorMap) { + int uLookup = (byte)codebook.u * 2; + int vLookup = (byte)codebook.v * 2; + uint32 uv1 = s_uLookup[uLookup] | s_vLookup[vLookup]; + uint32 uv2 = s_uLookup[uLookup + 1] | s_vLookup[vLookup + 1]; + + int yLookup1 = codebook.y[0] * 2; + int yLookup2 = codebook.y[1] * 2; + int yLookup3 = codebook.y[2] * 2; + int yLookup4 = codebook.y[3] * 2; + + uint32 pixelGroup1 = uv2 | s_yLookup[yLookup1 + 1]; + uint32 pixelGroup2 = uv2 | s_yLookup[yLookup2 + 1]; + uint32 pixelGroup3 = uv1 | s_yLookup[yLookup3]; + uint32 pixelGroup4 = uv1 | s_yLookup[yLookup4]; + uint32 pixelGroup5 = uv1 | s_yLookup[yLookup1]; + uint32 pixelGroup6 = uv1 | s_yLookup[yLookup2]; + uint32 pixelGroup7 = uv2 | s_yLookup[yLookup3 + 1]; + uint32 pixelGroup8 = uv2 | s_yLookup[yLookup4 + 1]; + + dst[0] = getRGBLookupEntry(colorMap, pixelGroup1 & 0xFFFF); + dst[1] = getRGBLookupEntry(colorMap, pixelGroup2 >> 16); + dst[2] = getRGBLookupEntry(colorMap, pixelGroup5 & 0xFFFF); + dst[3] = getRGBLookupEntry(colorMap, pixelGroup6 >> 16); + dst[4] = getRGBLookupEntry(colorMap, pixelGroup3 & 0xFFFF); + dst[5] = getRGBLookupEntry(colorMap, pixelGroup4 >> 16); + dst[6] = getRGBLookupEntry(colorMap, pixelGroup7 & 0xFFFF); + dst[7] = getRGBLookupEntry(colorMap, pixelGroup8 >> 16); + dst[8] = getRGBLookupEntry(colorMap, pixelGroup1 >> 16); + dst[9] = getRGBLookupEntry(colorMap, pixelGroup6 & 0xFFFF); + dst[10] = getRGBLookupEntry(colorMap, pixelGroup5 >> 16); + dst[11] = getRGBLookupEntry(colorMap, pixelGroup2 & 0xFFFF); + dst[12] = getRGBLookupEntry(colorMap, pixelGroup3 >> 16); + dst[13] = getRGBLookupEntry(colorMap, pixelGroup8 & 0xFFFF); + dst[14] = getRGBLookupEntry(colorMap, pixelGroup7 >> 16); + dst[15] = getRGBLookupEntry(colorMap, pixelGroup4 & 0xFFFF); + } + + static inline void ditherCodebookSmooth(const CinepakCodebook &codebook, byte *dst, const byte *colorMap) { + int uLookup = (byte)codebook.u * 2; + int vLookup = (byte)codebook.v * 2; + uint32 uv1 = s_uLookup[uLookup] | s_vLookup[vLookup]; + uint32 uv2 = s_uLookup[uLookup + 1] | s_vLookup[vLookup + 1]; + + int yLookup1 = codebook.y[0] * 2; + int yLookup2 = codebook.y[1] * 2; + int yLookup3 = codebook.y[2] * 2; + int yLookup4 = codebook.y[3] * 2; + + uint32 pixelGroup1 = uv2 | s_yLookup[yLookup1 + 1]; + uint32 pixelGroup2 = uv1 | s_yLookup[yLookup2]; + uint32 pixelGroup3 = uv1 | s_yLookup[yLookup1]; + uint32 pixelGroup4 = uv2 | s_yLookup[yLookup2 + 1]; + uint32 pixelGroup5 = uv2 | s_yLookup[yLookup3 + 1]; + uint32 pixelGroup6 = uv1 | s_yLookup[yLookup3]; + uint32 pixelGroup7 = uv1 | s_yLookup[yLookup4]; + uint32 pixelGroup8 = uv2 | s_yLookup[yLookup4 + 1]; + + dst[0] = getRGBLookupEntry(colorMap, pixelGroup1 & 0xFFFF); + dst[1] = getRGBLookupEntry(colorMap, pixelGroup1 >> 16); + dst[2] = getRGBLookupEntry(colorMap, pixelGroup2 & 0xFFFF); + dst[3] = getRGBLookupEntry(colorMap, pixelGroup2 >> 16); + dst[4] = getRGBLookupEntry(colorMap, pixelGroup3 & 0xFFFF); + dst[5] = getRGBLookupEntry(colorMap, pixelGroup3 >> 16); + dst[6] = getRGBLookupEntry(colorMap, pixelGroup4 & 0xFFFF); + dst[7] = getRGBLookupEntry(colorMap, pixelGroup4 >> 16); + dst[8] = getRGBLookupEntry(colorMap, pixelGroup5 >> 16); + dst[9] = getRGBLookupEntry(colorMap, pixelGroup6 & 0xFFFF); + dst[10] = getRGBLookupEntry(colorMap, pixelGroup7 >> 16); + dst[11] = getRGBLookupEntry(colorMap, pixelGroup8 & 0xFFFF); + dst[12] = getRGBLookupEntry(colorMap, pixelGroup6 >> 16); + dst[13] = getRGBLookupEntry(colorMap, pixelGroup5 & 0xFFFF); + dst[14] = getRGBLookupEntry(colorMap, pixelGroup8 >> 16); + dst[15] = getRGBLookupEntry(colorMap, pixelGroup7 & 0xFFFF); + } + + static inline byte getRGBLookupEntry(const byte *colorMap, uint16 index) { + return colorMap[s_defaultPaletteLookup[CLIP(index, 0, 1024)]]; + } +}; + +/** + * Codebook converter that dithers in QT-style + */ +struct CodebookConverterDitherQT { + static inline void decodeBlock1(byte codebookIndex, const CinepakStrip &strip, byte *(&rows)[4], const byte *clipTable, const byte *colorMap, const Graphics::PixelFormat &format) { + const byte *colorPtr = strip.v1_dither + (codebookIndex << 2); + WRITE_UINT32(rows[0], READ_UINT32(colorPtr)); + WRITE_UINT32(rows[1], READ_UINT32(colorPtr + 1024)); + WRITE_UINT32(rows[2], READ_UINT32(colorPtr + 2048)); + WRITE_UINT32(rows[3], READ_UINT32(colorPtr + 3072)); + } + + static inline void decodeBlock4(const byte (&codebookIndex)[4], const CinepakStrip &strip, byte *(&rows)[4], const byte *clipTable, const byte *colorMap, const Graphics::PixelFormat &format) { + const byte *colorPtr = strip.v4_dither + (codebookIndex[0] << 2); + WRITE_UINT16(rows[0] + 0, READ_UINT16(colorPtr + 0)); + WRITE_UINT16(rows[1] + 0, READ_UINT16(colorPtr + 2)); + + colorPtr = strip.v4_dither + (codebookIndex[1] << 2); + WRITE_UINT16(rows[0] + 2, READ_UINT16(colorPtr + 1024)); + WRITE_UINT16(rows[1] + 2, READ_UINT16(colorPtr + 1026)); + + colorPtr = strip.v4_dither + (codebookIndex[2] << 2); + WRITE_UINT16(rows[2] + 0, READ_UINT16(colorPtr + 2048)); + WRITE_UINT16(rows[3] + 0, READ_UINT16(colorPtr + 2050)); + + colorPtr = strip.v4_dither + (codebookIndex[3] << 2); + WRITE_UINT16(rows[2] + 2, READ_UINT16(colorPtr + 3072)); + WRITE_UINT16(rows[3] + 2, READ_UINT16(colorPtr + 3074)); + } +}; + +template +void decodeVectorsTmpl(CinepakFrame &frame, const byte *clipTable, const byte *colorMap, Common::SeekableReadStream &stream, uint16 strip, byte chunkID, uint32 chunkSize) { + uint32 flag = 0, mask = 0; + PixelInt *iy[4]; + int32 startPos = stream.pos(); + + for (uint16 y = frame.strips[strip].rect.top; y < frame.strips[strip].rect.bottom; y += 4) { + iy[0] = (PixelInt *)frame.surface->getBasePtr(frame.strips[strip].rect.left, + y); + iy[1] = iy[0] + frame.width; + iy[2] = iy[1] + frame.width; + iy[3] = iy[2] + frame.width; + + for (uint16 x = frame.strips[strip].rect.left; x < frame.strips[strip].rect.right; x += 4) { + if ((chunkID & 0x01) && !(mask >>= 1)) { + if ((stream.pos() - startPos + 4) > (int32)chunkSize) + return; + + flag = stream.readUint32BE(); + mask = 0x80000000; + } + + if (!(chunkID & 0x01) || (flag & mask)) { + if (!(chunkID & 0x02) && !(mask >>= 1)) { + if ((stream.pos() - startPos + 4) > (int32)chunkSize) + return; + + flag = stream.readUint32BE(); + mask = 0x80000000; + } + + if ((chunkID & 0x02) || (~flag & mask)) { + if ((stream.pos() - startPos + 1) > (int32)chunkSize) + return; + + // Get the codebook + byte codebook = stream.readByte(); + CodebookConverter::decodeBlock1(codebook, frame.strips[strip], iy, clipTable, colorMap, frame.surface->format); + } else if (flag & mask) { + if ((stream.pos() - startPos + 4) > (int32)chunkSize) + return; + + byte codebook[4]; + stream.read(codebook, 4); + CodebookConverter::decodeBlock4(codebook, frame.strips[strip], iy, clipTable, colorMap, frame.surface->format); + } + } + + for (byte i = 0; i < 4; i++) + iy[i] += 4; + } + } +} + +} // End of anonymous namespace + +CinepakDecoder::CinepakDecoder(int bitsPerPixel) : Codec(), _bitsPerPixel(bitsPerPixel) { + _curFrame.surface = 0; + _curFrame.strips = 0; _y = 0; + _colorMap = 0; + _ditherPalette = 0; + _ditherType = kDitherTypeUnknown; if (bitsPerPixel == 8) { _pixelFormat = Graphics::PixelFormat::createFormatCLUT8(); @@ -86,6 +392,9 @@ CinepakDecoder::~CinepakDecoder() { delete[] _curFrame.strips; delete[] _clipTableBuf; + + delete[] _colorMap; + delete[] _ditherPalette; } const Graphics::Surface *CinepakDecoder::decodeFrame(Common::SeekableReadStream &stream) { @@ -96,7 +405,7 @@ const Graphics::Surface *CinepakDecoder::decodeFrame(Common::SeekableReadStream _curFrame.height = stream.readUint16BE(); _curFrame.stripCount = stream.readUint16BE(); - if (_curFrame.strips == NULL) + if (!_curFrame.strips) _curFrame.strips = new CinepakStrip[_curFrame.stripCount]; debug(4, "Cinepak Frame: Width = %d, Height = %d, Strip Count = %d", _curFrame.width, _curFrame.height, _curFrame.stripCount); @@ -120,9 +429,12 @@ const Graphics::Surface *CinepakDecoder::decodeFrame(Common::SeekableReadStream for (uint16 i = 0; i < _curFrame.stripCount; i++) { if (i > 0 && !(_curFrame.flags & 1)) { // Use codebooks from last strip + for (uint16 j = 0; j < 256; j++) { _curFrame.strips[i].v1_codebook[j] = _curFrame.strips[i - 1].v1_codebook[j]; _curFrame.strips[i].v4_codebook[j] = _curFrame.strips[i - 1].v4_codebook[j]; + memcpy(_curFrame.strips[i].v1_dither, _curFrame.strips[i - 1].v1_dither, 256 * 4 * 4 * 4); + memcpy(_curFrame.strips[i].v4_dither, _curFrame.strips[i - 1].v4_dither, 256 * 4 * 4 * 4); } } @@ -166,7 +478,10 @@ const Graphics::Surface *CinepakDecoder::decodeFrame(Common::SeekableReadStream case 0x30: case 0x31: case 0x32: - decodeVectors(stream, i, chunkID, chunkSize); + if (_ditherPalette) + ditherVectors(stream, i, chunkID, chunkSize); + else + decodeVectors(stream, i, chunkID, chunkSize); break; default: warning("Unknown Cinepak chunk ID %02x", chunkID); @@ -203,8 +518,7 @@ void CinepakDecoder::loadCodebook(Common::SeekableReadStream &stream, uint16 str if ((stream.pos() - startPos + n) > (int32)chunkSize) break; - for (byte j = 0; j < 4; j++) - codebook[i].y[j] = stream.readByte(); + stream.read(codebook[i].y, 4); if (n == 6) { codebook[i].u = stream.readSByte(); @@ -216,99 +530,150 @@ void CinepakDecoder::loadCodebook(Common::SeekableReadStream &stream, uint16 str codebook[i].u = 0; codebook[i].v = 0; } + + // Dither the codebook if we're dithering for QuickTime + if (_ditherType == kDitherTypeQT) + ditherCodebookQT(strip, codebookType, i); } } } +void CinepakDecoder::ditherCodebookQT(uint16 strip, byte codebookType, uint16 codebookIndex) { + if (codebookType == 1) { + const CinepakCodebook &codebook = _curFrame.strips[strip].v1_codebook[codebookIndex]; + byte *output = _curFrame.strips[strip].v1_dither + (codebookIndex << 2); + + byte *ditherEntry = _colorMap + createDitherTableIndex(_clipTable, codebook.y[0], codebook.u, codebook.v); + output[0x000] = ditherEntry[0x0000]; + output[0x001] = ditherEntry[0x4000]; + output[0x400] = ditherEntry[0xC000]; + output[0x401] = ditherEntry[0x0000]; + + ditherEntry = _colorMap + createDitherTableIndex(_clipTable, codebook.y[1], codebook.u, codebook.v); + output[0x002] = ditherEntry[0x8000]; + output[0x003] = ditherEntry[0xC000]; + output[0x402] = ditherEntry[0x4000]; + output[0x403] = ditherEntry[0x8000]; + + ditherEntry = _colorMap + createDitherTableIndex(_clipTable, codebook.y[2], codebook.u, codebook.v); + output[0x800] = ditherEntry[0x4000]; + output[0x801] = ditherEntry[0x8000]; + output[0xC00] = ditherEntry[0x8000]; + output[0xC01] = ditherEntry[0xC000]; + + ditherEntry = _colorMap + createDitherTableIndex(_clipTable, codebook.y[3], codebook.u, codebook.v); + output[0x802] = ditherEntry[0xC000]; + output[0x803] = ditherEntry[0x0000]; + output[0xC02] = ditherEntry[0x0000]; + output[0xC03] = ditherEntry[0x4000]; + } else { + const CinepakCodebook &codebook = _curFrame.strips[strip].v4_codebook[codebookIndex]; + byte *output = _curFrame.strips[strip].v4_dither + (codebookIndex << 2); + + byte *ditherEntry = _colorMap + createDitherTableIndex(_clipTable, codebook.y[0], codebook.u, codebook.v); + output[0x000] = ditherEntry[0x0000]; + output[0x400] = ditherEntry[0x8000]; + output[0x800] = ditherEntry[0x4000]; + output[0xC00] = ditherEntry[0xC000]; + + ditherEntry = _colorMap + createDitherTableIndex(_clipTable, codebook.y[1], codebook.u, codebook.v); + output[0x001] = ditherEntry[0x4000]; + output[0x401] = ditherEntry[0xC000]; + output[0x801] = ditherEntry[0x8000]; + output[0xC01] = ditherEntry[0x0000]; + + ditherEntry = _colorMap + createDitherTableIndex(_clipTable, codebook.y[2], codebook.u, codebook.v); + output[0x002] = ditherEntry[0xC000]; + output[0x402] = ditherEntry[0x4000]; + output[0x802] = ditherEntry[0x8000]; + output[0xC02] = ditherEntry[0x0000]; + + ditherEntry = _colorMap + createDitherTableIndex(_clipTable, codebook.y[3], codebook.u, codebook.v); + output[0x003] = ditherEntry[0x0000]; + output[0x403] = ditherEntry[0x8000]; + output[0x803] = ditherEntry[0xC000]; + output[0xC03] = ditherEntry[0x4000]; + } +} + void CinepakDecoder::decodeVectors(Common::SeekableReadStream &stream, uint16 strip, byte chunkID, uint32 chunkSize) { - uint32 flag = 0, mask = 0; - uint32 iy[4]; - int32 startPos = stream.pos(); - - for (uint16 y = _curFrame.strips[strip].rect.top; y < _curFrame.strips[strip].rect.bottom; y += 4) { - iy[0] = _curFrame.strips[strip].rect.left + y * _curFrame.width; - iy[1] = iy[0] + _curFrame.width; - iy[2] = iy[1] + _curFrame.width; - iy[3] = iy[2] + _curFrame.width; - - for (uint16 x = _curFrame.strips[strip].rect.left; x < _curFrame.strips[strip].rect.right; x += 4) { - if ((chunkID & 0x01) && !(mask >>= 1)) { - if ((stream.pos() - startPos + 4) > (int32)chunkSize) - return; - - flag = stream.readUint32BE(); - mask = 0x80000000; - } - - if (!(chunkID & 0x01) || (flag & mask)) { - if (!(chunkID & 0x02) && !(mask >>= 1)) { - if ((stream.pos() - startPos + 4) > (int32)chunkSize) - return; - - flag = stream.readUint32BE(); - mask = 0x80000000; - } - - if ((chunkID & 0x02) || (~flag & mask)) { - if ((stream.pos() - startPos + 1) > (int32)chunkSize) - return; - - // Get the codebook - CinepakCodebook codebook = _curFrame.strips[strip].v1_codebook[stream.readByte()]; - - PUT_PIXEL(iy[0] + 0, codebook.y[0], codebook.u, codebook.v); - PUT_PIXEL(iy[0] + 1, codebook.y[0], codebook.u, codebook.v); - PUT_PIXEL(iy[1] + 0, codebook.y[0], codebook.u, codebook.v); - PUT_PIXEL(iy[1] + 1, codebook.y[0], codebook.u, codebook.v); - - PUT_PIXEL(iy[0] + 2, codebook.y[1], codebook.u, codebook.v); - PUT_PIXEL(iy[0] + 3, codebook.y[1], codebook.u, codebook.v); - PUT_PIXEL(iy[1] + 2, codebook.y[1], codebook.u, codebook.v); - PUT_PIXEL(iy[1] + 3, codebook.y[1], codebook.u, codebook.v); - - PUT_PIXEL(iy[2] + 0, codebook.y[2], codebook.u, codebook.v); - PUT_PIXEL(iy[2] + 1, codebook.y[2], codebook.u, codebook.v); - PUT_PIXEL(iy[3] + 0, codebook.y[2], codebook.u, codebook.v); - PUT_PIXEL(iy[3] + 1, codebook.y[2], codebook.u, codebook.v); - - PUT_PIXEL(iy[2] + 2, codebook.y[3], codebook.u, codebook.v); - PUT_PIXEL(iy[2] + 3, codebook.y[3], codebook.u, codebook.v); - PUT_PIXEL(iy[3] + 2, codebook.y[3], codebook.u, codebook.v); - PUT_PIXEL(iy[3] + 3, codebook.y[3], codebook.u, codebook.v); - } else if (flag & mask) { - if ((stream.pos() - startPos + 4) > (int32)chunkSize) - return; - - CinepakCodebook codebook = _curFrame.strips[strip].v4_codebook[stream.readByte()]; - PUT_PIXEL(iy[0] + 0, codebook.y[0], codebook.u, codebook.v); - PUT_PIXEL(iy[0] + 1, codebook.y[1], codebook.u, codebook.v); - PUT_PIXEL(iy[1] + 0, codebook.y[2], codebook.u, codebook.v); - PUT_PIXEL(iy[1] + 1, codebook.y[3], codebook.u, codebook.v); - - codebook = _curFrame.strips[strip].v4_codebook[stream.readByte()]; - PUT_PIXEL(iy[0] + 2, codebook.y[0], codebook.u, codebook.v); - PUT_PIXEL(iy[0] + 3, codebook.y[1], codebook.u, codebook.v); - PUT_PIXEL(iy[1] + 2, codebook.y[2], codebook.u, codebook.v); - PUT_PIXEL(iy[1] + 3, codebook.y[3], codebook.u, codebook.v); - - codebook = _curFrame.strips[strip].v4_codebook[stream.readByte()]; - PUT_PIXEL(iy[2] + 0, codebook.y[0], codebook.u, codebook.v); - PUT_PIXEL(iy[2] + 1, codebook.y[1], codebook.u, codebook.v); - PUT_PIXEL(iy[3] + 0, codebook.y[2], codebook.u, codebook.v); - PUT_PIXEL(iy[3] + 1, codebook.y[3], codebook.u, codebook.v); - - codebook = _curFrame.strips[strip].v4_codebook[stream.readByte()]; - PUT_PIXEL(iy[2] + 2, codebook.y[0], codebook.u, codebook.v); - PUT_PIXEL(iy[2] + 3, codebook.y[1], codebook.u, codebook.v); - PUT_PIXEL(iy[3] + 2, codebook.y[2], codebook.u, codebook.v); - PUT_PIXEL(iy[3] + 3, codebook.y[3], codebook.u, codebook.v); - } - } - - for (byte i = 0; i < 4; i++) - iy[i] += 4; - } + if (_curFrame.surface->format.bytesPerPixel == 1) { + decodeVectorsTmpl(_curFrame, _clipTable, _colorMap, stream, strip, chunkID, chunkSize); + } else if (_curFrame.surface->format.bytesPerPixel == 2) { + decodeVectorsTmpl(_curFrame, _clipTable, _colorMap, stream, strip, chunkID, chunkSize); + } else if (_curFrame.surface->format.bytesPerPixel == 4) { + decodeVectorsTmpl(_curFrame, _clipTable, _colorMap, stream, strip, chunkID, chunkSize); } } +bool CinepakDecoder::canDither(DitherType type) const { + return (type == kDitherTypeVFW || type == kDitherTypeQT) && _bitsPerPixel == 24; +} + +void CinepakDecoder::setDither(DitherType type, const byte *palette) { + assert(canDither(type)); + + delete[] _colorMap; + delete[] _ditherPalette; + + _ditherPalette = new byte[256 * 3]; + memcpy(_ditherPalette, palette, 256 * 3); + + _dirtyPalette = true; + _pixelFormat = Graphics::PixelFormat::createFormatCLUT8(); + _ditherType = type; + + if (type == kDitherTypeVFW) { + _colorMap = new byte[221]; + + for (int i = 0; i < 221; i++) + _colorMap[i] = findNearestRGB(i); + } else { + // Generate QuickTime dither table + // 4 blocks of 0x4000 bytes (RGB554 lookup) + _colorMap = createQuickTimeDitherTable(palette, 256); + } +} + +byte CinepakDecoder::findNearestRGB(int index) const { + int r = s_defaultPalette[index * 3]; + int g = s_defaultPalette[index * 3 + 1]; + int b = s_defaultPalette[index * 3 + 2]; + + byte result = 0; + int diff = 0x7FFFFFFF; + + for (int i = 0; i < 256; i++) { + int bDiff = b - (int)_ditherPalette[i * 3 + 2]; + int curDiffB = diff - (bDiff * bDiff); + + if (curDiffB > 0) { + int gDiff = g - (int)_ditherPalette[i * 3 + 1]; + int curDiffG = curDiffB - (gDiff * gDiff); + + if (curDiffG > 0) { + int rDiff = r - (int)_ditherPalette[i * 3]; + int curDiffR = curDiffG - (rDiff * rDiff); + + if (curDiffR > 0) { + diff -= curDiffR; + result = i; + + if (diff == 0) + break; + } + } + } + } + + return result; +} + +void CinepakDecoder::ditherVectors(Common::SeekableReadStream &stream, uint16 strip, byte chunkID, uint32 chunkSize) { + if (_ditherType == kDitherTypeVFW) + decodeVectorsTmpl(_curFrame, _clipTable, _colorMap, stream, strip, chunkID, chunkSize); + else + decodeVectorsTmpl(_curFrame, _clipTable, _colorMap, stream, strip, chunkID, chunkSize); +} + } // End of namespace Image diff --git a/image/codecs/cinepak.h b/image/codecs/cinepak.h index e9cd4377301..dc8172ea0f1 100644 --- a/image/codecs/cinepak.h +++ b/image/codecs/cinepak.h @@ -46,6 +46,7 @@ struct CinepakStrip { uint16 length; Common::Rect rect; CinepakCodebook v1_codebook[256], v4_codebook[256]; + byte v1_dither[256 * 4 * 4 * 4], v4_dither[256 * 4 * 4 * 4]; }; struct CinepakFrame { @@ -72,14 +73,31 @@ public: const Graphics::Surface *decodeFrame(Common::SeekableReadStream &stream); Graphics::PixelFormat getPixelFormat() const { return _pixelFormat; } + bool containsPalette() const { return _ditherPalette != 0; } + const byte *getPalette() { _dirtyPalette = false; return _ditherPalette; } + bool hasDirtyPalette() const { return _dirtyPalette; } + bool canDither(DitherType type) const; + void setDither(DitherType type, const byte *palette); + private: CinepakFrame _curFrame; int32 _y; + int _bitsPerPixel; Graphics::PixelFormat _pixelFormat; byte *_clipTable, *_clipTableBuf; + byte *_ditherPalette; + bool _dirtyPalette; + byte *_rgbLookup; + byte *_colorMap; + DitherType _ditherType; + void loadCodebook(Common::SeekableReadStream &stream, uint16 strip, byte codebookType, byte chunkID, uint32 chunkSize); void decodeVectors(Common::SeekableReadStream &stream, uint16 strip, byte chunkID, uint32 chunkSize); + + byte findNearestRGB(int index) const; + void ditherVectors(Common::SeekableReadStream &stream, uint16 strip, byte chunkID, uint32 chunkSize); + void ditherCodebookQT(uint16 strip, byte codebookType, uint16 codebookIndex); }; } // End of namespace Image diff --git a/image/codecs/cinepak_tables.h b/image/codecs/cinepak_tables.h new file mode 100644 index 00000000000..03131cec22e --- /dev/null +++ b/image/codecs/cinepak_tables.h @@ -0,0 +1,780 @@ +/* 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. + * + */ + +#ifndef IMAGE_CODECS_CINEPAK_TABLES_H +#define IMAGE_CODECS_CINEPAK_TABLES_H + +#include "common/scummsys.h" + +namespace Image { + +static const byte s_defaultPaletteLookup[1024] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, + 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, + 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, + 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, + 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, + 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, + 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x07, 0x07, + 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x07, 0x07, + 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x07, 0x07, + 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x07, 0x07, + 0x08, 0x08, 0x08, 0x09, 0x0A, 0x07, 0x07, 0x07, + 0x0B, 0x0B, 0x0B, 0x0C, 0x0D, 0x0D, 0x0D, 0x07, + 0x0E, 0x0E, 0x0E, 0x0F, 0x0D, 0x0D, 0x0D, 0x0D, + 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x0D, 0x0D, + 0x12, 0x12, 0x12, 0x13, 0x14, 0x15, 0x16, 0x16, + 0x12, 0x12, 0x12, 0x13, 0x14, 0x15, 0x16, 0x16, + 0x12, 0x12, 0x12, 0x13, 0x14, 0x15, 0x16, 0x16, + 0x12, 0x12, 0x12, 0x13, 0x14, 0x15, 0x16, 0x16, + 0x17, 0x17, 0x17, 0x18, 0x19, 0x1A, 0x16, 0x16, + 0x1B, 0x1B, 0x1B, 0x1C, 0x1D, 0x1E, 0x1E, 0x1E, + 0x1F, 0x1F, 0x1F, 0x20, 0x21, 0x1E, 0x1E, 0x1E, + 0x22, 0x22, 0x22, 0x23, 0x24, 0x24, 0x24, 0x1E, + 0x25, 0x25, 0x25, 0x26, 0x27, 0x28, 0x29, 0x29, + 0x25, 0x25, 0x25, 0x26, 0x27, 0x28, 0x29, 0x29, + 0x25, 0x25, 0x25, 0x26, 0x27, 0x28, 0x29, 0x29, + 0x25, 0x25, 0x25, 0x26, 0x27, 0x28, 0x29, 0x29, + 0x2A, 0x2A, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2E, + 0x2F, 0x2F, 0x2F, 0x30, 0x31, 0x32, 0x2E, 0x2E, + 0x33, 0x33, 0x33, 0x34, 0x35, 0x36, 0x36, 0x36, + 0x33, 0x33, 0x33, 0x34, 0x35, 0x36, 0x36, 0x36, + 0x37, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3B, 0x3B, + 0x37, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3B, 0x3B, + 0x37, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3B, 0x3B, + 0x3C, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x40, 0x40, + 0x41, 0x41, 0x42, 0x43, 0x44, 0x45, 0x45, 0x45, + 0x46, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4A, 0x4A, + 0x4B, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x4F, 0x4F, + 0x4B, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x4F, 0x4F, + 0x50, 0x50, 0x51, 0x52, 0x53, 0x54, 0x54, 0x54, + 0x50, 0x50, 0x51, 0x52, 0x53, 0x54, 0x54, 0x54, + 0x50, 0x50, 0x51, 0x52, 0x53, 0x54, 0x54, 0x54, + 0x55, 0x55, 0x56, 0x57, 0x58, 0x59, 0x59, 0x59, + 0x5A, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5E, 0x5E, + 0x5F, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x63, 0x63, + 0x64, 0x64, 0x65, 0x66, 0x67, 0x68, 0x68, 0x68, + 0x64, 0x64, 0x65, 0x66, 0x67, 0x68, 0x68, 0x68, + 0x69, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6D, 0x6D, + 0x69, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6D, 0x6D, + 0x69, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6D, 0x6D, + 0x6E, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x72, 0x72, + 0x73, 0x73, 0x74, 0x75, 0x76, 0x77, 0x77, 0x77, + 0x78, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7C, 0x7C, + 0x7D, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x81, 0x81, + 0x7D, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x81, 0x81, + 0x82, 0x82, 0x83, 0x84, 0x85, 0x86, 0x86, 0x86, + 0x82, 0x82, 0x83, 0x84, 0x85, 0x86, 0x86, 0x86, + 0x87, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8B, 0x8B, + 0x8C, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x90, 0x90, + 0x91, 0x91, 0x92, 0x93, 0x94, 0x95, 0x95, 0x95, + 0x96, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9A, 0x9A, + 0x96, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9A, 0x9A, + 0x96, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9A, 0x9A, + 0x9B, 0x9B, 0x9B, 0x9C, 0x9D, 0x9D, 0x9D, 0x9D, + 0x9E, 0x9B, 0x9B, 0x9C, 0x9D, 0x9D, 0x9D, 0x9D, + 0x9E, 0x9E, 0x9F, 0xA0, 0xA1, 0xA1, 0xA1, 0xA1, + 0xA2, 0xA2, 0xA3, 0xA4, 0xA5, 0xA5, 0xA5, 0xA5, + 0xA6, 0xA6, 0xA7, 0xA8, 0xA9, 0xA9, 0xA9, 0xA9, + 0xAA, 0xAA, 0xAB, 0xAC, 0xAD, 0xAD, 0xAD, 0xAD, + 0xAA, 0xAA, 0xAB, 0xAC, 0xAD, 0xAD, 0xAD, 0xAD, + 0xAA, 0xAA, 0xAB, 0xAC, 0xAD, 0xAD, 0xAD, 0xAD, + 0xAE, 0xAE, 0xAE, 0xAF, 0xB0, 0xB0, 0xB0, 0xB0, + 0xAE, 0xAE, 0xAE, 0xAF, 0xB0, 0xB0, 0xB0, 0xB0, + 0xB4, 0xB1, 0xB1, 0xB2, 0xB3, 0xB3, 0xB3, 0xB3, + 0xB4, 0xB4, 0xB5, 0xB6, 0xB7, 0xB7, 0xB7, 0xB7, + 0xB8, 0xB8, 0xB9, 0xBA, 0xBB, 0xBB, 0xBB, 0xBB, + 0xBC, 0xBC, 0xBD, 0xBE, 0xBF, 0xBF, 0xBF, 0xBF, + 0xBC, 0xBC, 0xBD, 0xBE, 0xBF, 0xBF, 0xBF, 0xBF, + 0xBC, 0xBC, 0xBD, 0xBE, 0xBF, 0xBF, 0xBF, 0xBF, + 0xC2, 0xC0, 0xC0, 0xC0, 0xC1, 0xC1, 0xC1, 0xC1, + 0xC2, 0xC2, 0xC0, 0xC0, 0xC1, 0xC1, 0xC1, 0xC1, + 0xC2, 0xC2, 0xC2, 0xC3, 0xC4, 0xC4, 0xC4, 0xC4, + 0xC8, 0xC5, 0xC5, 0xC6, 0xC7, 0xC7, 0xC7, 0xC7, + 0xC8, 0xC8, 0xC9, 0xCA, 0xCB, 0xCB, 0xCB, 0xCB, + 0xCC, 0xCC, 0xCD, 0xCE, 0xCF, 0xCF, 0xCF, 0xCF, + 0xCC, 0xCC, 0xCD, 0xCE, 0xCF, 0xCF, 0xCF, 0xCF, + 0xCC, 0xCC, 0xCD, 0xCE, 0xCF, 0xCF, 0xCF, 0xCF, + 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, + 0xD2, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, + 0xD2, 0xD2, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, + 0xD2, 0xD2, 0xD2, 0xD3, 0xD3, 0xD3, 0xD3, 0xD3, + 0xD4, 0xD4, 0xD4, 0xD5, 0xD5, 0xD5, 0xD5, 0xD5, + 0xD4, 0xD4, 0xD4, 0xD5, 0xD5, 0xD5, 0xD5, 0xD5, + 0xD4, 0xD4, 0xD4, 0xD5, 0xD5, 0xD5, 0xD5, 0xD5, + 0xD4, 0xD4, 0xD4, 0xD5, 0xD5, 0xD5, 0xD5, 0xD5, + 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, + 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, + 0xD8, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, + 0xD8, 0xD8, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, + 0xD8, 0xD8, 0xD8, 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, + 0xD8, 0xD8, 0xD8, 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, + 0xD8, 0xD8, 0xD8, 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, + 0xD8, 0xD8, 0xD8, 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, + 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, + 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, + 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, + 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, + 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, + 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, + 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, + 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, + 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, + 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, + 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, + 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, + 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, + 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, + 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, + 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC +}; + +static const byte s_defaultPalette[221 * 3] = { + 0x02, 0x02, 0x02, + 0x19, 0x19, 0x19, + 0x47, 0x02, 0x19, + 0x19, 0x0D, 0x47, + 0x00, 0x51, 0x00, + 0x2E, 0x3A, 0x00, + 0x5C, 0x23, 0x00, + 0x8F, 0x0A, 0x00, + 0x00, 0x45, 0x2E, + 0x2E, 0x2E, 0x2E, + 0x5C, 0x17, 0x2E, + 0x00, 0x3A, 0x5C, + 0x2E, 0x23, 0x5C, + 0x5C, 0x0C, 0x5C, + 0x00, 0x2C, 0x95, + 0x2E, 0x15, 0x95, + 0x00, 0x1A, 0xDC, + 0x2E, 0x03, 0xDC, + 0x15, 0x66, 0x15, + 0x43, 0x4F, 0x15, + 0x71, 0x38, 0x15, + 0xA4, 0x1E, 0x15, + 0xDB, 0x02, 0x15, + 0x15, 0x5A, 0x43, + 0x43, 0x43, 0x43, + 0x71, 0x2C, 0x43, + 0xA4, 0x13, 0x43, + 0x15, 0x4F, 0x71, + 0x43, 0x38, 0x71, + 0x71, 0x21, 0x71, + 0xA4, 0x07, 0x71, + 0x15, 0x40, 0xAA, + 0x43, 0x29, 0xAA, + 0x71, 0x12, 0xAA, + 0x15, 0x2F, 0xF1, + 0x43, 0x18, 0xF1, + 0x71, 0x01, 0xF1, + 0x29, 0x79, 0x29, + 0x57, 0x62, 0x29, + 0x85, 0x4B, 0x29, + 0xB7, 0x32, 0x29, + 0xEF, 0x16, 0x29, + 0x29, 0x6E, 0x57, + 0x57, 0x57, 0x57, + 0x85, 0x40, 0x57, + 0xB7, 0x27, 0x57, + 0xEF, 0x0B, 0x57, + 0x29, 0x62, 0x85, + 0x57, 0x4B, 0x85, + 0x85, 0x34, 0x85, + 0xB7, 0x1B, 0x85, + 0x29, 0x54, 0xBE, + 0x57, 0x3D, 0xBE, + 0x85, 0x26, 0xBE, + 0xB7, 0x0D, 0xBE, + 0x03, 0xB5, 0x09, + 0x3C, 0x99, 0x09, + 0x6A, 0x82, 0x09, + 0x98, 0x6B, 0x09, + 0xCA, 0x51, 0x09, + 0x03, 0xA9, 0x3C, + 0x3C, 0x8C, 0x3C, + 0x6A, 0x75, 0x3C, + 0x98, 0x5E, 0x3C, + 0xCA, 0x45, 0x3C, + 0x03, 0x9D, 0x6A, + 0x3C, 0x81, 0x6A, + 0x6A, 0x6A, 0x6A, + 0x98, 0x53, 0x6A, + 0xCA, 0x39, 0x6A, + 0x03, 0x92, 0x98, + 0x3C, 0x75, 0x98, + 0x6A, 0x5E, 0x98, + 0x98, 0x47, 0x98, + 0xCA, 0x2E, 0x98, + 0x03, 0x83, 0xD1, + 0x3C, 0x67, 0xD1, + 0x6A, 0x50, 0xD1, + 0x98, 0x39, 0xD1, + 0xCA, 0x20, 0xD1, + 0x14, 0xC7, 0x1B, + 0x4D, 0xAB, 0x1B, + 0x7B, 0x94, 0x1B, + 0xA9, 0x7D, 0x1B, + 0xDC, 0x63, 0x1B, + 0x14, 0xBA, 0x4D, + 0x4D, 0x9E, 0x4D, + 0x7B, 0x87, 0x4D, + 0xA9, 0x70, 0x4D, + 0xDC, 0x57, 0x4D, + 0x14, 0xAF, 0x7B, + 0x4D, 0x92, 0x7B, + 0x7B, 0x7B, 0x7B, + 0xA9, 0x64, 0x7B, + 0xDC, 0x4B, 0x7B, + 0x14, 0xA3, 0xA9, + 0x4D, 0x87, 0xA9, + 0x7B, 0x70, 0xA9, + 0xA9, 0x59, 0xA9, + 0xDC, 0x40, 0xA9, + 0x14, 0x95, 0xE2, + 0x4D, 0x79, 0xE2, + 0x7B, 0x62, 0xE2, + 0xA9, 0x4B, 0xE2, + 0xDC, 0x31, 0xE2, + 0x25, 0xD8, 0x2C, + 0x5E, 0xBB, 0x2C, + 0x8C, 0xA4, 0x2C, + 0xBA, 0x8D, 0x2C, + 0xED, 0x74, 0x2C, + 0x25, 0xCB, 0x5E, + 0x5E, 0xAF, 0x5E, + 0x8C, 0x98, 0x5E, + 0xBA, 0x81, 0x5E, + 0xED, 0x67, 0x5E, + 0x25, 0xC0, 0x8C, + 0x5E, 0xA3, 0x8C, + 0x8C, 0x8C, 0x8C, + 0xBA, 0x75, 0x8C, + 0xED, 0x5C, 0x8C, + 0x25, 0xB4, 0xBA, + 0x5E, 0x98, 0xBA, + 0x8C, 0x81, 0xBA, + 0xBA, 0x6A, 0xBA, + 0xED, 0x50, 0xBA, + 0x25, 0xA6, 0xF3, + 0x5E, 0x8A, 0xF3, + 0x8C, 0x73, 0xF3, + 0xBA, 0x5C, 0xF3, + 0xED, 0x42, 0xF3, + 0x35, 0xF6, 0x04, + 0x6E, 0xD9, 0x04, + 0x9C, 0xC2, 0x04, + 0xCA, 0xAB, 0x04, + 0xFD, 0x92, 0x04, + 0x35, 0xE8, 0x3C, + 0x6E, 0xCB, 0x3C, + 0x9C, 0xB4, 0x3C, + 0xCA, 0x9D, 0x3C, + 0xFD, 0x84, 0x3C, + 0x35, 0xDB, 0x6E, + 0x6E, 0xBF, 0x6E, + 0x9C, 0xA8, 0x6E, + 0xCA, 0x91, 0x6E, + 0xFD, 0x78, 0x6E, + 0x35, 0xD0, 0x9C, + 0x6E, 0xB3, 0x9C, + 0x9C, 0x9C, 0x9C, + 0xCA, 0x85, 0x9C, + 0xFD, 0x6C, 0x9C, + 0x35, 0xC4, 0xCA, + 0x6E, 0xA8, 0xCA, + 0x9C, 0x91, 0xCA, + 0xCA, 0x7A, 0xCA, + 0xFD, 0x61, 0xCA, + 0x7E, 0xE9, 0x13, + 0xAC, 0xD2, 0x13, + 0xDA, 0xBB, 0x13, + 0x45, 0xF7, 0x4B, + 0x7E, 0xDB, 0x4B, + 0xAC, 0xC4, 0x4B, + 0xDA, 0xAD, 0x4B, + 0x45, 0xEB, 0x7E, + 0x7E, 0xCE, 0x7E, + 0xAC, 0xB7, 0x7E, + 0xDA, 0xA0, 0x7E, + 0x45, 0xDF, 0xAC, + 0x7E, 0xC3, 0xAC, + 0xAC, 0xAC, 0xAC, + 0xDA, 0x95, 0xAC, + 0x45, 0xD4, 0xDA, + 0x7E, 0xB7, 0xDA, + 0xAC, 0xA0, 0xDA, + 0xDA, 0x89, 0xDA, + 0x8C, 0xF7, 0x22, + 0xBA, 0xE0, 0x22, + 0xE8, 0xC9, 0x22, + 0x8C, 0xE9, 0x59, + 0xBA, 0xD2, 0x59, + 0xE8, 0xBB, 0x59, + 0x53, 0xF9, 0x8C, + 0x8C, 0xDD, 0x8C, + 0xBA, 0xC6, 0x8C, + 0xE8, 0xAF, 0x8C, + 0x53, 0xEE, 0xBA, + 0x8C, 0xD1, 0xBA, + 0xBA, 0xBA, 0xBA, + 0xE8, 0xA3, 0xBA, + 0x53, 0xE2, 0xE8, + 0x8C, 0xC6, 0xE8, + 0xBA, 0xAF, 0xE8, + 0xE8, 0x98, 0xE8, + 0xC8, 0xEE, 0x30, + 0xF6, 0xD7, 0x30, + 0x9A, 0xF7, 0x67, + 0xC8, 0xE0, 0x67, + 0xF6, 0xC9, 0x67, + 0x9A, 0xEA, 0x9A, + 0xC8, 0xD3, 0x9A, + 0xF6, 0xBC, 0x9A, + 0x61, 0xFB, 0xC8, + 0x9A, 0xDF, 0xC8, + 0xC8, 0xC8, 0xC8, + 0xF6, 0xB1, 0xC8, + 0x61, 0xF0, 0xF6, + 0x9A, 0xD3, 0xF6, + 0xC8, 0xBC, 0xF6, + 0xF6, 0xA5, 0xF6, + 0xD5, 0xFB, 0x3D, + 0xD5, 0xED, 0x74, + 0xA7, 0xF7, 0xA7, + 0xD5, 0xE0, 0xA7, + 0xA7, 0xEC, 0xD5, + 0xD5, 0xD5, 0xD5, + 0xE1, 0xFA, 0x81, + 0xE1, 0xED, 0xB3, + 0xB3, 0xF8, 0xE1, + 0xE1, 0xE1, 0xE1, + 0xED, 0xF9, 0xBF, + 0xED, 0xED, 0xED, + 0xF8, 0xF8, 0xF8 +}; + +static const uint32 s_yLookup[512] = { + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000040, 0x00000000, 0x00000040, 0x00000000, + 0x00000040, 0x00000000, 0x00000040, 0x00000000, + 0x00000040, 0x00000000, 0x00000040, 0x00000040, + 0x00000040, 0x00000040, 0x00000040, 0x00000040, + 0x00000040, 0x00000040, 0x00400040, 0x00000040, + 0x00400040, 0x00000040, 0x00400040, 0x00000040, + 0x00400040, 0x00000040, 0x00400040, 0x00000040, + 0x00400040, 0x00400040, 0x00400040, 0x00400040, + 0x00400040, 0x00400040, 0x00400040, 0x00400040, + 0x00400040, 0x00400040, 0x00400040, 0x00400040, + 0x00400040, 0x00400040, 0x00400040, 0x00400040, + 0x00400040, 0x00400040, 0x00400080, 0x00400040, + 0x00400080, 0x00400040, 0x00400080, 0x00400040, + 0x00400080, 0x00400040, 0x00400080, 0x00400080, + 0x00400080, 0x00400080, 0x00400080, 0x00400080, + 0x00400080, 0x00400080, 0x00400080, 0x00400080, + 0x00800080, 0x00400080, 0x00800080, 0x00400080, + 0x00800080, 0x00400080, 0x00800080, 0x00400080, + 0x00800080, 0x00800080, 0x00800080, 0x00800080, + 0x00800080, 0x00800080, 0x00800080, 0x00800080, + 0x00800080, 0x00800080, 0x00800080, 0x00800080, + 0x00800080, 0x00800080, 0x00800080, 0x00800080, + 0x00800080, 0x00800080, 0x008000C0, 0x00800080, + 0x008000C0, 0x00800080, 0x008000C0, 0x00800080, + 0x008000C0, 0x00800080, 0x008000C0, 0x008000C0, + 0x008000C0, 0x008000C0, 0x008000C0, 0x008000C0, + 0x008000C0, 0x008000C0, 0x00C000C0, 0x008000C0, + 0x00C000C0, 0x008000C0, 0x00C000C0, 0x008000C0, + 0x00C000C0, 0x008000C0, 0x00C000C0, 0x00C000C0, + 0x00C000C0, 0x00C000C0, 0x00C000C0, 0x00C000C0, + 0x00C000C0, 0x00C000C0, 0x00C000C0, 0x00C000C0, + 0x00C000C0, 0x00C000C0, 0x00C000C0, 0x00C000C0, + 0x00C000C0, 0x00C000C0, 0x00C00100, 0x00C000C0, + 0x00C00100, 0x00C000C0, 0x00C00100, 0x00C000C0, + 0x00C00100, 0x00C000C0, 0x00C00100, 0x00C00100, + 0x00C00100, 0x00C00100, 0x00C00100, 0x00C00100, + 0x00C00100, 0x00C00100, 0x01000100, 0x00C00100, + 0x01000100, 0x00C00100, 0x01000100, 0x00C00100, + 0x01000100, 0x00C00100, 0x01000100, 0x01000100, + 0x01000100, 0x01000100, 0x01000100, 0x01000100, + 0x01000100, 0x01000100, 0x01000100, 0x01000100, + 0x01000100, 0x01000100, 0x01000100, 0x01000100, + 0x01000100, 0x01000100, 0x01000140, 0x01000100, + 0x01000140, 0x01000100, 0x01000140, 0x01000100, + 0x01000140, 0x01000140, 0x01000140, 0x01000140, + 0x01000140, 0x01000140, 0x01000140, 0x01000140, + 0x01400140, 0x01000140, 0x01400140, 0x01000140, + 0x01400140, 0x01000140, 0x01400140, 0x01000140, + 0x01400140, 0x01400140, 0x01400140, 0x01400140, + 0x01400140, 0x01400140, 0x01400140, 0x01400140, + 0x01400140, 0x01400140, 0x01400140, 0x01400140, + 0x01400140, 0x01400140, 0x01400180, 0x01400140, + 0x01400180, 0x01400140, 0x01400180, 0x01400140, + 0x01400180, 0x01400140, 0x01400180, 0x01400180, + 0x01400180, 0x01400180, 0x01400180, 0x01400180, + 0x01800180, 0x01400180, 0x01800180, 0x01400180, + 0x01800180, 0x01400180, 0x01800180, 0x01400180, + 0x01800180, 0x01800180, 0x01800180, 0x01800180, + 0x01800180, 0x01800180, 0x01800180, 0x01800180, + 0x01800180, 0x01800180, 0x01800180, 0x01800180, + 0x01800180, 0x01800180, 0x018001C0, 0x01800180, + 0x018001C0, 0x01800180, 0x018001C0, 0x01800180, + 0x018001C0, 0x018001C0, 0x018001C0, 0x018001C0, + 0x018001C0, 0x018001C0, 0x018001C0, 0x018001C0, + 0x01C001C0, 0x018001C0, 0x01C001C0, 0x018001C0, + 0x01C001C0, 0x018001C0, 0x01C001C0, 0x01C001C0, + 0x01C001C0, 0x01C001C0, 0x01C001C0, 0x01C001C0, + 0x01C001C0, 0x01C001C0, 0x01C001C0, 0x01C001C0, + 0x01C001C0, 0x01C001C0, 0x01C00200, 0x01C001C0, + 0x01C00200, 0x01C001C0, 0x01C00200, 0x01C001C0, + 0x01C00200, 0x01C001C0, 0x01C00200, 0x01C00200, + 0x01C00200, 0x01C00200, 0x01C00200, 0x01C00200, + 0x02000200, 0x01C00200, 0x02000200, 0x01C00200, + 0x02000200, 0x01C00200, 0x02000200, 0x02000200, + 0x02000200, 0x02000200, 0x02000200, 0x02000200, + 0x02000200, 0x02000200, 0x02000200, 0x02000200, + 0x02000200, 0x02000200, 0x02000240, 0x02000200, + 0x02000240, 0x02000200, 0x02000240, 0x02000200, + 0x02000240, 0x02000240, 0x02000240, 0x02000240, + 0x02000240, 0x02000240, 0x02400240, 0x02000240, + 0x02400240, 0x02000240, 0x02400240, 0x02000240, + 0x02400240, 0x02000240, 0x02400240, 0x02400240, + 0x02400240, 0x02400240, 0x02400240, 0x02400240, + 0x02400240, 0x02400240, 0x02400240, 0x02400240, + 0x02400280, 0x02400240, 0x02400280, 0x02400240, + 0x02400280, 0x02400240, 0x02400280, 0x02400280, + 0x02400280, 0x02400280, 0x02400280, 0x02400280, + 0x02800280, 0x02400280, 0x02800280, 0x02400280, + 0x02800280, 0x02400280, 0x02800280, 0x02800280, + 0x02800280, 0x02800280, 0x02800280, 0x02800280, + 0x02800280, 0x02800280, 0x02800280, 0x02800280, + 0x02800280, 0x02800280, 0x028002C0, 0x02800280, + 0x028002C0, 0x02800280, 0x028002C0, 0x02800280, + 0x028002C0, 0x028002C0, 0x028002C0, 0x028002C0, + 0x02C002C0, 0x028002C0, 0x02C002C0, 0x028002C0, + 0x02C002C0, 0x028002C0, 0x02C002C0, 0x02C002C0, + 0x02C002C0, 0x02C002C0, 0x02C002C0, 0x02C002C0, + 0x02C002C0, 0x02C002C0, 0x02C002C0, 0x02C002C0, + 0x02C00300, 0x02C002C0, 0x02C00300, 0x02C002C0, + 0x02C00300, 0x02C002C0, 0x02C00300, 0x02C00300, + 0x02C00300, 0x02C00300, 0x02C00300, 0x02C00300, + 0x03000300, 0x02C00300, 0x03000300, 0x02C00300, + 0x03000300, 0x03000300, 0x03000300, 0x03000300, + 0x03000300, 0x03000300, 0x03000300, 0x03000300, + 0x03000300, 0x03000300, 0x03000340, 0x03000300, + 0x03000340, 0x03000300, 0x03000340, 0x03000300, + 0x03000340, 0x03000340, 0x03000340, 0x03000340, + 0x03400340, 0x03000340, 0x03400340, 0x03000340, + 0x03400340, 0x03000340, 0x03400340, 0x03400340, + 0x03400340, 0x03400340, 0x03400340, 0x03400340, + 0x03400340, 0x03400340, 0x03400340, 0x03400340, + 0x03400380, 0x03400340, 0x03400380, 0x03400340, + 0x03400380, 0x03400380, 0x03400380, 0x03400380, + 0x03800380, 0x03400380, 0x03800380, 0x03400380, + 0x03800380, 0x03400380, 0x03800380, 0x03800380, + 0x03800380, 0x03800380, 0x03800380, 0x03800380, + 0x03800380, 0x03800380, 0x038003C0, 0x03800380, + 0x038003C0, 0x03800380, 0x038003C0, 0x03800380, + 0x038003C0, 0x038003C0, 0x038003C0, 0x038003C0, + 0x03C003C0, 0x038003C0, 0x03C003C0, 0x038003C0, + 0x03C003C0, 0x03C003C0, 0x03C003C0, 0x03C003C0, + 0x03C003C0, 0x03C003C0, 0x03C003C0, 0x03C003C0, + 0x03C003C0, 0x03C003C0, 0x03C003C0, 0x03C003C0, + 0x03C003C0, 0x03C003C0, 0x03C003C0, 0x03C003C0, + 0x03C003C0, 0x03C003C0, 0x03C003C0, 0x03C003C0 +}; + +static const uint32 s_uLookup[512] = { + 0x00200020, 0x00200020, 0x00200020, 0x00200020, + 0x00200020, 0x00200020, 0x00200020, 0x00200020, + 0x00200020, 0x00200020, 0x00280020, 0x00200020, + 0x00280020, 0x00200020, 0x00280020, 0x00200020, + 0x00280020, 0x00200020, 0x00280020, 0x00280020, + 0x00280020, 0x00280020, 0x00280020, 0x00280020, + 0x00280020, 0x00280020, 0x00280020, 0x00280020, + 0x00280020, 0x00280028, 0x00280020, 0x00280028, + 0x00280020, 0x00280028, 0x00280020, 0x00280028, + 0x00280028, 0x00280028, 0x00280028, 0x00280028, + 0x00280028, 0x00280028, 0x00280028, 0x00280028, + 0x00280028, 0x00280028, 0x00280028, 0x00280028, + 0x00280028, 0x00280028, 0x00280028, 0x00280028, + 0x00280028, 0x00280028, 0x00280028, 0x00280028, + 0x00280028, 0x00280028, 0x00300028, 0x00280028, + 0x00300028, 0x00280028, 0x00300028, 0x00280028, + 0x00300028, 0x00280028, 0x00300028, 0x00280028, + 0x00300028, 0x00300028, 0x00300028, 0x00300028, + 0x00300028, 0x00300028, 0x00300028, 0x00300028, + 0x00300028, 0x00300028, 0x00300028, 0x00300028, + 0x00300028, 0x00300030, 0x00300028, 0x00300030, + 0x00300028, 0x00300030, 0x00300028, 0x00300030, + 0x00300028, 0x00300030, 0x00300028, 0x00300030, + 0x00300030, 0x00300030, 0x00300030, 0x00300030, + 0x00300030, 0x00300030, 0x00300030, 0x00300030, + 0x00300030, 0x00300030, 0x00300030, 0x00300030, + 0x00300030, 0x00300030, 0x00300030, 0x00300030, + 0x00300030, 0x00300030, 0x00300030, 0x00300030, + 0x00300030, 0x00300030, 0x00300030, 0x00300030, + 0x00300030, 0x00300030, 0x00380030, 0x00300030, + 0x00380030, 0x00300030, 0x00380030, 0x00300030, + 0x00380030, 0x00300030, 0x00380030, 0x00300030, + 0x00380030, 0x00300030, 0x00380030, 0x00300030, + 0x00380030, 0x00380030, 0x00380030, 0x00380030, + 0x00380030, 0x00380030, 0x00380030, 0x00380030, + 0x00380030, 0x00380030, 0x00380030, 0x00380030, + 0x00380030, 0x00380030, 0x00380030, 0x00380038, + 0x00380030, 0x00380038, 0x00380030, 0x00380038, + 0x00380030, 0x00380038, 0x00380030, 0x00380038, + 0x00380030, 0x00380038, 0x00380030, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00380038, 0x00380038, 0x00380038, 0x00380038, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00080000, 0x00000000, + 0x00080000, 0x00000000, 0x00080000, 0x00000000, + 0x00080000, 0x00000000, 0x00080000, 0x00000000, + 0x00080000, 0x00000000, 0x00080000, 0x00000000, + 0x00080000, 0x00080000, 0x00080000, 0x00080000, + 0x00080000, 0x00080000, 0x00080000, 0x00080000, + 0x00080000, 0x00080000, 0x00080000, 0x00080000, + 0x00080000, 0x00080008, 0x00080000, 0x00080008, + 0x00080000, 0x00080008, 0x00080000, 0x00080008, + 0x00080000, 0x00080008, 0x00080000, 0x00080008, + 0x00080008, 0x00080008, 0x00080008, 0x00080008, + 0x00080008, 0x00080008, 0x00080008, 0x00080008, + 0x00080008, 0x00080008, 0x00080008, 0x00080008, + 0x00080008, 0x00080008, 0x00080008, 0x00080008, + 0x00080008, 0x00080008, 0x00080008, 0x00080008, + 0x00080008, 0x00080008, 0x00100008, 0x00080008, + 0x00100008, 0x00080008, 0x00100008, 0x00080008, + 0x00100008, 0x00080008, 0x00100008, 0x00080008, + 0x00100008, 0x00080008, 0x00100008, 0x00100008, + 0x00100008, 0x00100008, 0x00100008, 0x00100008, + 0x00100008, 0x00100008, 0x00100008, 0x00100008, + 0x00100008, 0x00100008, 0x00100008, 0x00100010, + 0x00100008, 0x00100010, 0x00100008, 0x00100010, + 0x00100008, 0x00100010, 0x00100008, 0x00100010, + 0x00100010, 0x00100010, 0x00100010, 0x00100010, + 0x00100010, 0x00100010, 0x00100010, 0x00100010, + 0x00100010, 0x00100010, 0x00100010, 0x00100010, + 0x00100010, 0x00100010, 0x00100010, 0x00100010, + 0x00100010, 0x00100010, 0x00100010, 0x00100010, + 0x00100010, 0x00100010, 0x00180010, 0x00100010, + 0x00180010, 0x00100010, 0x00180010, 0x00100010, + 0x00180010, 0x00100010, 0x00180010, 0x00100010, + 0x00180010, 0x00180010, 0x00180010, 0x00180010, + 0x00180010, 0x00180010, 0x00180010, 0x00180010, + 0x00180010, 0x00180010, 0x00180010, 0x00180018, + 0x00180010, 0x00180018, 0x00180010, 0x00180018, + 0x00180010, 0x00180018, 0x00180010, 0x00180018, + 0x00180018, 0x00180018, 0x00180018, 0x00180018, + 0x00180018, 0x00180018, 0x00180018, 0x00180018, + 0x00180018, 0x00180018, 0x00180018, 0x00180018, + 0x00180018, 0x00180018, 0x00180018, 0x00180018, + 0x00180018, 0x00180018, 0x00180018, 0x00180018, + 0x00200018, 0x00180018, 0x00200018, 0x00180018, + 0x00200018, 0x00180018, 0x00200018, 0x00180018, + 0x00200018, 0x00200018, 0x00200018, 0x00200018, + 0x00200018, 0x00200018, 0x00200018, 0x00200018, + 0x00200018, 0x00200018, 0x00200018, 0x00200020, + 0x00200018, 0x00200020, 0x00200018, 0x00200020, + 0x00200018, 0x00200020, 0x00200020, 0x00200020, + 0x00200020, 0x00200020, 0x00200020, 0x00200020, + 0x00200020, 0x00200020, 0x00200020, 0x00200020 +}; + +static const uint32 s_vLookup[512] = { + 0x00030003, 0x00030003, 0x00030003, 0x00030003, + 0x00030003, 0x00030003, 0x00030003, 0x00030003, + 0x00030003, 0x00030003, 0x00030003, 0x00030004, + 0x00030003, 0x00030004, 0x00030003, 0x00030004, + 0x00030003, 0x00030004, 0x00030004, 0x00030004, + 0x00030004, 0x00030004, 0x00030004, 0x00030004, + 0x00030004, 0x00030004, 0x00030004, 0x00030004, + 0x00030004, 0x00040004, 0x00030004, 0x00040004, + 0x00030004, 0x00040004, 0x00030004, 0x00040004, + 0x00040004, 0x00040004, 0x00040004, 0x00040004, + 0x00040004, 0x00040004, 0x00040004, 0x00040004, + 0x00040004, 0x00040004, 0x00040004, 0x00040004, + 0x00040004, 0x00040004, 0x00040004, 0x00040004, + 0x00040004, 0x00040004, 0x00040004, 0x00040004, + 0x00040004, 0x00040005, 0x00040004, 0x00040005, + 0x00040004, 0x00040005, 0x00040004, 0x00040005, + 0x00040004, 0x00040005, 0x00040005, 0x00040005, + 0x00040005, 0x00040005, 0x00040005, 0x00040005, + 0x00040005, 0x00040005, 0x00040005, 0x00040005, + 0x00040005, 0x00050005, 0x00040005, 0x00050005, + 0x00040005, 0x00050005, 0x00040005, 0x00050005, + 0x00040005, 0x00050005, 0x00050005, 0x00050005, + 0x00050005, 0x00050005, 0x00050005, 0x00050005, + 0x00050005, 0x00050005, 0x00050005, 0x00050005, + 0x00050005, 0x00050005, 0x00050005, 0x00050005, + 0x00050005, 0x00050005, 0x00050005, 0x00050005, + 0x00050005, 0x00050005, 0x00050005, 0x00050005, + 0x00050005, 0x00050006, 0x00050005, 0x00050006, + 0x00050005, 0x00050006, 0x00050005, 0x00050006, + 0x00050005, 0x00050006, 0x00050006, 0x00050006, + 0x00050006, 0x00050006, 0x00050006, 0x00050006, + 0x00050006, 0x00050006, 0x00050006, 0x00050006, + 0x00050006, 0x00050006, 0x00050006, 0x00060006, + 0x00050006, 0x00060006, 0x00050006, 0x00060006, + 0x00050006, 0x00060006, 0x00050006, 0x00060006, + 0x00050006, 0x00060006, 0x00060006, 0x00060006, + 0x00060006, 0x00060006, 0x00060006, 0x00060006, + 0x00060006, 0x00060006, 0x00060006, 0x00060006, + 0x00060006, 0x00060006, 0x00060006, 0x00060006, + 0x00060006, 0x00060006, 0x00060006, 0x00060006, + 0x00060006, 0x00060006, 0x00060006, 0x00060006, + 0x00060006, 0x00060007, 0x00060006, 0x00060007, + 0x00060006, 0x00060007, 0x00060006, 0x00060007, + 0x00060006, 0x00060007, 0x00060006, 0x00060007, + 0x00060007, 0x00060007, 0x00060007, 0x00060007, + 0x00060007, 0x00060007, 0x00060007, 0x00060007, + 0x00060007, 0x00060007, 0x00060007, 0x00060007, + 0x00060007, 0x00070007, 0x00060007, 0x00070007, + 0x00060007, 0x00070007, 0x00060007, 0x00070007, + 0x00060007, 0x00070007, 0x00060007, 0x00070007, + 0x00060007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00070007, 0x00070007, 0x00070007, 0x00070007, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000001, 0x00000000, 0x00000001, + 0x00000000, 0x00000001, 0x00000000, 0x00000001, + 0x00000000, 0x00000001, 0x00000000, 0x00000001, + 0x00000000, 0x00000001, 0x00000001, 0x00000001, + 0x00000001, 0x00000001, 0x00000001, 0x00000001, + 0x00000001, 0x00000001, 0x00000001, 0x00000001, + 0x00000001, 0x00000001, 0x00000001, 0x00000001, + 0x00000001, 0x00010001, 0x00000001, 0x00010001, + 0x00000001, 0x00010001, 0x00000001, 0x00010001, + 0x00000001, 0x00010001, 0x00000001, 0x00010001, + 0x00000001, 0x00010001, 0x00010001, 0x00010001, + 0x00010001, 0x00010001, 0x00010001, 0x00010001, + 0x00010001, 0x00010001, 0x00010001, 0x00010001, + 0x00010001, 0x00010001, 0x00010001, 0x00010001, + 0x00010001, 0x00010001, 0x00010001, 0x00010001, + 0x00010001, 0x00010001, 0x00010001, 0x00010001, + 0x00010001, 0x00010001, 0x00010001, 0x00010001, + 0x00010001, 0x00010002, 0x00010001, 0x00010002, + 0x00010001, 0x00010002, 0x00010001, 0x00010002, + 0x00010001, 0x00010002, 0x00010001, 0x00010002, + 0x00010002, 0x00010002, 0x00010002, 0x00010002, + 0x00010002, 0x00010002, 0x00010002, 0x00010002, + 0x00010002, 0x00010002, 0x00010002, 0x00010002, + 0x00010002, 0x00020002, 0x00010002, 0x00020002, + 0x00010002, 0x00020002, 0x00010002, 0x00020002, + 0x00010002, 0x00020002, 0x00020002, 0x00020002, + 0x00020002, 0x00020002, 0x00020002, 0x00020002, + 0x00020002, 0x00020002, 0x00020002, 0x00020002, + 0x00020002, 0x00020002, 0x00020002, 0x00020002, + 0x00020002, 0x00020002, 0x00020002, 0x00020002, + 0x00020002, 0x00020002, 0x00020002, 0x00020002, + 0x00020002, 0x00020003, 0x00020002, 0x00020003, + 0x00020002, 0x00020003, 0x00020002, 0x00020003, + 0x00020003, 0x00020003, 0x00020003, 0x00020003, + 0x00020003, 0x00020003, 0x00020003, 0x00020003, + 0x00020003, 0x00020003, 0x00020003, 0x00030003, + 0x00020003, 0x00030003, 0x00020003, 0x00030003, + 0x00020003, 0x00030003, 0x00030003, 0x00030003, + 0x00030003, 0x00030003, 0x00030003, 0x00030003, + 0x00030003, 0x00030003, 0x00030003, 0x00030003 +}; + +} // End of namespace Image + +#endif diff --git a/image/codecs/codec.cpp b/image/codecs/codec.cpp index 6b0c7ebcfba..398e9c562ce 100644 --- a/image/codecs/codec.cpp +++ b/image/codecs/codec.cpp @@ -20,6 +20,7 @@ * */ +#include "common/list.h" #include "common/scummsys.h" #include "image/codecs/codec.h" @@ -44,6 +45,153 @@ namespace Image { +namespace { + +/** + * Add a color to the QuickTime dither table check queue if it hasn't already been found. + */ +inline void addColorToQueue(uint16 color, uint16 index, byte *checkBuffer, Common::List &checkQueue) { + if ((READ_UINT16(checkBuffer + color * 2) & 0xFF) == 0) { + // Previously unfound color + WRITE_UINT16(checkBuffer + color * 2, index); + checkQueue.push_back(color); + } +} + +inline byte adjustColorRange(byte currentColor, byte correctColor, byte palColor) { + return CLIP(currentColor - palColor + correctColor, 0, 255); +} + +inline uint16 makeQuickTimeDitherColor(byte r, byte g, byte b) { + // RGB554 + return ((r & 0xF8) << 6) | ((g & 0xF8) << 1) | (b >> 4); +} + +} // End of anonymous namespace + +byte *Codec::createQuickTimeDitherTable(const byte *palette, uint colorCount) { + byte *buf = new byte[0x10000]; + memset(buf, 0, 0x10000); + + Common::List checkQueue; + + bool foundBlack = false; + bool foundWhite = false; + + const byte *palPtr = palette; + + // See what colors we have, and add them to the queue to check + for (uint i = 0; i < colorCount; i++) { + byte r = *palPtr++; + byte g = *palPtr++; + byte b = *palPtr++; + uint16 n = (i << 8) | 1; + uint16 col = makeQuickTimeDitherColor(r, g, b); + + if (col == 0) { + // Special case for close-to-black + // The original did more here, but it effectively discarded the value + // due to a poor if-check (whole 16-bit value instead of lower 8-bits). + WRITE_UINT16(buf, n); + foundBlack = true; + } else if (col == 0x3FFF) { + // Special case for close-to-white + // The original did more here, but it effectively discarded the value + // due to a poor if-check (whole 16-bit value instead of lower 8-bits). + WRITE_UINT16(buf + 0x7FFE, n); + foundWhite = true; + } else { + // Previously unfound color + addColorToQueue(col, n, buf, checkQueue); + } + } + + // More special handling for white + if (foundWhite) + checkQueue.push_front(0x3FFF); + + // More special handling for black + if (foundBlack) + checkQueue.push_front(0); + + // Go through the list of colors we have and match up similar colors + // to fill in the table as best as we can. + while (!checkQueue.empty()) { + uint16 col = checkQueue.front(); + checkQueue.pop_front(); + uint16 index = READ_UINT16(buf + col * 2); + + uint32 x = col << 4; + if ((x & 0xFF) < 0xF0) + addColorToQueue((x + 0x10) >> 4, index, buf, checkQueue); + if ((x & 0xFF) >= 0x10) + addColorToQueue((x - 0x10) >> 4, index, buf, checkQueue); + + uint32 y = col << 7; + if ((y & 0xFF00) < 0xF800) + addColorToQueue((y + 0x800) >> 7, index, buf, checkQueue); + if ((y & 0xFF00) >= 0x800) + addColorToQueue((y - 0x800) >> 7, index, buf, checkQueue); + + uint32 z = col << 2; + if ((z & 0xFF00) < 0xF800) + addColorToQueue((z + 0x800) >> 2, index, buf, checkQueue); + if ((z & 0xFF00) >= 0x800) + addColorToQueue((z - 0x800) >> 2, index, buf, checkQueue); + } + + // Contract the table back to just palette entries + for (int i = 0; i < 0x4000; i++) + buf[i] = READ_UINT16(buf + i * 2) >> 8; + + // Now go through and distribute the error to three more pixels + byte *bufPtr = buf; + for (uint realR = 0; realR < 0x100; realR += 8) { + for (uint realG = 0; realG < 0x100; realG += 8) { + for (uint realB = 0; realB < 0x100; realB += 16) { + byte palIndex = *bufPtr; + byte r = realR; + byte g = realG; + byte b = realB; + + byte palR = palette[palIndex * 3] & 0xF8; + byte palG = palette[palIndex * 3 + 1] & 0xF8; + byte palB = palette[palIndex * 3 + 2] & 0xF0; + + r = adjustColorRange(r, realR, palR); + g = adjustColorRange(g, realG, palG); + b = adjustColorRange(b, realB, palB); + palIndex = buf[makeQuickTimeDitherColor(r, g, b)]; + bufPtr[0x4000] = palIndex; + + palR = palette[palIndex * 3] & 0xF8; + palG = palette[palIndex * 3 + 1] & 0xF8; + palB = palette[palIndex * 3 + 2] & 0xF0; + + r = adjustColorRange(r, realR, palR); + g = adjustColorRange(g, realG, palG); + b = adjustColorRange(b, realB, palB); + palIndex = buf[makeQuickTimeDitherColor(r, g, b)]; + bufPtr[0x8000] = palIndex; + + palR = palette[palIndex * 3] & 0xF8; + palG = palette[palIndex * 3 + 1] & 0xF8; + palB = palette[palIndex * 3 + 2] & 0xF0; + + r = adjustColorRange(r, realR, palR); + g = adjustColorRange(g, realG, palG); + b = adjustColorRange(b, realB, palB); + palIndex = buf[makeQuickTimeDitherColor(r, g, b)]; + bufPtr[0xC000] = palIndex; + + bufPtr++; + } + } + } + + return buf; +} + Codec *createBitmapCodec(uint32 tag, int width, int height, int bitsPerPixel) { switch (tag) { case SWAP_CONSTANT_32(0): diff --git a/image/codecs/codec.h b/image/codecs/codec.h index d87758e65ea..5c072132d34 100644 --- a/image/codecs/codec.h +++ b/image/codecs/codec.h @@ -58,6 +58,20 @@ public: Codec() {} virtual ~Codec() {} + /** + * A type of dithering. + */ + enum DitherType { + /** Unknown */ + kDitherTypeUnknown, + + /** Video for Windows dithering */ + kDitherTypeVFW, + + /** QuickTime dithering */ + kDitherTypeQT + }; + /** * Decode the frame for the given data and return a pointer to a surface * containing the decoded frame. @@ -86,6 +100,21 @@ public: * Does the codec have a dirty palette? */ virtual bool hasDirtyPalette() const { return false; } + + /** + * Can the codec dither down to 8bpp? + */ + virtual bool canDither(DitherType type) const { return false; } + + /** + * Activate dithering mode with a palette + */ + virtual void setDither(DitherType type, const byte *palette) {} + + /** + * Create a dither table, as used by QuickTime codecs. + */ + static byte *createQuickTimeDitherTable(const byte *palette, uint colorCount); }; /** diff --git a/image/codecs/qtrle.cpp b/image/codecs/qtrle.cpp index 94744efa5a9..8a83cfa2bd1 100644 --- a/image/codecs/qtrle.cpp +++ b/image/codecs/qtrle.cpp @@ -37,27 +37,45 @@ namespace Image { QTRLEDecoder::QTRLEDecoder(uint16 width, uint16 height, byte bitsPerPixel) : Codec() { _bitsPerPixel = bitsPerPixel; + _ditherPalette = 0; + _width = width; + _height = height; + _surface = 0; + _dirtyPalette = false; + _colorMap = 0; // We need to ensure the width is a multiple of 4 + _paddedWidth = width; uint16 wMod = width % 4; if (wMod != 0) - width += 4 - wMod; + _paddedWidth += 4 - wMod; +} - _surface = new Graphics::Surface(); - _surface->create(width, height, getPixelFormat()); +QTRLEDecoder::~QTRLEDecoder() { + if (_surface) { + _surface->free(); + delete _surface; + } + + delete[] _colorMap; + delete[] _ditherPalette; } #define CHECK_STREAM_PTR(n) \ - if ((stream.pos() + n) > stream.size()) { \ - warning("QTRLE Problem: stream out of bounds (%d > %d)", stream.pos() + n, stream.size()); \ - return; \ - } + do { \ + if ((stream.pos() + n) > stream.size()) { \ + warning("QTRLE Problem: stream out of bounds (%d > %d)", stream.pos() + n, stream.size()); \ + return; \ + } \ + } while (0) #define CHECK_PIXEL_PTR(n) \ - if ((int32)pixelPtr + n > _surface->w * _surface->h) { \ - warning("QTRLE Problem: pixel ptr = %d, pixel limit = %d", pixelPtr + n, _surface->w * _surface->h); \ - return; \ - } \ + do { \ + if ((int32)pixelPtr + n > (int)_paddedWidth * _surface->h) { \ + warning("QTRLE Problem: pixel ptr = %d, pixel limit = %d", pixelPtr + n, _paddedWidth * _surface->h); \ + return; \ + } \ + } while (0) void QTRLEDecoder::decode1(Common::SeekableReadStream &stream, uint32 rowPtr, uint32 linesToChange) { uint32 pixelPtr = 0; @@ -73,7 +91,7 @@ void QTRLEDecoder::decode1(Common::SeekableReadStream &stream, uint32 rowPtr, ui if (skip & 0x80) { linesToChange--; - rowPtr += _surface->w; + rowPtr += _paddedWidth; pixelPtr = rowPtr + 2 * (skip & 0x7f); } else pixelPtr += 2 * skip; @@ -159,7 +177,7 @@ void QTRLEDecoder::decode2_4(Common::SeekableReadStream &stream, uint32 rowPtr, } } - rowPtr += _surface->w; + rowPtr += _paddedWidth; } } @@ -204,7 +222,7 @@ void QTRLEDecoder::decode8(Common::SeekableReadStream &stream, uint32 rowPtr, ui } } - rowPtr += _surface->w; + rowPtr += _paddedWidth; } } @@ -242,7 +260,7 @@ void QTRLEDecoder::decode16(Common::SeekableReadStream &stream, uint32 rowPtr, u } } - rowPtr += _surface->w; + rowPtr += _paddedWidth; } } @@ -288,7 +306,72 @@ void QTRLEDecoder::decode24(Common::SeekableReadStream &stream, uint32 rowPtr, u } } - rowPtr += _surface->w; + rowPtr += _paddedWidth; + } +} + +namespace { + +inline uint16 readDitherColor24(Common::ReadStream &stream) { + uint16 color = (stream.readByte() & 0xF8) << 6; + color |= (stream.readByte() & 0xF8) << 1; + color |= stream.readByte() >> 4; + return color; +} + +} // End of anonymous namespace + +void QTRLEDecoder::dither24(Common::SeekableReadStream &stream, uint32 rowPtr, uint32 linesToChange) { + uint32 pixelPtr = 0; + byte *output = (byte *)_surface->getPixels(); + + static const uint16 colorTableOffsets[] = { 0x0000, 0xC000, 0x4000, 0x8000 }; + + // clone2727 thinks this should be startLine & 3, but the original definitely + // isn't doing this. Unless startLine & 3 is always 0? Kinda defeats the + // purpose of the compression then. + byte curColorTableOffset = 0; + + while (linesToChange--) { + CHECK_STREAM_PTR(2); + + byte rowOffset = stream.readByte() - 1; + pixelPtr = rowPtr + rowOffset; + uint16 colorTableOffset = colorTableOffsets[curColorTableOffset] + (rowOffset << 14); + + for (int8 rleCode = stream.readSByte(); rleCode != -1; rleCode = stream.readSByte()) { + if (rleCode == 0) { + // there's another skip code in the stream + CHECK_STREAM_PTR(1); + pixelPtr += stream.readByte() - 1; + } else if (rleCode < 0) { + // decode the run length code + rleCode = -rleCode; + + CHECK_STREAM_PTR(3); + CHECK_PIXEL_PTR(rleCode); + + uint16 color = readDitherColor24(stream); + + while (rleCode--) { + output[pixelPtr++] = _colorMap[colorTableOffset + color]; + colorTableOffset += 0x4000; + } + } else { + CHECK_STREAM_PTR(rleCode * 3); + CHECK_PIXEL_PTR(rleCode); + + // copy pixels directly to output + while (rleCode--) { + uint16 color = readDitherColor24(stream); + output[pixelPtr++] = _colorMap[colorTableOffset + color]; + colorTableOffset += 0x4000; + } + } + } + + rowPtr += _paddedWidth; + curColorTableOffset = (curColorTableOffset + 1) & 3; } } @@ -336,13 +419,16 @@ void QTRLEDecoder::decode32(Common::SeekableReadStream &stream, uint32 rowPtr, u } } - rowPtr += _surface->w; + rowPtr += _paddedWidth; } } const Graphics::Surface *QTRLEDecoder::decodeFrame(Common::SeekableReadStream &stream) { + if (!_surface) + createSurface(); + uint16 startLine = 0; - uint16 height = _surface->h; + uint16 height = _height; // check if this frame is even supposed to change if (stream.size() < 8) @@ -365,7 +451,7 @@ const Graphics::Surface *QTRLEDecoder::decodeFrame(Common::SeekableReadStream &s stream.readUint16BE(); // Unknown } - uint32 rowPtr = _surface->w * startLine; + uint32 rowPtr = _paddedWidth * startLine; switch (_bitsPerPixel) { case 1: @@ -388,7 +474,10 @@ const Graphics::Surface *QTRLEDecoder::decodeFrame(Common::SeekableReadStream &s decode16(stream, rowPtr, height); break; case 24: - decode24(stream, rowPtr, height); + if (_ditherPalette) + dither24(stream, rowPtr, height); + else + decode24(stream, rowPtr, height); break; case 32: decode32(stream, rowPtr, height); @@ -400,12 +489,10 @@ const Graphics::Surface *QTRLEDecoder::decodeFrame(Common::SeekableReadStream &s return _surface; } -QTRLEDecoder::~QTRLEDecoder() { - _surface->free(); - delete _surface; -} - Graphics::PixelFormat QTRLEDecoder::getPixelFormat() const { + if (_ditherPalette) + return Graphics::PixelFormat::createFormatCLUT8(); + switch (_bitsPerPixel) { case 1: case 33: @@ -428,4 +515,31 @@ Graphics::PixelFormat QTRLEDecoder::getPixelFormat() const { return Graphics::PixelFormat(); } +bool QTRLEDecoder::canDither(DitherType type) const { + // Only 24-bit dithering is implemented at the moment + return type == kDitherTypeQT && _bitsPerPixel == 24; +} + +void QTRLEDecoder::setDither(DitherType type, const byte *palette) { + assert(canDither(type)); + + _ditherPalette = new byte[256 * 3]; + memcpy(_ditherPalette, palette, 256 * 3); + _dirtyPalette = true; + + delete[] _colorMap; + _colorMap = createQuickTimeDitherTable(palette, 256); +} + +void QTRLEDecoder::createSurface() { + if (_surface) { + _surface->free(); + delete _surface; + } + + _surface = new Graphics::Surface(); + _surface->create(_paddedWidth, _height, getPixelFormat()); + _surface->w = _width; +} + } // End of namespace Image diff --git a/image/codecs/qtrle.h b/image/codecs/qtrle.h index b44a46c3e29..e345fbeedf3 100644 --- a/image/codecs/qtrle.h +++ b/image/codecs/qtrle.h @@ -41,16 +41,29 @@ public: const Graphics::Surface *decodeFrame(Common::SeekableReadStream &stream); Graphics::PixelFormat getPixelFormat() const; + bool containsPalette() const { return _ditherPalette != 0; } + const byte *getPalette() { _dirtyPalette = false; return _ditherPalette; } + bool hasDirtyPalette() const { return _dirtyPalette; } + bool canDither(DitherType type) const; + void setDither(DitherType type, const byte *palette); + private: byte _bitsPerPixel; - Graphics::Surface *_surface; + uint16 _width, _height; + uint32 _paddedWidth; + byte *_ditherPalette; + bool _dirtyPalette; + byte *_colorMap; + + void createSurface(); void decode1(Common::SeekableReadStream &stream, uint32 rowPtr, uint32 linesToChange); void decode2_4(Common::SeekableReadStream &stream, uint32 rowPtr, uint32 linesToChange, byte bpp); void decode8(Common::SeekableReadStream &stream, uint32 rowPtr, uint32 linesToChange); void decode16(Common::SeekableReadStream &stream, uint32 rowPtr, uint32 linesToChange); void decode24(Common::SeekableReadStream &stream, uint32 rowPtr, uint32 linesToChange); + void dither24(Common::SeekableReadStream &stream, uint32 rowPtr, uint32 linesToChange); void decode32(Common::SeekableReadStream &stream, uint32 rowPtr, uint32 linesToChange); }; diff --git a/image/codecs/rpza.cpp b/image/codecs/rpza.cpp index 5aeee7c90b3..8d648e1cc12 100644 --- a/image/codecs/rpza.cpp +++ b/image/codecs/rpza.cpp @@ -32,43 +32,185 @@ namespace Image { RPZADecoder::RPZADecoder(uint16 width, uint16 height) : Codec() { - // We need to increase the surface size to a multiple of 4 - uint16 wMod = width % 4; - if (wMod != 0) - width += 4 - wMod; - - _surface = new Graphics::Surface(); - _surface->create(width, height, getPixelFormat()); + _format = Graphics::PixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0); + _ditherPalette = 0; + _dirtyPalette = false; + _colorMap = 0; + _width = width; + _height = height; + _blockWidth = (width + 3) / 4; + _blockHeight = (height + 3) / 4; + _surface = 0; } RPZADecoder::~RPZADecoder() { - _surface->free(); - delete _surface; + if (_surface) { + _surface->free(); + delete _surface; + } + + delete[] _ditherPalette; } #define ADVANCE_BLOCK() \ - pixelPtr += 4; \ - if (pixelPtr >= _surface->w) { \ - pixelPtr = 0; \ - rowPtr += _surface->w * 4; \ + blockPtr += 4; \ + if (blockPtr >= endPtr) { \ + blockPtr += pitch * 3; \ + endPtr = blockPtr + pitch; \ } \ totalBlocks--; \ if (totalBlocks < 0) \ error("rpza block counter just went negative (this should not happen)") \ -#define PUT_PIXEL(color) \ - if ((int32)blockPtr < _surface->w * _surface->h) \ - WRITE_UINT16((uint16 *)_surface->getPixels() + blockPtr, color); \ - blockPtr++ +struct BlockDecoderRaw { + static inline void drawFillBlock(uint16 *blockPtr, uint16 pitch, uint16 color, const byte *colorMap) { + blockPtr[0] = color; + blockPtr[1] = color; + blockPtr[2] = color; + blockPtr[3] = color; + blockPtr += pitch; + blockPtr[0] = color; + blockPtr[1] = color; + blockPtr[2] = color; + blockPtr[3] = color; + blockPtr += pitch; + blockPtr[0] = color; + blockPtr[1] = color; + blockPtr[2] = color; + blockPtr[3] = color; + blockPtr += pitch; + blockPtr[0] = color; + blockPtr[1] = color; + blockPtr[2] = color; + blockPtr[3] = color; + } -const Graphics::Surface *RPZADecoder::decodeFrame(Common::SeekableReadStream &stream) { + static inline void drawRawBlock(uint16 *blockPtr, uint16 pitch, const uint16 (&colors)[16], const byte *colorMap) { + blockPtr[0] = colors[0]; + blockPtr[1] = colors[1]; + blockPtr[2] = colors[2]; + blockPtr[3] = colors[3]; + blockPtr += pitch; + blockPtr[0] = colors[4]; + blockPtr[1] = colors[5]; + blockPtr[2] = colors[6]; + blockPtr[3] = colors[7]; + blockPtr += pitch; + blockPtr[0] = colors[8]; + blockPtr[1] = colors[9]; + blockPtr[2] = colors[10]; + blockPtr[3] = colors[11]; + blockPtr += pitch; + blockPtr[0] = colors[12]; + blockPtr[1] = colors[13]; + blockPtr[2] = colors[14]; + blockPtr[3] = colors[15]; + } + + static inline void drawBlendBlock(uint16 *blockPtr, uint16 pitch, const uint16 (&colors)[4], const byte (&indexes)[4], const byte *colorMap) { + blockPtr[0] = colors[(indexes[0] >> 6) & 0x03]; + blockPtr[1] = colors[(indexes[0] >> 4) & 0x03]; + blockPtr[2] = colors[(indexes[0] >> 2) & 0x03]; + blockPtr[3] = colors[(indexes[0] >> 0) & 0x03]; + blockPtr += pitch; + blockPtr[0] = colors[(indexes[1] >> 6) & 0x03]; + blockPtr[1] = colors[(indexes[1] >> 4) & 0x03]; + blockPtr[2] = colors[(indexes[1] >> 2) & 0x03]; + blockPtr[3] = colors[(indexes[1] >> 0) & 0x03]; + blockPtr += pitch; + blockPtr[0] = colors[(indexes[2] >> 6) & 0x03]; + blockPtr[1] = colors[(indexes[2] >> 4) & 0x03]; + blockPtr[2] = colors[(indexes[2] >> 2) & 0x03]; + blockPtr[3] = colors[(indexes[2] >> 0) & 0x03]; + blockPtr += pitch; + blockPtr[0] = colors[(indexes[3] >> 6) & 0x03]; + blockPtr[1] = colors[(indexes[3] >> 4) & 0x03]; + blockPtr[2] = colors[(indexes[3] >> 2) & 0x03]; + blockPtr[3] = colors[(indexes[3] >> 0) & 0x03]; + } +}; + +struct BlockDecoderDither { + static inline void drawFillBlock(byte *blockPtr, uint16 pitch, uint16 color, const byte *colorMap) { + const byte *mapOffset = colorMap + (color >> 1); + byte pixel1 = mapOffset[0x0000]; + byte pixel2 = mapOffset[0x4000]; + byte pixel3 = mapOffset[0x8000]; + byte pixel4 = mapOffset[0xC000]; + + blockPtr[0] = pixel1; + blockPtr[1] = pixel2; + blockPtr[2] = pixel3; + blockPtr[3] = pixel4; + blockPtr += pitch; + blockPtr[0] = pixel4; + blockPtr[1] = pixel1; + blockPtr[2] = pixel2; + blockPtr[3] = pixel3; + blockPtr += pitch; + blockPtr[0] = pixel2; + blockPtr[1] = pixel3; + blockPtr[2] = pixel4; + blockPtr[3] = pixel1; + blockPtr += pitch; + blockPtr[0] = pixel3; + blockPtr[1] = pixel4; + blockPtr[2] = pixel1; + blockPtr[3] = pixel2; + } + + static inline void drawRawBlock(byte *blockPtr, uint16 pitch, const uint16 (&colors)[16], const byte *colorMap) { + blockPtr[0] = colorMap[(colors[0] >> 1) + 0x0000]; + blockPtr[1] = colorMap[(colors[1] >> 1) + 0x4000]; + blockPtr[2] = colorMap[(colors[2] >> 1) + 0x8000]; + blockPtr[3] = colorMap[(colors[3] >> 1) + 0xC000]; + blockPtr += pitch; + blockPtr[0] = colorMap[(colors[4] >> 1) + 0xC000]; + blockPtr[1] = colorMap[(colors[5] >> 1) + 0x0000]; + blockPtr[2] = colorMap[(colors[6] >> 1) + 0x4000]; + blockPtr[3] = colorMap[(colors[7] >> 1) + 0x8000]; + blockPtr += pitch; + blockPtr[0] = colorMap[(colors[8] >> 1) + 0x4000]; + blockPtr[1] = colorMap[(colors[9] >> 1) + 0x8000]; + blockPtr[2] = colorMap[(colors[10] >> 1) + 0xC000]; + blockPtr[3] = colorMap[(colors[11] >> 1) + 0x0000]; + blockPtr += pitch; + blockPtr[0] = colorMap[(colors[12] >> 1) + 0x8000]; + blockPtr[1] = colorMap[(colors[13] >> 1) + 0xC000]; + blockPtr[2] = colorMap[(colors[14] >> 1) + 0x0000]; + blockPtr[3] = colorMap[(colors[15] >> 1) + 0x4000]; + } + + static inline void drawBlendBlock(byte *blockPtr, uint16 pitch, const uint16 (&colors)[4], const byte (&indexes)[4], const byte *colorMap) { + blockPtr[0] = colorMap[(colors[(indexes[0] >> 6) & 0x03] >> 1) + 0x0000]; + blockPtr[1] = colorMap[(colors[(indexes[0] >> 4) & 0x03] >> 1) + 0x4000]; + blockPtr[2] = colorMap[(colors[(indexes[0] >> 2) & 0x03] >> 1) + 0x8000]; + blockPtr[3] = colorMap[(colors[(indexes[0] >> 0) & 0x03] >> 1) + 0xC000]; + blockPtr += pitch; + blockPtr[0] = colorMap[(colors[(indexes[1] >> 6) & 0x03] >> 1) + 0xC000]; + blockPtr[1] = colorMap[(colors[(indexes[1] >> 4) & 0x03] >> 1) + 0x0000]; + blockPtr[2] = colorMap[(colors[(indexes[1] >> 2) & 0x03] >> 1) + 0x4000]; + blockPtr[3] = colorMap[(colors[(indexes[1] >> 0) & 0x03] >> 1) + 0x8000]; + blockPtr += pitch; + blockPtr[0] = colorMap[(colors[(indexes[2] >> 6) & 0x03] >> 1) + 0x4000]; + blockPtr[1] = colorMap[(colors[(indexes[2] >> 4) & 0x03] >> 1) + 0x8000]; + blockPtr[2] = colorMap[(colors[(indexes[2] >> 2) & 0x03] >> 1) + 0xC000]; + blockPtr[3] = colorMap[(colors[(indexes[2] >> 0) & 0x03] >> 1) + 0x0000]; + blockPtr += pitch; + blockPtr[0] = colorMap[(colors[(indexes[3] >> 6) & 0x03] >> 1) + 0x8000]; + blockPtr[1] = colorMap[(colors[(indexes[3] >> 4) & 0x03] >> 1) + 0xC000]; + blockPtr[2] = colorMap[(colors[(indexes[3] >> 2) & 0x03] >> 1) + 0x0000]; + blockPtr[3] = colorMap[(colors[(indexes[3] >> 0) & 0x03] >> 1) + 0x4000]; + } +}; + +template +static inline void decodeFrameTmpl(Common::SeekableReadStream &stream, PixelInt *ptr, uint16 pitch, uint16 blockWidth, uint16 blockHeight, const byte *colorMap) { uint16 colorA = 0, colorB = 0; uint16 color4[4]; - uint32 rowPtr = 0; - uint32 pixelPtr = 0; - uint32 blockPtr = 0; - uint32 rowInc = _surface->w - 4; + PixelInt *blockPtr = ptr; + PixelInt *endPtr = ptr + pitch; uint16 ta; uint16 tb; @@ -88,7 +230,7 @@ const Graphics::Surface *RPZADecoder::decodeFrame(Common::SeekableReadStream &st } // Number of 4x4 blocks in frame - int32 totalBlocks = ((_surface->w + 3) / 4) * ((_surface->h + 3) / 4); + int32 totalBlocks = blockWidth * blockHeight; // Process chunk data while ((uint32)stream.pos() < chunkSize) { @@ -117,14 +259,9 @@ const Graphics::Surface *RPZADecoder::decodeFrame(Common::SeekableReadStream &st break; case 0xa0: // Fill blocks with one color colorA = stream.readUint16BE(); + while (numBlocks--) { - blockPtr = rowPtr + pixelPtr; - for (byte pixel_y = 0; pixel_y < 4; pixel_y++) { - for (byte pixel_x = 0; pixel_x < 4; pixel_x++) { - PUT_PIXEL(colorA); - } - blockPtr += rowInc; - } + BlockDecoder::drawFillBlock(blockPtr, pitch, colorA, colorMap); ADVANCE_BLOCK(); } break; @@ -136,10 +273,10 @@ const Graphics::Surface *RPZADecoder::decodeFrame(Common::SeekableReadStream &st colorB = stream.readUint16BE(); // Sort out the colors - color4[0] = colorB; + color4[0] = colorB & 0x7FFF; color4[1] = 0; color4[2] = 0; - color4[3] = colorA; + color4[3] = colorA & 0x7FFF; // Red components ta = (colorA >> 10) & 0x1F; @@ -160,42 +297,69 @@ const Graphics::Surface *RPZADecoder::decodeFrame(Common::SeekableReadStream &st color4[2] |= ((21 * ta + 11 * tb) >> 5); while (numBlocks--) { - blockPtr = rowPtr + pixelPtr; - for (byte pixel_y = 0; pixel_y < 4; pixel_y++) { - byte index = stream.readByte(); - for (byte pixel_x = 0; pixel_x < 4; pixel_x++) { - byte idx = (index >> (2 * (3 - pixel_x))) & 0x03; - PUT_PIXEL(color4[idx]); - } - blockPtr += rowInc; - } + byte indexes[4]; + stream.read(indexes, 4); + + BlockDecoder::drawBlendBlock(blockPtr, pitch, color4, indexes, colorMap); ADVANCE_BLOCK(); } break; // Fill block with 16 colors - case 0x00: - blockPtr = rowPtr + pixelPtr; - for (byte pixel_y = 0; pixel_y < 4; pixel_y++) { - for (byte pixel_x = 0; pixel_x < 4; pixel_x++) { - // We already have color of upper left pixel - if (pixel_y != 0 || pixel_x != 0) - colorA = stream.readUint16BE(); + case 0x00: { + uint16 colors[16]; + colors[0] = colorA; - PUT_PIXEL(colorA); - } - blockPtr += rowInc; - } + for (int i = 0; i < 15; i++) + colors[i + 1] = stream.readUint16BE(); + + BlockDecoder::drawRawBlock(blockPtr, pitch, colors, colorMap); ADVANCE_BLOCK(); break; + } // Unknown opcode default: error("Unknown opcode %02x in rpza chunk", opcode); } } +} + +const Graphics::Surface *RPZADecoder::decodeFrame(Common::SeekableReadStream &stream) { + if (!_surface) { + _surface = new Graphics::Surface(); + + // Allocate enough space in the surface for the blocks + _surface->create(_blockWidth * 4, _blockHeight * 4, getPixelFormat()); + + // Adjust width/height to be the right ones + _surface->w = _width; + _surface->h = _height; + } + + if (_colorMap) + decodeFrameTmpl(stream, (byte *)_surface->getPixels(), _surface->pitch, _blockWidth, _blockHeight, _colorMap); + else + decodeFrameTmpl(stream, (uint16 *)_surface->getPixels(), _surface->pitch / 2, _blockWidth, _blockHeight, _colorMap); return _surface; } +bool RPZADecoder::canDither(DitherType type) const { + return type == kDitherTypeQT; +} + +void RPZADecoder::setDither(DitherType type, const byte *palette) { + assert(canDither(type)); + + _ditherPalette = new byte[256 * 3]; + memcpy(_ditherPalette, palette, 256 * 3); + + _dirtyPalette = true; + _format = Graphics::PixelFormat::createFormatCLUT8(); + + delete[] _colorMap; + _colorMap = createQuickTimeDitherTable(palette, 256); +} + } // End of namespace Image diff --git a/image/codecs/rpza.h b/image/codecs/rpza.h index d1dbbdb6765..d62b3853306 100644 --- a/image/codecs/rpza.h +++ b/image/codecs/rpza.h @@ -39,10 +39,22 @@ public: ~RPZADecoder(); const Graphics::Surface *decodeFrame(Common::SeekableReadStream &stream); - Graphics::PixelFormat getPixelFormat() const { return Graphics::PixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0); } + Graphics::PixelFormat getPixelFormat() const { return _format; } + + bool containsPalette() const { return _ditherPalette != 0; } + const byte *getPalette() { _dirtyPalette = false; return _ditherPalette; } + bool hasDirtyPalette() const { return _dirtyPalette; } + bool canDither(DitherType type) const; + void setDither(DitherType type, const byte *palette); private: + Graphics::PixelFormat _format; Graphics::Surface *_surface; + byte *_ditherPalette; + bool _dirtyPalette; + byte *_colorMap; + uint16 _width, _height; + uint16 _blockWidth, _blockHeight; }; } // End of namespace Image diff --git a/ports.mk b/ports.mk index db88d276ec4..231f0532bd6 100644 --- a/ports.mk +++ b/ports.mk @@ -310,10 +310,12 @@ endif @cd $(srcdir)/dists/msvc10 && ../../devtools/create_project/create_project ../.. --msvc --msvc-version 10 >/dev/null && git add -f engines/plugins_table.h *.sln *.vcxproj *.vcxproj.filters *.props @echo Creating MSVC11 project files... @cd $(srcdir)/dists/msvc11 && ../../devtools/create_project/create_project ../.. --msvc --msvc-version 11 >/dev/null && git add -f engines/plugins_table.h *.sln *.vcxproj *.vcxproj.filters *.props + @echo Creating MSVC12 project files... + @cd $(srcdir)/dists/msvc12 && ../../devtools/create_project/create_project ../.. --msvc --msvc-version 12 >/dev/null && git add -f engines/plugins_table.h *.sln *.vcxproj *.vcxproj.filters *.props @echo @echo All is done. @echo Now run - @echo "\tgit commit 'DISTS: Generated Code::Blocks and MSVC project files'" + @echo "\tgit commit -m 'DISTS: Generated Code::Blocks and MSVC project files'" # Special target to create a win32 snapshot binary under Debian Linux using cross mingw32 toolchain crosswin32dist: $(EXECUTABLE) diff --git a/video/avi_decoder.cpp b/video/avi_decoder.cpp index 7119c72f077..700975d9a2e 100644 --- a/video/avi_decoder.cpp +++ b/video/avi_decoder.cpp @@ -815,6 +815,30 @@ void AVIDecoder::AVIVideoTrack::forceTrackEnd() { _curFrame = _frameCount - 1; } +const byte *AVIDecoder::AVIVideoTrack::getPalette() const { + if (_videoCodec && _videoCodec->containsPalette()) + return _videoCodec->getPalette(); + + _dirtyPalette = false; + return _palette; +} + +bool AVIDecoder::AVIVideoTrack::hasDirtyPalette() const { + if (_videoCodec && _videoCodec->containsPalette()) + return _videoCodec->hasDirtyPalette(); + + return _dirtyPalette; +} + +bool AVIDecoder::AVIVideoTrack::canDither() const { + return _videoCodec && _videoCodec->canDither(Image::Codec::kDitherTypeVFW); +} + +void AVIDecoder::AVIVideoTrack::setDither(const byte *palette) { + assert(_videoCodec); + _videoCodec->setDither(Image::Codec::kDitherTypeVFW, palette); +} + AVIDecoder::AVIAudioTrack::AVIAudioTrack(const AVIStreamHeader &streamHeader, const PCMWaveFormat &waveFormat, Audio::Mixer::SoundType soundType) : _audsHeader(streamHeader), _wvInfo(waveFormat), _soundType(soundType), _curChunk(0) { _audStream = createAudioStream(); diff --git a/video/avi_decoder.h b/video/avi_decoder.h index 8941ff4e756..6c1ce1a4b9a 100644 --- a/video/avi_decoder.h +++ b/video/avi_decoder.h @@ -179,11 +179,14 @@ protected: int getCurFrame() const { return _curFrame; } int getFrameCount() const { return _frameCount; } const Graphics::Surface *decodeNextFrame() { return _lastFrame; } - const byte *getPalette() const { _dirtyPalette = false; return _palette; } - bool hasDirtyPalette() const { return _dirtyPalette; } + + const byte *getPalette() const; + bool hasDirtyPalette() const; void setCurFrame(int frame) { _curFrame = frame; } void loadPaletteFromChunk(Common::SeekableReadStream *chunk); void useInitialPalette(); + bool canDither() const; + void setDither(const byte *palette); bool isTruemotion1() const; void forceDimensions(uint16 width, uint16 height); diff --git a/video/qt_decoder.cpp b/video/qt_decoder.cpp index 9b77ef70c10..324bf652943 100644 --- a/video/qt_decoder.cpp +++ b/video/qt_decoder.cpp @@ -292,6 +292,9 @@ QuickTimeDecoder::VideoTrackHandler::VideoTrackHandler(QuickTimeDecoder *decoder _curPalette = 0; _dirtyPalette = false; _reversed = false; + _forcedDitherPalette = 0; + _ditherTable = 0; + _ditherFrame = 0; } QuickTimeDecoder::VideoTrackHandler::~VideoTrackHandler() { @@ -299,6 +302,14 @@ QuickTimeDecoder::VideoTrackHandler::~VideoTrackHandler() { _scaledSurface->free(); delete _scaledSurface; } + + delete[] _forcedDitherPalette; + delete[] _ditherTable; + + if (_ditherFrame) { + _ditherFrame->free(); + delete _ditherFrame; + } } bool QuickTimeDecoder::VideoTrackHandler::endOfTrack() const { @@ -382,6 +393,9 @@ uint16 QuickTimeDecoder::VideoTrackHandler::getHeight() const { } Graphics::PixelFormat QuickTimeDecoder::VideoTrackHandler::getPixelFormat() const { + if (_forcedDitherPalette) + return Graphics::PixelFormat::createFormatCLUT8(); + return ((VideoSampleDesc *)_parent->sampleDescs[0])->_videoCodec->getPixelFormat(); } @@ -463,6 +477,10 @@ const Graphics::Surface *QuickTimeDecoder::VideoTrackHandler::decodeNextFrame() } } + // Handle forced dithering + if (frame && _forcedDitherPalette) + frame = forceDither(*frame); + if (frame && (_parent->scaleFactorX != 1 || _parent->scaleFactorY != 1)) { if (!_scaledSurface) { _scaledSurface = new Graphics::Surface(); @@ -476,6 +494,11 @@ const Graphics::Surface *QuickTimeDecoder::VideoTrackHandler::decodeNextFrame() return frame; } +const byte *QuickTimeDecoder::VideoTrackHandler::getPalette() const { + _dirtyPalette = false; + return _forcedDitherPalette ? _forcedDitherPalette : _curPalette; +} + bool QuickTimeDecoder::VideoTrackHandler::setReverse(bool reverse) { _reversed = reverse; @@ -751,4 +774,101 @@ bool QuickTimeDecoder::VideoTrackHandler::endOfCurEdit() const { return getRateAdjustedFrameTime() >= getCurEditTimeOffset() + getCurEditTrackDuration(); } +bool QuickTimeDecoder::VideoTrackHandler::canDither() const { + for (uint i = 0; i < _parent->sampleDescs.size(); i++) { + VideoSampleDesc *desc = (VideoSampleDesc *)_parent->sampleDescs[i]; + + if (!desc || !desc->_videoCodec) + return false; + } + + return true; +} + +void QuickTimeDecoder::VideoTrackHandler::setDither(const byte *palette) { + assert(canDither()); + + for (uint i = 0; i < _parent->sampleDescs.size(); i++) { + VideoSampleDesc *desc = (VideoSampleDesc *)_parent->sampleDescs[i]; + + if (desc->_videoCodec->canDither(Image::Codec::kDitherTypeQT)) { + // Codec dither + desc->_videoCodec->setDither(Image::Codec::kDitherTypeQT, palette); + } else { + // Forced dither + _forcedDitherPalette = new byte[256 * 3]; + memcpy(_forcedDitherPalette, palette, 256 * 3); + _ditherTable = Image::Codec::createQuickTimeDitherTable(_forcedDitherPalette, 256); + _dirtyPalette = true; + } + } +} + +namespace { + +// Return a pixel in RGB554 +uint16 makeDitherColor(byte r, byte g, byte b) { + return ((r & 0xF8) << 6) | ((g & 0xF8) << 1) | (b >> 4); +} + +// Default template to convert a dither color +template +inline uint16 readDitherColor(PixelInt srcColor, const Graphics::PixelFormat& format, const byte *palette) { + byte r, g, b; + format.colorToRGB(srcColor, r, g, b); + return makeDitherColor(r, g, b); +} + +// Specialized version for 8bpp +template<> +inline uint16 readDitherColor(byte srcColor, const Graphics::PixelFormat& format, const byte *palette) { + return makeDitherColor(palette[srcColor * 3], palette[srcColor * 3 + 1], palette[srcColor * 3 + 2]); +} + +template +void ditherFrame(const Graphics::Surface &src, Graphics::Surface &dst, const byte *ditherTable, const byte *palette = 0) { + static const uint16 colorTableOffsets[] = { 0x0000, 0xC000, 0x4000, 0x8000 }; + + for (int y = 0; y < dst.h; y++) { + const PixelInt *srcPtr = (const PixelInt *)src.getBasePtr(0, y); + byte *dstPtr = (byte *)dst.getBasePtr(0, y); + uint16 colorTableOffset = colorTableOffsets[y & 3]; + + for (int x = 0; x < dst.w; x++) { + uint16 color = readDitherColor(*srcPtr++, src.format, palette); + *dstPtr++ = ditherTable[colorTableOffset + color]; + colorTableOffset += 0x4000; + } + } +} + +} // End of anonymous namespace + +const Graphics::Surface *QuickTimeDecoder::VideoTrackHandler::forceDither(const Graphics::Surface &frame) { + if (frame.format.bytesPerPixel == 1) { + // This should always be true, but this is for sanity + if (!_curPalette) + return &frame; + + // If the palettes match, bail out + if (memcmp(_forcedDitherPalette, _curPalette, 256 * 3) == 0) + return &frame; + } + + // Need to create a new one + if (!_ditherFrame) { + _ditherFrame = new Graphics::Surface(); + _ditherFrame->create(frame.w, frame.h, Graphics::PixelFormat::createFormatCLUT8()); + } + + if (frame.format.bytesPerPixel == 1) + ditherFrame(frame, *_ditherFrame, _ditherTable, _curPalette); + else if (frame.format.bytesPerPixel == 2) + ditherFrame(frame, *_ditherFrame, _ditherTable); + else if (frame.format.bytesPerPixel == 4) + ditherFrame(frame, *_ditherFrame, _ditherTable); + + return _ditherFrame; +} + } // End of namespace Video diff --git a/video/qt_decoder.h b/video/qt_decoder.h index 99ac9ff5f7d..5a6c5eebecd 100644 --- a/video/qt_decoder.h +++ b/video/qt_decoder.h @@ -136,10 +136,12 @@ private: int getFrameCount() const; uint32 getNextFrameStartTime() const; const Graphics::Surface *decodeNextFrame(); - const byte *getPalette() const { _dirtyPalette = false; return _curPalette; } + const byte *getPalette() const; bool hasDirtyPalette() const { return _curPalette; } bool setReverse(bool reverse); bool isReversed() const { return _reversed; } + bool canDither() const; + void setDither(const byte *palette); Common::Rational getScaledWidth() const; Common::Rational getScaledHeight() const; @@ -156,6 +158,12 @@ private: mutable bool _dirtyPalette; bool _reversed; + // Forced dithering of frames + byte *_forcedDitherPalette; + byte *_ditherTable; + Graphics::Surface *_ditherFrame; + const Graphics::Surface *forceDither(const Graphics::Surface &frame); + Common::SeekableReadStream *getNextFramePacket(uint32 &descId); uint32 getFrameDuration(); uint32 findKeyFrame(uint32 frame) const; diff --git a/video/video_decoder.cpp b/video/video_decoder.cpp index a4bc5b81a23..217b4c84566 100644 --- a/video/video_decoder.cpp +++ b/video/video_decoder.cpp @@ -47,6 +47,7 @@ VideoDecoder::VideoDecoder() { _endTimeSet = false; _nextVideoTrack = 0; _mainAudioTrack = 0; + _canSetDither = true; // Find the best format for output _defaultHighColorFormat = g_system->getScreenFormat(); @@ -77,6 +78,7 @@ void VideoDecoder::close() { _endTimeSet = false; _nextVideoTrack = 0; _mainAudioTrack = 0; + _canSetDither = true; } bool VideoDecoder::loadFile(const Common::String &filename) { @@ -171,6 +173,7 @@ Graphics::PixelFormat VideoDecoder::getPixelFormat() const { const Graphics::Surface *VideoDecoder::decodeNextFrame() { _needsUpdate = false; + _canSetDither = false; readNextPacket(); @@ -488,6 +491,23 @@ bool VideoDecoder::seekIntern(const Audio::Timestamp &time) { return true; } +bool VideoDecoder::setDitheringPalette(const byte *palette) { + // If a frame was already decoded, we can't set it now. + if (!_canSetDither) + return false; + + bool result = false; + + for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++) { + if ((*it)->getTrackType() == Track::kTrackTypeVideo && ((VideoTrack *)*it)->canDither()) { + ((VideoTrack *)*it)->setDither(palette); + result = true; + } + } + + return result; +} + VideoDecoder::Track::Track() { _paused = false; } diff --git a/video/video_decoder.h b/video/video_decoder.h index c3879e91447..eca15e72659 100644 --- a/video/video_decoder.h +++ b/video/video_decoder.h @@ -377,6 +377,25 @@ public: */ bool setReverse(bool reverse); + /** + * Tell the video to dither to a palette. + * + * By default, VideoDecoder will return surfaces in native, or in the case + * of YUV-based videos, the format set by setDefaultHighColorFormat(). + * For video formats or codecs that support it, this will start outputting + * its surfaces in 8bpp with this palette. + * + * This should be called after loadStream(), but before a decodeNextFrame() + * call. This is enforced. + * + * The palette will be copied, so you do not need to worry about the pointer + * going out-of-scope. + * + * @param palette The palette to use for dithering + * @return true on success, false otherwise + */ + bool setDitheringPalette(const byte *palette); + ///////////////////////////////////////// // Audio Control ///////////////////////////////////////// @@ -604,6 +623,16 @@ protected: * Is the video track set to play in reverse? */ virtual bool isReversed() const { return false; } + + /** + * Can the video track dither? + */ + virtual bool canDither() const { return false; } + + /** + * Activate dithering mode with a palette + */ + virtual void setDither(const byte *palette) {} }; /** @@ -901,6 +930,9 @@ private: mutable bool _dirtyPalette; const byte *_palette; + // Enforcement of not being able to set dither + bool _canSetDither; + // Default PixelFormat settings Graphics::PixelFormat _defaultHighColorFormat;