First cleanup of image loading code

This commit is contained in:
Manuel Reimer 2014-10-28 16:54:37 +01:00
parent abd8fd8db0
commit 943c899288
5 changed files with 16 additions and 103 deletions

View File

@ -14,16 +14,12 @@ cImageLoader::cImageLoader() : cImageMagickWrapper() {
cImageLoader::~cImageLoader() { cImageLoader::~cImageLoader() {
} }
cImage cImageLoader::GetImage() { cImage *cImageLoader::GetImage(int width, int height) {
return CreateImageCopy(); return CreateImage(width, height, false);
} }
bool cImageLoader::LoadImage(const char *path, int width, int height) { bool cImageLoader::LoadImage(const char *path) {
if (cImageMagickWrapper::LoadImage(path)) { return cImageMagickWrapper::LoadImage(path);
buffer.sample(Geometry(width, height));
return true;
}
return false;
} }
void cImageLoader::DeterminateChannelLogoSize(int &width, int &height) { void cImageLoader::DeterminateChannelLogoSize(int &width, int &height) {

View File

@ -14,8 +14,8 @@ class cImageLoader : public cImageMagickWrapper {
public: public:
cImageLoader(); cImageLoader();
~cImageLoader(); ~cImageLoader();
cImage GetImage(); cImage *GetImage(int width, int height);
bool LoadImage(const char *path, int width, int height); bool LoadImage(const char *path);
void DeterminateChannelLogoSize(int &width, int &height); void DeterminateChannelLogoSize(int &width, int &height);
private: private:
}; };

View File

@ -48,51 +48,6 @@ cImage *cImageMagickWrapper::CreateImage(int width, int height, bool preserveAsp
return image; return image;
} }
cImage cImageMagickWrapper::CreateImageCopy() {
int w, h;
w = buffer.columns();
h = buffer.rows();
cImage image (cSize(w, h));
const PixelPacket *pixels = buffer.getConstPixels(0, 0, w, h);
for (int iy = 0; iy < h; ++iy) {
for (int ix = 0; ix < w; ++ix) {
tColor col = (~int(pixels->opacity * 255 / MaxRGB) << 24)
| (int(pixels->green * 255 / MaxRGB) << 8)
| (int(pixels->red * 255 / MaxRGB) << 16)
| (int(pixels->blue * 255 / MaxRGB) );
image.SetPixel(cPoint(ix, iy), col);
++pixels;
}
}
return image;
}
bool cImageMagickWrapper::LoadImage(std::string FileName, std::string Path, std::string Extension) {
try {
std::stringstream sstrImgFile;
sstrImgFile << Path << FileName << "." << Extension;
std::string imgFile = sstrImgFile.str();
if (config.debugImageLoading)
dsyslog("skindesigner: trying to load: %s", imgFile.c_str());
buffer.read(imgFile.c_str());
if (config.debugImageLoading)
dsyslog("skindesigner: %s sucessfully loaded", imgFile.c_str());
} catch( Magick::Warning &warning ) {
if (config.debugImageLoading)
dsyslog("skindesigner: Magick Warning: %s", warning.what());
return true;
} catch( Magick::Error &error ) {
if (config.debugImageLoading)
dsyslog("skindesigner: Magick Error: %s", error.what());
return false;
} catch(...) {
if (config.debugImageLoading)
dsyslog("skindesigner: an unknown Magick error occured during image loading");
return false;
}
return true;
}
bool cImageMagickWrapper::LoadImage(const char *fullpath) { bool cImageMagickWrapper::LoadImage(const char *fullpath) {
if ((fullpath == NULL) || (strlen(fullpath) < 5)) if ((fullpath == NULL) || (strlen(fullpath) < 5))
return false; return false;
@ -118,45 +73,10 @@ bool cImageMagickWrapper::LoadImage(const char *fullpath) {
return true; return true;
} }
Color cImageMagickWrapper::Argb2Color(tColor col) { // Just a different way to call LoadImage. Calls the above one.
tIndex alpha = (col & 0xFF000000) >> 24; bool cImageMagickWrapper::LoadImage(std::string FileName, std::string Path, std::string Extension) {
tIndex red = (col & 0x00FF0000) >> 16; std::stringstream sstrImgFile;
tIndex green = (col & 0x0000FF00) >> 8; sstrImgFile << Path << FileName << "." << Extension;
tIndex blue = (col & 0x000000FF); std::string imgFile = sstrImgFile.str();
Color color(MaxRGB*red/255, MaxRGB*green/255, MaxRGB*blue/255, MaxRGB*(0xFF-alpha)/255); return LoadImage(imgFile.c_str());
return color;
}
void cImageMagickWrapper::CreateGradient(tColor back, tColor blend, int width, int height, double wfactor, double hfactor) {
Color Back = Argb2Color(back);
Color Blend = Argb2Color(blend);
int maxw = MaxRGB * wfactor;
int maxh = MaxRGB * hfactor;
Image imgblend(Geometry(width, height), Blend);
imgblend.modifyImage();
imgblend.type(TrueColorMatteType);
PixelPacket *pixels = imgblend.getPixels(0, 0, width, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
PixelPacket *pixel = pixels + y * width + x;
int opacity = (maxw / width * x + maxh - maxh / height * y) / 2;
pixel->opacity = (opacity <= MaxRGB) ? opacity : MaxRGB;
}
}
imgblend.syncPixels();
Image imgback(Geometry(width, height), Back);
imgback.composite(imgblend, 0, 0, OverCompositeOp);
buffer = imgback;
}
void cImageMagickWrapper::CreateBackground(tColor back, tColor blend, int width, int height, bool mirror) {
CreateGradient(back, blend, width, height, 0.8, 0.8);
if (mirror)
buffer.flop();
}
void cImageMagickWrapper::CreateBackgroundReverse(tColor back, tColor blend, int width, int height) {
CreateGradient(back, blend, width, height, 1.3, 0.7);
} }

View File

@ -10,19 +10,14 @@ using namespace Magick;
class cImageMagickWrapper { class cImageMagickWrapper {
private: private:
void CreateGradient(tColor back, tColor blend, int width, int height, double wfactor, double hfactor);
public: public:
cImageMagickWrapper(); cImageMagickWrapper();
~cImageMagickWrapper(); ~cImageMagickWrapper();
protected: protected:
Image buffer; Image buffer;
Color Argb2Color(tColor col);
cImage *CreateImage(int width, int height, bool preserveAspect = true); cImage *CreateImage(int width, int height, bool preserveAspect = true);
cImage CreateImageCopy(void);
bool LoadImage(std::string FileName, std::string Path, std::string Extension); bool LoadImage(std::string FileName, std::string Path, std::string Extension);
bool LoadImage(const char *fullpath); bool LoadImage(const char *fullpath);
void CreateBackground(tColor back, tColor blend, int width, int height, bool mirror = false);
void CreateBackgroundReverse(tColor back, tColor blend, int width, int height);
}; };
#endif //__NOPACITY_IMAGEMAGICKWRAPPER_H #endif //__NOPACITY_IMAGEMAGICKWRAPPER_H

View File

@ -711,8 +711,10 @@ void cView::DoDrawImage(int num, cTemplateFunction *func, int x0, int y0) {
break; } break; }
case itImage: { case itImage: {
cImageLoader imgLoader; cImageLoader imgLoader;
if (imgLoader.LoadImage(path.c_str(), width, height)) { if (imgLoader.LoadImage(path.c_str())) {
DrawImage(num, pos, imgLoader.GetImage()); cImage *image = imgLoader.GetImage(width, height);
DrawImage(num, pos, *image);
delete(image);
} }
break; } break; }
default: default: