/********************************************************************/ /* Copyright (c) 2017 System fugen G.K. and Yuzi Mizuno */ /* All rights reserved. */ /********************************************************************/ #include "MGCLStdAfx.h" #include #include #include #include #include #include #include "mgGL/Color.h" #include "mgGL/Image.h" using namespace Gdiplus; //Set only RGB of pixel2 without updating Alpha data. void MGPixel::setRGB(const MGPixel& pixel2){ unsigned char A=getAlpha(); *this=pixel2; setAlpha(A); } ///Conversion conrtructor from MGColor to MGPixel. MGPixel::MGPixel(const MGColor& color){ float rgba[4]; color.get_color(rgba[0],rgba[1],rgba[2],rgba[3]); for(int i=0; i<4; i++){ if(rgba[i]>1.f) rgba[i]=1.f; else if(rgba[i]<0.f) rgba[i]=0.f; } setRed(unsigned char(rgba[0]*255.)); setGreen(unsigned char(rgba[1]*255.)); setBlue(unsigned char(rgba[2]*255.)); setAlpha(unsigned char(rgba[3]*255.)); } // //Implements MGTextureImages Class. //MGTextureImages defines the attributes of TextureImages. //Garbage image data constructor. MGImage::MGImage(int wdth, int hght) :m_width(wdth),m_height(hght){ m_image=new MGPixel[wdth*hght]; } ///Conversion constructor from Gdiplus::Bitmap. MGImage::MGImage(const MGImage& image2):m_width(image2.width()),m_height(image2.height()), m_image(image2.m_image){ MGImage* im2=const_cast(&image2); im2->m_image=0; } ///Extract a part of image2. MGImage::MGImage( const MGImage& image2, int x, int y, ///(&image2); im2->m_image=0; return *this; } MGPixel& MGImage::operator()(int i, int j){ MGPixel* pixelP=image(); return pixelP[j*m_width+i]; } const MGPixel& MGImage::operator()(int i, int j)const{ const MGPixel* pixelP=image(); return pixelP[j*m_width+i]; } ///Generate a cloned MGImage. ///Returned is newed one, must be deleted. ///clone() does not affect this image data. MGImage* MGImage::clone()const{ MGImage* image2=new MGImage; image2->m_width=m_width; image2->m_height=m_height; size_t tlen=m_width*m_height; image2->m_image=new MGPixel[tlen]; for(size_t i=0; im_image[i]=m_image[i]; } return image2; } //Extract a part of bitmap into this, from(x,y) to (x+width, y+height). void MGImage::extract( Gdiplus::Bitmap& bitmap, int x, int y, //left bottom address of bitmap. int width, int height, double alpha ){ int bmW=bitmap.GetWidth(), bmH=bitmap.GetHeight(); assert(x+width<=bmW); assert(y+height<=bmH); // Change the MGPixel data format MGPixel* pixelP=m_image; if(!pixelP) return; Rect rect(0,0,bmW,bmH); BitmapData bmData; bitmap.LockBits(&rect,ImageLockModeRead,PixelFormat32bppARGB,&bmData); int totalhm1=bmH-1;//total height -1. UINT* gdiPixels=(UINT*)bmData.Scan0;; for(int j=0; j& ranges// Ranges(j,i1,i2) are input. ){ int n=(int)ranges.size(); for(int k3=0; k3& ranges// Ranges(j,i1,i2) are input. ///ranges indicate the places of both this and image2. ){ int n=(int)ranges.size(); for(int k3=0; k3