python - Applying variable transparency to an image in java -
i'm wondering how apply variable mask image's transparency in java (presumably using buffered image). so, instance, want apply mask such following:
to image such following:
in order get:
it's important transparency changed gradient, not color, because want able to kind of thing:
i've found incredibly slow way of achieving going through , setting transparency value of every single pixel manually, said, it's super slow. should looking @ try accomplish this? or should doing in java? i've been getting know python recently, if thinks it's better try sort of thing particular python module, useful information well.
thank in advance!
here 1 way transparent mask.
gradientmask.java:
import utilities.*; import javax.swing.*; import javax.imageio.*; import java.awt.*; import java.awt.image.*; import java.io.*; public class gradientmask { public static void main(string[] args) throws exception{ jframe main = new jframe("gradient mask"); jlabel imagelayer = new jlabel(); jlabel masklayer = new jlabel(); bufferedimage image = imageio.read(new file("c:\\users\\"+system.getenv("username")+"\\desktop\\cat image.jpg")); bufferedimage gradientmask = new gradientimage(image.getwidth(), image.getheight(), new color[]{new color(255, 255, 255, 125), color.black}, gradientimage.radial_from_center).getimage(); main.setdefaultcloseoperation(jframe.exit_on_close); main.setbounds(100, 50, image.getwidth(), image.getheight()); imagelayer.setbounds(0, 0, main.getwidth(), main.getheight()); masklayer.setbounds(0, 0, main.getwidth(), main.getheight()); imagelayer.seticon(new imageicon((image) image)); masklayer.seticon(new imageicon((image) gradientmask)); main.getcontentpane().add(imagelayer); imagelayer.add(masklayer); main.setvisible(true); } }
gradientimage.java:
package utilities; import java.awt.*; import java.awt.image.*; public class gradientimage { public final static int linear_left_to_right = 1; public final static int linear_right_to_left = 2; public final static int linear_top_to_bottom = 3; public final static int linear_bottom_to_top = 4; public final static int linear_diagonal_up = 5; public final static int linear_diagonal_down = 6; public final static int radial_from_top_left_corner = 7; public final static int radial_from_bottom_left_corner = 8; public final static int radial_from_top_right_corner = 9; public final static int radial_from_bottom_right_corner = 10; public final static int radial_from_center = 11; public final static int radial_from_corners = 12; public final static int path_from_center = 13; public final static int path_from_top_left_corner = 14; public final static int path_from_top_right_corner = 15; public final static int path_from_bottom_left_corner = 16; public final static int path_from_bottom_right_corner = 17; public final static int linear_from_top_right_corner = 18; public final static int linear_from_top_left_corner = 19; public final static int linear_from_bottom_right_corner = 20; public final static int linear_from_bottom_left_corner = 21; public final static int linear_from_center = 22; public final static int linear_from_corners = 23; public final static int path_from_corners = 24; private bufferedimage image = null; private bufferedimage circleimage = null; private int[] pixels; private int[] circlepixels; private int[] positions; private color[] colors; private int[] rgbs; private int alignment; private int width; private int height; public gradientimage(int width, int height, color[] colors, int alignment){ image = new bufferedimage(width, height, bufferedimage.type_int_argb); pixels = ((databufferint) image.getraster().getdatabuffer()).getdata(); this.alignment = alignment; this.width = width; this.height = height; this.colors = colors; rgbs = new int[colors.length]; (int i=0;i<rgbs.length;i++){ rgbs[i] = colors[i].getrgb(); } try{ renderimage(); }catch(exception error){error.printstacktrace();} } public gradientimage(int width, int height, color[] colors, int[] positions, int alignment){ image = new bufferedimage(width, height, bufferedimage.type_int_argb); pixels = ((databufferint) image.getraster().getdatabuffer()).getdata(); this.alignment = alignment; this.width = width; this.height = height; this.colors = colors; this.positions = positions; rgbs = new int[colors.length]; (int i=0;i<rgbs.length;i++){ rgbs[i] = colors[i].getrgb(); } try{ renderimage(); }catch(exception error){error.printstacktrace();} } public bufferedimage getimage(){ return image; } public bufferedimage getimageascircle(){ if (circleimage==null){ circleimage = new bufferedimage(width, height, bufferedimage.type_int_argb); circlepixels = ((databufferint) circleimage.getraster().getdatabuffer()).getdata(); int radius = math.min(width, height)>>1; (int x=0;x<width;x++){ (int y=0;y<height;y++){ if (math.sqrt((math.max(width>>1, x)-math.min(width>>1, x))*(math.max(width>>1, x)-math.min(width>>1, x))+(math.max(height>>1, y)-math.min(height>>1, y))*(math.max(height>>1, y)-math.min(height>>1, y)))<=radius){ circlepixels[x+y*width] = pixels[x+y*width]; } } } } return circleimage; } private void renderimage() throws exception{ if (alignment==linear_left_to_right){ int[] rgbrange = loadrgbrange(width, rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+y*width] = rgbrange[x]; } } }else if (alignment==linear_right_to_left){ int[] rgbrange = loadrgbrange(width, rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+y*width] = rgbrange[width-x-1]; } } }else if (alignment==linear_bottom_to_top){ int[] rgbrange = loadrgbrange(height, rgbs, positions); (int y=0;y<height;y++){ (int x=0;x<width;x++){ pixels[x+y*width] = rgbrange[height-y-1]; } } }else if (alignment==linear_top_to_bottom){ int[] rgbrange = loadrgbrange(height, rgbs, positions); (int y=0;y<height;y++){ (int x=0;x<width;x++){ pixels[x+y*width] = rgbrange[y]; } } }else if (alignment==radial_from_top_left_corner){ int[] rgbrange = loadrgbrange((int) math.sqrt((width-1)*(width-1)+(height-1)*(height-1)), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+y*width] = rgbrange[(int) math.sqrt((x-1)*(x-1)+(y-1)*(y-1))]; } } }else if (alignment==radial_from_bottom_left_corner){ int[] rgbrange = loadrgbrange((int) math.sqrt((width-1)*(width-1)+(height-1)*(height-1)), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+(height-y-1)*width] = rgbrange[(int) math.sqrt((x-1)*(x-1)+(y-1)*(y-1))]; } } }else if (alignment==radial_from_top_right_corner){ int[] rgbrange = loadrgbrange((int) math.sqrt((width-1)*(width-1)+(height-1)*(height-1)), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[(width-x-1)+y*width] = rgbrange[(int) math.sqrt((x-1)*(x-1)+(y-1)*(y-1))]; } } }else if (alignment==radial_from_bottom_right_corner){ int[] rgbrange = loadrgbrange((int) math.sqrt((width-1)*(width-1)+(height-1)*(height-1)), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[(width-x-1)+(height-y-1)*width] = rgbrange[(int) math.sqrt((x-1)*(x-1)+(y-1)*(y-1))]; } } }else if (alignment==radial_from_center){ int[] divarray = dividearray(positions, 2); bufferedimage quad1 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.radial_from_bottom_right_corner).getimage(); bufferedimage quad2 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.radial_from_bottom_left_corner).getimage(); bufferedimage quad3 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.radial_from_top_right_corner).getimage(); bufferedimage quad4 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.radial_from_top_left_corner).getimage(); graphics2d g = image.creategraphics(); g.drawimage(quad1, 0, 0, null); g.drawimage(quad2, width>>1, 0, null); g.drawimage(quad3, 0, height>>1, null); g.drawimage(quad4, width>>1, height>>1, null); g.dispose(); }else if (alignment==radial_from_corners){ int[] divarray = dividearray(positions, 2); bufferedimage quad1 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.radial_from_top_left_corner).getimage(); bufferedimage quad2 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.radial_from_top_right_corner).getimage(); bufferedimage quad3 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.radial_from_bottom_left_corner).getimage(); bufferedimage quad4 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.radial_from_bottom_right_corner).getimage(); graphics2d g = image.creategraphics(); g.drawimage(quad1, 0, 0, null); g.drawimage(quad2, width>>1, 0, null); g.drawimage(quad3, 0, height>>1, null); g.drawimage(quad4, width>>1, height>>1, null); g.dispose(); }else if (alignment==linear_diagonal_up){ int[] rgbrange = loadrgbrange((int) math.sqrt((width-1)*(width-1)+(height-1)*(height-1)), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+(height-y-1)*width] = rgbrange[math.max(y-x, x-y)]; } } }else if (alignment==linear_diagonal_down){ int[] rgbrange = loadrgbrange((int) math.sqrt((width-1)*(width-1)+(height-1)*(height-1)), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+y*width] = rgbrange[math.max(y-x, x-y)]; } } }else if (alignment==linear_from_top_right_corner){ int[] rgbrange = loadrgbrange((width+height)>>1, rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+(height-y-1)*width] = rgbrange[rgbrange.length-((x+y)>>1)-1]; } } }else if (alignment==linear_from_top_left_corner){ int[] rgbrange = loadrgbrange((width+height)>>1, rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[(width-x-1)+(height-y-1)*width] = rgbrange[rgbrange.length-((x+y)>>1)-1]; } } }else if (alignment==linear_from_bottom_right_corner){ int[] rgbrange = loadrgbrange((width+height)>>1, rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+y*width] = rgbrange[rgbrange.length-((x+y)>>1)-1]; } } }else if (alignment==linear_from_bottom_left_corner){ int[] rgbrange = loadrgbrange((width+height)>>1, rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[(width-x-1)+y*width] = rgbrange[rgbrange.length-((x+y)>>1)-1]; } } }else if (alignment==linear_from_center){ int[] divarray = dividearray(positions, 2); bufferedimage quad1 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.linear_from_bottom_right_corner).getimage(); bufferedimage quad2 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.linear_from_bottom_left_corner).getimage(); bufferedimage quad3 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.linear_from_top_right_corner).getimage(); bufferedimage quad4 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.linear_from_top_left_corner).getimage(); graphics2d g = image.creategraphics(); g.drawimage(quad1, 0, 0, null); g.drawimage(quad2, width>>1, 0, null); g.drawimage(quad3, 0, height>>1, null); g.drawimage(quad4, width>>1, height>>1, null); g.dispose(); }else if (alignment==linear_from_corners){ int[] divarray = dividearray(positions, 2); bufferedimage quad1 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.linear_from_top_left_corner).getimage(); bufferedimage quad2 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.linear_from_top_right_corner).getimage(); bufferedimage quad3 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.linear_from_bottom_left_corner).getimage(); bufferedimage quad4 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.linear_from_bottom_right_corner).getimage(); graphics2d g = image.creategraphics(); g.drawimage(quad1, 0, 0, null); g.drawimage(quad2, width>>1, 0, null); g.drawimage(quad3, 0, height>>1, null); g.drawimage(quad4, width>>1, height>>1, null); g.dispose(); }else if (alignment==path_from_top_left_corner){ int[] rgbrange = loadrgbrange(math.max(width, height), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+y*width] = rgbrange[math.max(x, y)]; } } }else if (alignment==path_from_top_right_corner){ int[] rgbrange = loadrgbrange(math.max(width, height), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[(width-x-1)+y*width] = rgbrange[math.max(x, y)]; } } }else if (alignment==path_from_bottom_left_corner){ int[] rgbrange = loadrgbrange(math.max(width, height), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[x+(height-y-1)*width] = rgbrange[math.max(x, y)]; } } }else if (alignment==path_from_bottom_right_corner){ int[] rgbrange = loadrgbrange(math.max(width, height), rgbs, positions); (int x=0;x<width;x++){ (int y=0;y<height;y++){ pixels[(width-x-1)+(height-y-1)*width] = rgbrange[math.max(x, y)]; } } }else if (alignment==path_from_center){ int[] divarray = dividearray(positions, 2); bufferedimage quad1 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.path_from_bottom_right_corner).getimage(); bufferedimage quad2 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.path_from_bottom_left_corner).getimage(); bufferedimage quad3 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.path_from_top_right_corner).getimage(); bufferedimage quad4 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.path_from_top_left_corner).getimage(); graphics2d g = image.creategraphics(); g.drawimage(quad1, 0, 0, null); g.drawimage(quad2, width>>1, 0, null); g.drawimage(quad3, 0, height>>1, null); g.drawimage(quad4, width>>1, height>>1, null); g.dispose(); }else if (alignment==path_from_corners){ int[] divarray = dividearray(positions, 2); bufferedimage quad1 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.path_from_top_left_corner).getimage(); bufferedimage quad2 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.path_from_top_right_corner).getimage(); bufferedimage quad3 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.path_from_bottom_left_corner).getimage(); bufferedimage quad4 = new gradientimage(width>>1, height>>1, colors, divarray, gradientimage.path_from_bottom_right_corner).getimage(); graphics2d g = image.creategraphics(); g.drawimage(quad1, 0, 0, null); g.drawimage(quad2, width>>1, 0, null); g.drawimage(quad3, 0, height>>1, null); g.drawimage(quad4, width>>1, height>>1, null); g.dispose(); } } public int[] dividearray(int[] array, int div){ if (array==null){ return null; } int[] arr = new int[array.length]; if (div==2){ (int i=0;i<arr.length;i++){ arr[i] = array[i]>>1; } }else{ (int i=0;i<arr.length;i++){ arr[i] = array[i]/div; } } return arr; } public int[] loadrgbrange(int length, int[] rgbs) throws exception { if (rgbs==null){ throw new exception("rgb[]'s cannot null"); } if (length==0){ throw new exception("length cannot 0"); } if (rgbs.length==0){ throw new exception("rgb[]'s length cannot 0"); } int[] rgbrange = new int[length]; if (rgbs.length==1){ (int i=0;i<rgbrange.length;i++){ rgbrange[i] = rgbs[0]; } return rgbrange; } int[] positions = new int[rgbs.length]; double pos = 0; double block = (double) length/(rgbs.length-1); (int i=0;i<positions.length;i++){ positions[i] = (int) pos; pos+=block; } int[] = new int[rgbs.length]; int[] rs = new int[rgbs.length]; int[] gs = new int[rgbs.length]; int[] bs = new int[rgbs.length]; (int i=0;i<rgbs.length;i++){ as[i] = (rgbs[i]>>24) & 0xff; rs[i] = (rgbs[i]>>16) & 0xff; gs[i] = (rgbs[i]>>8) & 0xff; bs[i] = (rgbs[i]) & 0xff; } int[] adifs = new int[rgbs.length-1]; int[] rdifs = new int[rgbs.length-1]; int[] gdifs = new int[rgbs.length-1]; int[] bdifs = new int[rgbs.length-1]; (int i=0;i<rgbs.length-1;i++){ adifs[i] = as[i]-as[i+1]; rdifs[i] = rs[i]-rs[i+1]; gdifs[i] = gs[i]-gs[i+1]; bdifs[i] = bs[i]-bs[i+1]; } double[] ab = new double[rgbs.length-1]; double[] rb = new double[rgbs.length-1]; double[] gb = new double[rgbs.length-1]; double[] bb = new double[rgbs.length-1]; (int i=0;i<rgbs.length-1;i++){ int l = positions[i+1]-positions[i]; ab[i] = (double) adifs[i]/l; rb[i] = (double) rdifs[i]/l; gb[i] = (double) gdifs[i]/l; bb[i] = (double) bdifs[i]/l; } double = as[0]; double r = rs[0]; double g = gs[0]; double b = bs[0]; int color = 0; (int i=0;i<rgbrange.length;i++){ rgbrange[i] = ((int)a<<24)|((int)r<<16)|((int)g<<8)|((int)b); if (i+1>positions[0] && i+1<positions[positions.length-1]){ if (i==positions[color+1]){ color++; = as[color]; r = rs[color]; g = gs[color]; b = bs[color]; }else{ a-=ab[color]; r-=rb[color]; g-=gb[color]; b-=bb[color]; } } } return rgbrange; } public int[] loadrgbrange(int length, int[] rgbs, int[] positions) throws exception { if (positions==null){ return loadrgbrange(length, rgbs); } if (rgbs==null){ throw new exception("rgb[]'s cannot null"); } if (length==0){ throw new exception("length cannot 0"); } if (rgbs.length==0 || positions.length==0){ return null; } if (positions.length!=rgbs.length){ throw new exception("the length of positions[] must equals length of rgb[]'s"); } (int i=0;i<positions.length;i++){ if (positions[i]>length){ throw new exception("any positions cannot greater length"); } } int[] rgbrange = new int[length]; if (rgbs.length==1){ (int i=0;i<rgbrange.length;i++){ rgbrange[i] = rgbs[0]; } return rgbrange; } int[] = new int[rgbs.length]; int[] rs = new int[rgbs.length]; int[] gs = new int[rgbs.length]; int[] bs = new int[rgbs.length]; (int i=0;i<rgbs.length;i++){ as[i] = (rgbs[i]>>24) & 0xff; rs[i] = (rgbs[i]>>16) & 0xff; gs[i] = (rgbs[i]>>8) & 0xff; bs[i] = (rgbs[i]) & 0xff; } int[] adifs = new int[rgbs.length-1]; int[] rdifs = new int[rgbs.length-1]; int[] gdifs = new int[rgbs.length-1]; int[] bdifs = new int[rgbs.length-1]; (int i=0;i<rgbs.length-1;i++){ adifs[i] = as[i]-as[i+1]; rdifs[i] = rs[i]-rs[i+1]; gdifs[i] = gs[i]-gs[i+1]; bdifs[i] = bs[i]-bs[i+1]; } double[] ab = new double[rgbs.length-1]; double[] rb = new double[rgbs.length-1]; double[] gb = new double[rgbs.length-1]; double[] bb = new double[rgbs.length-1]; (int i=0;i<rgbs.length-1;i++){ int l = positions[i+1]-positions[i]; ab[i] = (double) adifs[i]/l; rb[i] = (double) rdifs[i]/l; gb[i] = (double) gdifs[i]/l; bb[i] = (double) bdifs[i]/l; } double = as[0]; double r = rs[0]; double g = gs[0]; double b = bs[0]; int color = 0; (int i=0;i<rgbrange.length;i++){ rgbrange[i] = ((int)a<<24)|((int)r<<16)|((int)g<<8)|((int)b); if (i+1>positions[0] && i+1<positions[positions.length-1]){ if (i==positions[color+1]){ color++; = as[color]; r = rs[color]; g = gs[color]; b = bs[color]; }else{ a-=ab[color]; r-=rb[color]; g-=gb[color]; b-=bb[color]; } } } return rgbrange; } }
Comments
Post a Comment