放点东西存一下

发布时间:2021-07-19 12:11:22
贴主:李林森
热度:1

李林森 2021-07-19

1.

利用指针快速修改原图像所选区域的透明度

规定r,g,b>50 也就是把灰色到白色之间的颜色对应的像素点变透明

并保存为新的.png图片

 

#include<iostream>

#include<atlimage.h>

using namespace std;

int main()

{

    LPCTSTR srcFilePath2 = _T("5a.jpg"), destFilePath = _T("5a.png");

    CImage srcImage, destImage;

    srcImage.Load(srcFilePath2);

    int width = srcImage.GetWidth(), height = srcImage.GetHeight();

    int bpp1 = srcImage.GetBPP(), pitch1 = srcImage.GetPitch();

    destImage.Create(width, height, 32, destImage.createAlphaChannel);

    int bpp2 = destImage.GetBPP(), pitch2 = destImage.GetPitch();

    BYTE* pData1 = (BYTE*)srcImage.GetBits(), * pData2 = (BYTE*)destImage.GetBits();

    for (int x = 0; x < width; x++)

    {

       for (int y = 0; y < height; y++)

       {

           int r = *(pData2 + pitch2 * y + x * bpp2 / 8 + 0) = *(pData1 + pitch1 * y + x * bpp1 / 8 + 0);

           int g = *(pData2 + pitch2 * y + x * bpp2 / 8 + 1) = *(pData1 + pitch1 * y + x * bpp1 / 8 + 1);

           int b = *(pData2 + pitch2 * y + x * bpp2 / 8 + 2) = *(pData1 + pitch1 * y + x * bpp1 / 8 + 2);

           if (r > 50 && g > 50 && b > 50)

           {

              *(pData2 + pitch2 * y + x * bpp2 / 8 + 3) = 0;

           }

           else

           {

              *(pData2 + pitch2 * y + x * bpp2 / 8 + 3) = 255;

           }

       }

    }

    destImage.Save(destFilePath);

    return 0;

}

//修改文字背景为透明,保存为5a.png

 

2.

给图片打上水印

利用指针快速将水印上不透明的像素点的颜色值赋值给图片

并保存为新的.png图片

#include<iostream>

#include<atlimage.h>

using namespace std;

void merge(LPCTSTR srcFilePath1, LPCTSTR srcFilePath2, LPCTSTR destFilePath)

{

    CImage srcImage1, srcImage2;

    srcImage1.Load(srcFilePath1);

    srcImage2.Load(srcFilePath2);

    int width1 = srcImage1.GetWidth(), height1 = srcImage1.GetHeight();

    int width2 = srcImage2.GetWidth(), height2 = srcImage2.GetHeight();

    if (width1 < width2 || height1 < height2) { exit(1); }

    int bpp1 = srcImage1.GetBPP(), pitch1 = srcImage1.GetPitch();

    int bpp2 = srcImage2.GetBPP(), pitch2 = srcImage2.GetPitch();

    BYTE* pData1 = (BYTE*)srcImage1.GetBits(), * pData2 = (BYTE*)srcImage2.GetBits();

    for (int x = 0; x < width2; x++)

    {

       for (int y = 0; y < height2; y++)

       {

           if (*(pData2 + pitch2 * y + x * bpp2 / 8 + 3) == 255)

           {

              *(pData1 + pitch1 * y + x * bpp1 / 8 + 0) = *(pData2 + pitch2 * y + x * bpp2 / 8 + 0);

              *(pData1 + pitch1 * y + x * bpp1 / 8 + 1) = *(pData2 + pitch2 * y + x * bpp2 / 8 + 1);

              *(pData1 + pitch1 * y + x * bpp1 / 8 + 2) = *(pData2 + pitch2 * y + x * bpp2 / 8 + 2);

           }

       }

    }

    srcImage1.Save(destFilePath);

}

int main()

{

    LPCTSTR srcFilePath1 = _T("4a.jpg"), srcFilePath2 = _T("5a.png"), destFilePath = _T("5b-2.jpg");

    merge(srcFilePath1, srcFilePath2, destFilePath);

    return 0;

}

 

3.

由于水印在图片上的某处显示效果不佳

现将水印与背景取反色,增强对比度

利用指针将限定范围内的原图的颜色值赋值成与背景取反色的水印的颜色值

并保存为新的.png图片

 

#include<iostream>

#include<atlimage.h>

using namespace std;

void merge(LPCTSTR srcFilePath1, LPCTSTR srcFilePath2, LPCTSTR destFilePath)

{

    CImage srcImage1, srcImage2;

    srcImage1.Load(srcFilePath1);

    srcImage2.Load(srcFilePath2);

    int width1 = srcImage1.GetWidth(), height1 = srcImage1.GetHeight();

    int width2 = srcImage2.GetWidth(), height2 = srcImage2.GetHeight();

    if (width1 < width2 || height1 < height2) { exit(1); }

    int bpp1 = srcImage1.GetBPP(), pitch1 = srcImage1.GetPitch();

    int bpp2 = srcImage2.GetBPP(), pitch2 = srcImage2.GetPitch();

    BYTE* pData1 = (BYTE*)srcImage1.GetBits(), * pData2 = (BYTE*)srcImage2.GetBits();

    for (int x = width1 - 1; x > 0; x--)

    {

       for (int y = height1 - 1; y > 0; y--)

       {

           if ((x >= width1 - width2 && y >= height1 - height2) && *(pData2 + pitch2 * (y - height1 + height2) + (x - width1 + width2) * bpp2 / 8 + 3) == 255)

           {

              int b = *(pData1 + pitch1 * y + x * bpp1 / 8 + 0);

              int g = *(pData1 + pitch1 * y + x * bpp1 / 8 + 1);

              int r = *(pData1 + pitch1 * y + x * bpp1 / 8 + 2);

              *(pData1 + pitch1 * y + x * bpp1 / 8 + 0) = 255 - b;

              *(pData1 + pitch1 * y + x * bpp1 / 8 + 1) = 255 - g;

              *(pData1 + pitch1 * y + x * bpp1 / 8 + 2) = 255 - r;

 

           }

       }

    }

    srcImage1.Save(destFilePath);

}

int main()

{

    LPCTSTR srcFilePath1 = _T("4a.jpg"), srcFilePath2 = _T("5a.png"), destFilePath = _T("5b-4.jpg");

    merge(srcFilePath1, srcFilePath2, destFilePath);

    return 0;

}

 

4.

现水印过于显眼

原处理方案为:改变水印的透明度信息,使其淡化

遇到了不知名bug,改变alpha值之后,RGB值也会更改,使得水印颜色发生变化

退而求其次,为达到预想的效果,现从颜色入手,减小水印与图片背景的颜色差距

(0)


Copyright 2016 - 2024 XUJC ACM Team
闽ICP备2020022076号-1