Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhas committed Jan 8, 2019
1 parent 978c6a4 commit 08a97d9
Show file tree
Hide file tree
Showing 12 changed files with 1,136 additions and 1,007 deletions.
44 changes: 44 additions & 0 deletions Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "MagickMeter.h"

BOOL Config::Match(LPCWSTR input)
{
return Utils::IsEqual(name, input);
}

BOOL Config::Equal(LPCWSTR input)
{
return Utils::IsEqual(para, input);
}

WSVector Config::ToList(int minItems, LPCWSTR defVal)
{
return Utils::SeparateList(para, L",", minItems, defVal);
}

Magick::Color Config::ToColor() {
return Utils::ParseColor(para);
}

BOOL Config::ToBool() {
return MathParser::ParseBool(para);
}

std::string Config::ToString() {
return Utils::WStringToString(para);
}

double Config::ToDouble() {
return MathParser::ParseDouble(para);
}

int Config::ToInt() {
return MathParser::ParseInt(para);
}

size_t Config::ToSizeT() {
return MathParser::ParseSizeT(para);
}

ssize_t Config::ToSSizeT() {
return MathParser::ParseSSizeT(para);
}
5 changes: 3 additions & 2 deletions magickmeter.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<RootNamespace>magickmeter</RootNamespace>
<IgnoreWarnCompileDuplicatedFilename>true</IgnoreWarnCompileDuplicatedFilename>
<PreferredToolArchitecture>x64</PreferredToolArchitecture>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Label="Configuration">
Expand Down Expand Up @@ -86,7 +86,7 @@
<ClCompile>
<AdditionalIncludeDirectories>src;API;$(ImageMagick_x64)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<Optimization>MinSpace</Optimization>
<Optimization>MaxSpeed</Optimization>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PreprocessorDefinitions>UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
Expand Down Expand Up @@ -122,6 +122,7 @@
<ClInclude Include="src\version.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Config.cpp" />
<ClCompile Include="src\FromCombine.cpp" />
<ClCompile Include="src\FromFile.cpp" />
<ClCompile Include="src\FromGradient.cpp" />
Expand Down
1 change: 1 addition & 0 deletions magickmeter.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<ClCompile Include="src\FromText.cpp" />
<ClCompile Include="src\MagickMeter.cpp" />
<ClCompile Include="src\MathParser.cpp" />
<ClCompile Include="Config.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />
Expand Down
45 changes: 19 additions & 26 deletions src/FromCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,37 +74,30 @@ const std::map<LPCWSTR, Magick::CompositeOperator> compOperationMap{
{L"STEREO", Magick::CompositeOperator::StereoCompositeOp}
};

