-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
417 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) 2013-2016 mogemimi. Distributed under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "Pomdog/Basic/Export.hpp" | ||
#include "Pomdog/Experimental/Image/Image.hpp" | ||
#include <memory> | ||
#include <vector> | ||
#include <chrono> | ||
|
||
namespace Pomdog { | ||
|
||
using GifDuration = std::chrono::duration<long long, std::ratio<1LL, 100LL>>; | ||
|
||
class POMDOG_EXPORT GifImageFrame final { | ||
public: | ||
std::shared_ptr<Image> Image; | ||
GifDuration Delay; | ||
}; | ||
|
||
class POMDOG_EXPORT GifImage final { | ||
public: | ||
std::vector<GifImageFrame> Frames; | ||
int LoopCount; | ||
}; | ||
|
||
} // namespace Pomdog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2013-2016 mogemimi. Distributed under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "Pomdog/Basic/Export.hpp" | ||
#include "Pomdog/Experimental/Image/GifImage.hpp" | ||
#include "Pomdog/Utility/Optional.hpp" | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace Pomdog { | ||
|
||
class POMDOG_EXPORT GifLoader final { | ||
public: | ||
static Optional<GifImage> Open(const std::string& filePath); | ||
}; | ||
|
||
} // namespace Pomdog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2013-2016 mogemimi. Distributed under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "Pomdog/Basic/Export.hpp" | ||
#include "Pomdog/Math/Color.hpp" | ||
#include <vector> | ||
|
||
namespace Pomdog { | ||
|
||
class POMDOG_EXPORT Image final { | ||
public: | ||
Image(int width, int height); | ||
|
||
Image() = delete; | ||
Image(const Image&) = delete; | ||
Image(Image &&); | ||
|
||
Image & operator=(const Image&) = delete; | ||
Image & operator=(Image &&); | ||
|
||
int GetWidth() const noexcept; | ||
|
||
int GetHeight() const noexcept; | ||
|
||
const Color* GetData() const; | ||
|
||
void SetData(const Color* pixelData); | ||
|
||
void SetData(std::vector<Color> && pixelData); | ||
|
||
const Color& GetPixel(int x, int y) const; | ||
|
||
void SetPixel(int x, int y, const Color& color); | ||
|
||
void Fill(const Color& color); | ||
|
||
void PremultiplyAlpha(); | ||
|
||
private: | ||
std::vector<Color> data; | ||
int width; | ||
int height; | ||
}; | ||
|
||
} // namespace Pomdog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// Copyright (c) 2013-2016 mogemimi. Distributed under the MIT license. | ||
|
||
#include "Pomdog/Experimental/Image/GifImageLoader.hpp" | ||
#include "Pomdog/Utility/Assert.hpp" | ||
#include <gif_lib.h> | ||
#include <functional> | ||
|
||
namespace Pomdog { | ||
namespace { | ||
|
||
void DumpExtensions( | ||
int extensionBlockCount, | ||
ExtensionBlock* extensionBlocks, | ||
const std::function<void(int)>& findLoopCount) | ||
{ | ||
const auto extensionBlockEnd = extensionBlocks + extensionBlockCount; | ||
|
||
for (auto extensionBlock = extensionBlocks; extensionBlock != extensionBlockEnd; ++extensionBlock) { | ||
POMDOG_ASSERT(extensionBlock < extensionBlockEnd); | ||
if (extensionBlock->Function == APPLICATION_EXT_FUNC_CODE && | ||
(std::memcmp(extensionBlock->Bytes, "NETSCAPE2.0", 11) == 0) && | ||
(extensionBlock->Bytes[10] == 0x01)) { | ||
|
||
++extensionBlock; | ||
GifByteType x1 = extensionBlock->Bytes[1]; | ||
GifByteType x2 = extensionBlock->Bytes[2]; | ||
int loopCount = (static_cast<int>(x2) << 8) | static_cast<int>(x1); | ||
findLoopCount(loopCount); | ||
} | ||
} | ||
} | ||
|
||
} // unnamed namespace | ||
|
||
Optional<GifImage> GifLoader::Open(const std::string& filePath) | ||
{ | ||
if (filePath.empty()) { | ||
// error | ||
return NullOpt; | ||
} | ||
|
||
int gifError = 0; | ||
auto gifFileIn = std::shared_ptr<GifFileType>( | ||
DGifOpenFileName(filePath.c_str(), &gifError), | ||
[](GifFileType* p) { | ||
if (p != nullptr) { | ||
DGifCloseFile(p, nullptr); | ||
} | ||
}); | ||
|
||
if (gifError != 0) { | ||
//throw CannotOpenGifFileException{}; | ||
return NullOpt; | ||
} | ||
|
||
if (DGifSlurp(gifFileIn.get()) == GIF_ERROR) { | ||
//throw CannotOpenGifFileException{}; | ||
return NullOpt; | ||
} | ||
|
||
GifImage result; | ||
result.LoopCount = 0; | ||
|
||
DumpExtensions( | ||
gifFileIn->ExtensionBlockCount, | ||
gifFileIn->ExtensionBlocks, | ||
[&](int loopCountIn) { | ||
result.LoopCount = loopCountIn; | ||
}); | ||
|
||
std::shared_ptr<Pomdog::Image const> prevImage; | ||
|
||
for (int index = 0; index < gifFileIn->ImageCount; ++index) { | ||
auto& saveImage = gifFileIn->SavedImages[index]; | ||
|
||
const int imageNumber = ((index + 1) % gifFileIn->ImageCount); | ||
GraphicsControlBlock gcb; | ||
bool hasGCB = false; | ||
if (DGifSavedExtensionToGCB(gifFileIn.get(), imageNumber, &gcb) == GIF_OK) { | ||
hasGCB = true; | ||
} | ||
else { | ||
hasGCB = false; | ||
} | ||
|
||
const auto colorMap = saveImage.ImageDesc.ColorMap | ||
? saveImage.ImageDesc.ColorMap | ||
: gifFileIn->SColorMap; | ||
|
||
auto img = std::make_shared<Pomdog::Image>(gifFileIn->SWidth, gifFileIn->SHeight); | ||
|
||
if (hasGCB && gcb.DisposalMode == DISPOSE_BACKGROUND) { | ||
auto fillColor = [&]() -> Color { | ||
if (hasGCB && (gcb.TransparentColor != NO_TRANSPARENT_COLOR)) { | ||
GifColorType& color = colorMap->Colors[gcb.TransparentColor]; | ||
return Color{color.Red, color.Green, color.Blue, 0}; | ||
} | ||
return Color{255, 255, 255, 255}; | ||
}(); | ||
img->Fill(fillColor); | ||
} | ||
else if (hasGCB && gcb.DisposalMode == DISPOSE_DO_NOT) { | ||
if (index > 0) { | ||
// Fill color | ||
POMDOG_ASSERT(prevImage); | ||
POMDOG_ASSERT(img->GetWidth() == prevImage->GetWidth()); | ||
POMDOG_ASSERT(img->GetHeight() == prevImage->GetHeight()); | ||
img->SetData(prevImage->GetData()); | ||
} | ||
} | ||
|
||
const int width = saveImage.ImageDesc.Width; | ||
const int height = saveImage.ImageDesc.Height; | ||
|
||
for (int x = 0; x < width; ++x) { | ||
for (int y = 0; y < height; ++y) { | ||
int colorIndex = saveImage.RasterBits[x + width * y]; | ||
assert(colorIndex < colorMap->ColorCount); | ||
GifColorType& colorFromMap = | ||
colorMap->Colors[colorIndex]; | ||
|
||
bool isTransparent = [&]{ | ||
if (!hasGCB) { | ||
return false; | ||
} | ||
return gcb.TransparentColor == colorIndex; | ||
}(); | ||
|
||
GifByteType alpha = isTransparent ? 0x00 : 0xFF; | ||
|
||
auto rgba = Color{ | ||
colorFromMap.Red, | ||
colorFromMap.Green, | ||
colorFromMap.Blue, | ||
alpha | ||
}; | ||
|
||
if (!isTransparent) { | ||
img->SetPixel( | ||
saveImage.ImageDesc.Left + x, | ||
saveImage.ImageDesc.Top + y, | ||
rgba); | ||
} | ||
} | ||
} | ||
|
||
DumpExtensions( | ||
gifFileIn->ExtensionBlockCount, | ||
gifFileIn->ExtensionBlocks, | ||
[&](int loopCountIn) { | ||
result.LoopCount = loopCountIn; | ||
}); | ||
|
||
const GifDuration sourceDelay{hasGCB ? gcb.DelayTime : 0}; | ||
|
||
GifImageFrame frame; | ||
frame.Image = img; | ||
frame.Delay = sourceDelay; | ||
result.Frames.push_back(std::move(frame)); | ||
|
||
prevImage = img; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} // namespace Pomdog |
Oops, something went wrong.