Browse Source

图片alpha处理与透明处理

master
blobt 5 years ago
parent
commit
f3d7ae9dd9
  1. 40
      src/Raster.cc
  2. 3
      src/Raster.h
  3. 1
      src/common.h
  4. 24
      src/draw_image.cc

40
src/Raster.cc

@ -377,6 +377,42 @@ void Raster::drawImageWithKey(int startX, int startY, const Image* image, Rgba k
}
}
void Raster::drawImageAlphaTest(int startX, int startY, const Image* image, byte alpha) {
/*越界部分不描画*/
int left = tmax<int>(startX, 0);
int bottom = tmax<int>(startY, 0);
int right = tmin<int>(startX + image->width(), _width);
int top = tmin<int>(startY + image->height(), _height);
for (int x = left; x < right; x++) {
for (int y = bottom; y < top; y++) {
Rgba color = image->pixelAt(x - left, y - bottom);
if (color._a > alpha) {
setPixel(x, y, color);
}
}
}
}
void Raster::drawImageAlphaBlend(int startX, int startY, const Image* image, float alpha) {
/*越界部分不描画*/
int left = tmax<int>(startX, 0);
int bottom = tmax<int>(startY, 0);
int right = tmin<int>(startX + image->width(), _width);
int top = tmin<int>(startY + image->height(), _height);
for (int x = left; x < right; x++) {
for (int y = bottom; y < top; y++) {
Rgba dstColor = image->pixelAt(x - left, y - bottom);
Rgba srcColor = getPixel(x, y);
Rgba color = colorLerp(srcColor, dstColor, dstColor._a / 255.0f * alpha);
setPixel(x, y, color);
}
}
}
int Raster::size() {
return _width * _height * sizeof (Rgba);
}
@ -393,6 +429,10 @@ bool Raster::setPixel(int x, int y, Rgba color) {
buffer[ ((_height - 1) - y) * _width + x] = color;
}
Rgba Raster::getPixel(int x, int y) {
return buffer[ ((_height - 1) - y) * _width + x];
}
bool Raster::isIn(int2 point) {
if (point.x >= 0 && point.x <= _width && point.y >= 0 && point.y < _height) {
return true;

3
src/Raster.h

@ -97,9 +97,12 @@ public:
void drawRect(const int2* points, const Rgba* colors);
void drawImage(int startX, int startY, const Image* image);
void drawImageWithKey(int startX, int startY, const Image* image, Rgba key);
void drawImageAlphaTest(int startX, int startY, const Image* image, byte key);
void drawImageAlphaBlend(int startX, int startY, const Image* image, float alpha = 1);
int size();
void clean();
bool setPixel(int x, int y, Rgba color);
Rgba getPixel(int x, int y);
bool isIn(int2 point);
private:
int _width;

1
src/common.h

@ -65,6 +65,7 @@ struct tvec2 {
typedef tvec2<float> float2;
typedef tvec2<int> int2;
typedef unsigned char byte;
#endif /* COMMON_H */

24
src/draw_image.cc

@ -17,15 +17,21 @@ gint width = 800;
Raster raster(width, height);
void example1() {
//画背景
Image* image1 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/bg.png");
raster.drawImage(0, 0, image1);
delete image1;
//画前景
Image* image2 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/colorKey.bmp");
raster.drawImageWithKey(200, 250, image2, Rgba(255, 0, 0));
delete image2;
//画背景
Image* image1 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/bg.png");
raster.drawImage(0, 0, image1);
delete image1;
//画前景
Image* image2 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/colorKey.bmp");
raster.drawImageWithKey(200, 250, image2, Rgba(255, 0, 0));
delete image2;
//画草
Image* image3 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/grass.png");
//raster.drawImageAlphaTest(400, 250, image3, 100);
raster.drawImageAlphaBlend(400, 250, image3,0.5);
delete image3;
}

Loading…
Cancel
Save