BOOL Measure::CreateCombine(LPCWSTR baseImage, WSVector &config, ImgContainer &out)
BOOL Measure::CreateCombine(std::shared_ptr<ImgContainer> out)
{
const int baseIndex = Utils::NameToIndex(baseImage);
const int baseIndex = Utils::NameToIndex(out->config.at(0).para);
if (baseIndex != -1 && baseIndex < imgList.size())
{
out.img = imgList.at(baseIndex).img;
imgList.at(baseIndex).isCombined = TRUE;
out->img = imgList.at(baseIndex)->img;
imgList.at(baseIndex)->isCombined = TRUE;
}
else
{
RmLogF(rm, 2, L"Combine: %s is invalid.", baseImage);
RmLogF(rm, 2, L"Combine: %s is invalid.", out->config.at(0).para);
return FALSE;
}

for (auto &option : config)
for (auto &option : out->config)
{
if (option.empty())
if (option.isApplied)
continue;

std::wstring tempName;
std::wstring tempPara;
Utils::GetNamePara(option, tempName, tempPara);

LPCWSTR name = tempName.c_str();
LPCWSTR parameter = tempPara.c_str();

auto compOp = std::find_if(
compOperationMap.begin(),
compOperationMap.end(),
[name](const std::pair<LPCWSTR, Magick::CompositeOperator> &i) noexcept {
return _wcsicmp(name, i.first) == 0;
[option](const std::pair<LPCWSTR, Magick::CompositeOperator> &i) {
return Utils::IsEqual(option.name, i.first);
}
);

Expand All @@ -114,43 +107,43 @@ BOOL Measure::CreateCombine(LPCWSTR baseImage, WSVector &config, ImgContainer &o
}

// Found valid Composite operation
option.clear();
option.isApplied = TRUE;

const int index = Utils::NameToIndex(tempPara);
const int index = Utils::NameToIndex(option.para);

if (index < 0 || index >= imgList.size())
{
RmLogF(rm, 2, L"Combine: %s is invalid.", baseImage);
RmLogF(rm, 2, L"Combine: %s is invalid.", option.para);
return FALSE;
}

ImgContainer* childImage = &imgList[index];
ImgContainer* childImage = &*imgList.at(index);

childImage->isCombined = TRUE;

//Extend main image size
const size_t newW = max(
childImage->img.columns(),
out.img.columns()
out->img.columns()
);
const size_t newH = max(
childImage->img.rows(),
out.img.rows()
out->img.rows()
);

Magick::Geometry newSize(newW, newH);

try
{
Magick::Image temp = Magick::Image(out.img);
Magick::Image temp = Magick::Image(out->img);

out.img = Magick::Image(newSize, INVISIBLE);
out.img.composite(temp, 0, 0, Magick::OverCompositeOp);
out->img = Magick::Image(newSize, INVISIBLE);
out->img.composite(temp, 0, 0, Magick::OverCompositeOp);

temp = Magick::Image(newSize, INVISIBLE);
temp.composite(childImage->img, 0, 0, Magick::OverCompositeOp);

out.img.composite(temp, 0, 0, compOp->second);
out->img.composite(temp, 0, 0, compOp->second);
}
catch (Magick::Exception &error_)
{
Expand Down
66 changes: 33 additions & 33 deletions src/FromFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
#include <filesystem>
#include <sstream>

BOOL Measure::CreateFromFile(LPCWSTR baseFile, WSVector &config, ImgContainer &out)
BOOL Measure::CreateFromFile(std::shared_ptr<ImgContainer> out)
{
std::ostringstream targetFile;
BOOL isScreenshot = FALSE;

if (_wcsnicmp(baseFile, L"SCREENSHOT", 10) == 0)
if (out->config.at(0).Equal(L"SCREENSHOT"))
{
isScreenshot = TRUE;
if (config.size() > 0 && _wcsnicmp(config[0].c_str(), L"AUTOHIDE", 8) == 0)
{
isScreenshot = MathParser::ParseBool(config[0].substr(8));
config[0].clear();
}
targetFile << "screenshot:";

std::wstring baseString = baseFile;
std::wstring baseString = out->config.at(0).para;
const size_t monitorFlag = baseString.find(L"@", 10);

if (monitorFlag != std::wstring::npos &&
Expand All @@ -26,53 +21,56 @@ BOOL Measure::CreateFromFile(LPCWSTR baseFile, WSVector &config, ImgContainer &o
const int monitorIndex = MathParser::ParseInt(baseString.substr(11));
targetFile << "[" << monitorIndex << "]";
}

if (out->config.size() > 1 && out->config.at(1).Match(L"AUTOHIDE"))
{
isScreenshot = out->config.at(1).ToBool();
out->config.at(1).isApplied = TRUE;
}
}
else if (_wcsicmp(baseFile, L"CLIPBOARD") == 0)
else if (out->config.at(0).Equal(L"CLIPBOARD"))
{
targetFile << "clipboard:";
}
else // baseFile is a file path or URL
{
targetFile << Utils::WStringToString(baseFile);
targetFile << out->config.at(0).ToString();
}

BOOL isDefinedSize = FALSE;
Magick::Geometry defineSize;

for (auto &option : config)
for (auto &option : out->config)
{
if (option.empty())
if (option.isApplied)
continue;

std::wstring tempName;
std::wstring tempPara;
Utils::GetNamePara(option, tempName, tempPara);
ParseInternalVariable(tempPara, out);
ParseInternalVariable(option.para, out);

LPCWSTR name = tempName.c_str();

if (_wcsicmp(name, L"RENDERSIZE") == 0)
if (option.Match(L"RENDERSIZE"))
{
WSVector imgSize = Utils::SeparateParameter(tempPara, 3);
WSVector imgSize = option.ToList(3);

const size_t width = MathParser::ParseSizeT(imgSize[0]);
const size_t height = MathParser::ParseSizeT(imgSize[1]);
const size_t width = MathParser::ParseSizeT(imgSize.at(0));
const size_t height = MathParser::ParseSizeT(imgSize.at(1));

if (width <= 0 && height <= 0)
{
RmLogF(rm, 2, L"%s is invalid RenderSize. Render image at normal size.", tempPara);
RmLogF(rm, 2, L"%s is invalid RenderSize. Render image at normal size.", option.para.c_str());
isDefinedSize = FALSE;
}
else
{
if (width > 0) defineSize.width(width);
if (height > 0) defineSize.height(height);
const int resizeType = MathParser::ParseInt(imgSize[2]);
Utils::SetGeometryMode(resizeType, defineSize);

Utils::SetGeometryMode(MathParser::ParseInt(imgSize.at(2)), defineSize);

isDefinedSize = TRUE;
}

option.clear();
option.isApplied = TRUE;
}
}

Expand All @@ -82,23 +80,25 @@ BOOL Measure::CreateFromFile(LPCWSTR baseFile, WSVector &config, ImgContainer &o

if (isDefinedSize)
{
out.img.read(defineSize, targetFile.str()); // Why it just works with SVG?
out.img.resize(defineSize);
out->img.read(defineSize, targetFile.str()); // Why it just works with SVG?
out->img.resize(defineSize);
}
else
{
out.img.read(targetFile.str());
out->img.read(targetFile.str());
}

if (isScreenshot) RmExecute(skin, L"!SetTransparency 255");

if (!out.img.isValid()) return FALSE;
if (!out->img.isValid()) return FALSE;

out.img.strip();
out.img.alpha(true);
out->img.strip();
out->img.alpha(true);

out.W = out.img.columns();
out.H = out.img.rows();
out->geometry = Magick::Geometry{
out->img.columns(),
out->img.rows()
};
}
catch(Magick::Exception &error_)
{
Expand Down
Loading

0 comments on commit 08a97d9

Please sign in to comment.