首页 > 编程语言 >Java SWT Image 图像 —— 变灰、变亮变黑、旋转、反色、拉伸、透明叠加

Java SWT Image 图像 —— 变灰、变亮变黑、旋转、反色、拉伸、透明叠加

时间:2023-10-13 13:07:46浏览次数:29  
标签:变亮 srcData int Image 图像 bytesPerPixel width 反色 data


图像变灰

图像变灰在桌面应用程序中有着广泛的应用。例如,一个图标被作为一个按钮的背景,我们需要一个灰色效果的图标作为按钮的背景来表示这个按钮处于禁用状态。在SWT中,基于已经存在的图像来创建一个具有灰色效果的图像,我们可以使用构造函数 Image(Display display, Image image, int flag) 来创建,其中参数 flag 使用 SWT.IMAGE_GRAY。

 

例子:


Image newImage = new Image(null, image, SWT.IMAGE_GRAY);

 图像变亮/变黑

下面讨论图像变亮/变黑。 RGB 和 HSL (也叫 HSB/HSV )是两种色彩空间,即:红、绿、蓝( Red, Green, Blue) 和色调、饱和度、亮度( Hue, Saturation, Lightness 或 Brightness 或 Value),前者适用于机器采样,目前的显示器颜色即由这三种基色构成,而后者更符合人类的直观感觉。在 Windows 的标准颜色对话框中均包含这两种表示方法。 RGB 的取值范围在 0~255 之间, HSL 的取值在 0~1 之间。因此我们只需要将 HSL 空间数据的 L 分量进行调整即可调整此图像的亮度。要实现图像变亮/变黑的功能,只需要调整清单 3 中函数 lightImage 行(*)中等号右边的值( 0 到 1 之间)。

 

 例子:

private static ImageData lightImage(ImageData srcData) {
		double[] data = rgbTohsl(srcData.data);
		byte[] newData = new byte[srcData.data.length];
		int bytesPerPixel = srcData.bytesPerLine / srcData.width;
		int destBytesPerLine = srcData.width * bytesPerPixel;
		for(int i = 0; i < data.length; i += 3) {
			data[i + 2] = 0.75;  ----------------- (*)
		}
		data = hslTorgb(data);
		for(int i = 0; i < srcData.data.length; i ++) {
			newData[i] = (byte)data[i];
		}
		ImageData newImageData = new ImageData(srcData.width, srcData.height,
				srcData.depth,srcData.palette, destBytesPerLine,  newData);
		return newImageData;
	}

 

其中,方法 double[] rgbTohsl(byte[] data) 是把 RGB 空间数据转换到 HSL 空间;相反的,方法 double[] hslTorgb(double[] data) 是把数据从 HSL 空间转换到 RGB 空间。

 

图像旋转

清单4中方法 rotate 实现了将图像相左旋转 90 度。如图 1 ,对于像素点 (x, y) ,向左旋转90 度以后,它在图象中的位置变成了 (y, width - x - 1) 。因此,相左旋转 90 度即将所有的像素点按照规则换一下位置。其他的旋转可用同样的方法。

图1 旋转前与相左旋转 90 度后

例子:

private static ImageData rotate(ImageData srcData) {
    int bytesPerPixel = srcData.bytesPerLine / srcData.width;
    int destBytesPerLine = srcData.height * bytesPerPixel;
    byte[] newData = new byte[srcData.data.length];
    int width = 0, height = 0;
    for (int srcY = 0; srcY < srcData.height; srcY++) {
      for (int srcX = 0; srcX < srcData.width; srcX++) {
      int destX = 0, destY = 0, destIndex = 0, srcIndex = 0;
      destX = srcY;
      destY = srcData.width - srcX - 1;
      width = srcData.height;
      height = srcData.width;
      destIndex = (destY * destBytesPerLine) + (destX * bytesPerPixel);
      srcIndex = (srcY * srcData.bytesPerLine)  + (srcX * bytesPerPixel);
      System.arraycopy(srcData.data, srcIndex, newData, destIndex, bytesPerPixel);
      }
    }

     return new ImageData(width, height, srcData.depth, srcData.palette,
        destBytesPerLine, newData);
}

 

 

标签:变亮,srcData,int,Image,图像,bytesPerPixel,width,反色,data
From: https://blog.51cto.com/u_16298170/7843210

相关文章

  • 将docker里的所有正在运行的docker容器commit成images镜像并推送至harbor仓库里
    脚本一需求:将本地服务器上正在运行的所有容器打包成镜像并上传至Harbor仓库下的指定项目里  #!/bin/bash#设置Harbor仓库的地址和凭据HARBOR_URL="192.168.1.55:88"HARBOR_USERNAME="admin"HARBOR_PASSWORD="Harbor12345"HARBOR_PROJECT="m55all-images"#登录到Harbor仓......
  • 【论文阅读】MD vs FA: Adversarial learning for small object segmentation in infr
    WangH,ZhouL,WangL.Missdetectionvs.falsealarm:Adversariallearningforsmallobjectsegmentationininfraredimages[C]//ProceedingsoftheIEEE/CVFInternationalConferenceonComputerVision.2019:8509-8518.论文试图解决什么问题?解决红外小目标的目标......
  • 2023ICCV_Retinexformer: One-stage Retinex-based Transformer for Low-light Image
    一.Motivation(1)Retinex理论没有考虑到噪声,并且基于Retinex分解的网络通常需要很多阶段训练。(2)直接使用从CNN从低光图像到正常光图像的映射忽略了人类的颜色感知,CNN更适合捕获局部信息,对于捕获远程依赖和非局部自相似性方面存在局限。二.Contribution(1)设计了一个阶段......
  • Firefly Image 2 Model 新增​功能
    FireflyImage2Model(Beta)现​已​可用通过​更高​质量​的​图像​和​插图​生成,​以及​自​定义​选项​和​改进​的​动态​范围,​更​快​地​实现​您​的​创意​愿景。立即​尝试FireflyImage2(Beta)轻松​尝试​不同​的​样式通过​将​现有​图......
  • Unity完美像素Sprite:怎么让图片变得清晰(转载) Unity Pixel Perfect Sprite: How To Ach
    https://gamedevelopertips.com/unity-pixel-perfect-sprite/SoIwasmakingalittleprototypeformynewgamewhenIjustcameacrossalittleproblem.ThespritethatIwasloadingintoUnitywasjustlookingtoopixelatedandnotsharpatall.SoIspen......
  • Android入门教程 | ImageView 图片显示
    为了让App界面更美观生动,我们可以放上图片。显示图片是“刚需”。不论是书籍,报纸,网站,都有显示图片的需求。毕竟“无图无真相”。在Android应用开发中,我们通常使用ImageView来显示图片。ImageView的主要属性ImageView的使用ImageView经常用来显示图片。例如直接显示drawable里......
  • python报错cannot import name ‘compare_ssim‘ from ‘skimage.measure‘
    问题原因: scikit-image版本过高导致 解决办法: 重装,安装低版本 pipuninstallscikit-imagepipinstallscikit-image==0.15.0-U-ihttps://pypi.tuna.tsinghua.edu.cn/simple ......
  • Go - Resizing an Image
    Problem: Youwanttoresizeanimage,makingitlargerorsmaller.Solution: Convertanimagetoagridofpixelsasthesourceandcreateanewimagewiththeresizeddimensions.Usethenearestneighborinterpolationalgorithmtofigureoutthecoloro......
  • Go - Converting an Image to Grayscale
    Problem: Youwanttoconverttheimagetograyscale.Solution: Convertanimagetoagridofpixels.Takeeachpixelinthegridandconvertittoagraypixelaccordingtotherelativeluminanceformula.Convertthegridofpixelsbackintoanimageto......
  • Go - Flipping an Image Upside Down
    Problem: Youwanttoflipanimageupsidedown.Solution: Convertanimagetoagridofpixels.Swapthepositionsofthetopandbottompairsofpixelsandmovedowntoswapthenextpairuntilallthepixelpositionsareswapped.Convertthegridofpi......