//
/* ----------------------------------------------------------------------------------------------
    watermark.java		電子透かし							ver. 1.00 (JDK 1.02)
    														ueyama@infonet.co.jp  2001.07.29
---------------------------------------------------------------------------------------------- */

import java.applet.Applet;
import java.awt.*;
import java.lang.Math;
import java.util.*;
import java.awt.image.*;


public class watermark extends Applet
{
	int			Wm[][]={{ 1359225219,  739519490,  696273698},{ 1157640306,  361294033,  679510016},	// 透かしパターン
						{ 1081632880,  722485268,   13165348},{-1302330708,-1558445562,-1071564479},
						{  203719808,  822940224,  -96196429},{ -661209070,   33701168,  644817984},
						{-2013134526,-1044299260,  390513669},{ 1179914432,-2012477409,  134587904},
						{  572543648,  403858280,-1594358780},{-2100946808,   18932481,-1920972532},
						{-1874705408,  174264956, -116840400},{  411079304, 1116079712, -939261536},
						{-1995210622,   39453696,-1004525689},{       1024,  643827733, -955244534},
						{ -523397564,   25219202,  419430883},{ -231103552,-2128895356,  276988164},
						{        163,     533516,-1879029760},{  155238484,     958466,   73431085},
						{ 1630658568,  763760260,  332400672},{  138423300,-1272942576, 1074677444},
						{ 1235222944,    9302048,   39442122},{  201523509,  560562250,   46142596},
						{   17109601,   35870212,-1810657128},{-1878667968,   16813132,-1164344536},
						{   21123076,   67469580,    5378560},{-1807219048, 1746960451, -531599092},
						{  673977408,-1799351535,-1054594796},{ -297756646, 1079777936,   14970016},
						{-1032695019,   67176722,  151259349},{   36962816,   30017041,  139018316},
						{  541459073, -786676984,  557842688},{ 1192804936, -662015858, 1105690657},
						{   25356290,   56681024,-2113617913},{ 1360005137, 1644954433,-1515134198},
						{ 1610647088, 1898284225, 1882788864},{  559092292,  572621414, 1074539588},
						{    3279945,    7361566,  109065964},{ 1345599616, -994046876,   69272210},
						{  352848100,-1339291838, 1212238338},{   92341174,  604284292,   54692867},
						{ 1145225224,  152080803,  377915584},{-1035925886, -964472429,  343965988},
						{-1587436271,  302286722,    3410339},{  273000452,  607395888,-2147280864},
						{  512246185,-2071961468,  322974259},{-2078125913,-1240178688,  273089417},
						{ 1816592674,  510691328,   67380656},{   84083680,-2008886517,  542385788},
						{ 1208025874,  139199832,-1538882483},{ -127780791,-2008476895,   19137249},
						{ 1099308058,  146805344, -939441324},{-2121037750,  805390470, 1617015817},
						{  646988627,  806963334, 1890583128},{-2100523008,  270090526,  674109896},
						{    1594113,  390134088,   84494480},{ 1494780984, 1610635324,  138679396},
						{  139365440,   18759729, 1627818786},{  305694344,-1717247231,-2146688475},
						{  329266824,  631365655,   80742564},{ 1084693761,   16974088,  289654872},
						{ 1080445090,-2079749760,-1039794036},{  721645774,  805339648,  439769242},
						{  202006789,-2139033598,-2147344367},{    4980752,-1065336800, -465358078},
						{ 2021524532,-2010904576,-2147075296},{ 1225351992,  488640516,    1337554},
						{-2013263576,   84819977,-1819117552},{-1006475239, 1082130897,  822430724},
						{ -534867699,-1296031453,  147720897},{  105907592, -788512507,  679551425}};
	int			Wb[][]=new int[96][70];
	int			Ws=1;												// 透かし表示のつよさ
	int			Data[]=new int[384*280];							// 写真データ
	boolean		Bs[]={true, false, true, false, false, false, false};
	int			Bgc;
	Graphics	gr;
	Image		Img, Gif;
	Random		Rnd=new Random();
					

	public void init()
	{
		int i,j,k,l;
		String	Bg=getParameter("BgColor");
		Bgc=Integer.valueOf(Bg,16).intValue();
		setBackground(new Color(Bgc)); 
		MediaTracker mt=new MediaTracker(this);
		Img=getImage(getCodeBase(), getParameter("figure"));
		mt.addImage(Img, 0);
		Gif=getImage(getCodeBase(), getParameter("button"));
		mt.addImage(Gif, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		gr=getGraphics();
		for(i=0;i<96;i++) for(j=0;j<70;j++) Wb[i][j]=Rnd.nextInt()%3;
		pxgr();
	}
	
	public boolean pxgr()
	{
		PixelGrabber pg=new PixelGrabber(Img,0,0,384,280,Data,0,384);
		try
		{
			pg.grabPixels();
		}
		catch (InterruptedException ie) {return false;}
		return true;
	}
	
	public boolean inside(int x, int a, int b)
	{
		return (x<=Math.max(a,b) && x>=Math.min(a,b)) ? true:false;
	}
	
	public void dsp_b(int m, int n)
	{
		int			ox=0, oy=0, x=0, y=0, w=0, h=0;
		Graphics	gx=gr.create();
		switch(m)
		{
			case  0: x= 77; y=297; ox=  0; oy= 0; w=26; h=23; break;	// "P"
			case  1: x=119; y=297; ox=  0; oy=23; w=26; h=23; break;	// "0"
			case  2: x=149; y=297; ox= 52; oy= 0; w=26; h=23; break;	// "1"
			case  3: x=179; y=297; ox= 52; oy=23; w=26; h=23; break;	// "2"
			case  4: x=209; y=297; ox=104; oy= 0; w=26; h=23; break;	// "4"
			case  5: x=239; y=297; ox=104; oy=23; w=26; h=23; break;	// "8"
			case  6: x=281; y=297; ox=156; oy= 0; w=26; h=23; break;	// "W"
			case  7: x= 77; y=285; ox=156; oy=23; w=24; h=11; break;	// "写真"
			case  8: x=278; y=285; ox=180; oy=23; w=34; h=11; break;	// "透し"
			case  9: x=180; y=284; ox=156; oy=34; w=33; h=11; break;	// "つよさ"
		}
		gx.clipRect(x,y,w,h);
		gx.drawImage(Gif,x-(ox+w*n),y-oy,this);
		gx.dispose();
	}
		
	public void dsp_wm(int m, boolean p)							// 透かしの表示
	{
		int x0,x1,y, i,j, r=0,g=0,b=0, c,w;
		long f;
		
		if(p) gr.drawImage(Img,0,0,384,280,this);
		else {gr.setColor(new Color(204,204,204)); gr.fillRect(0,0,384,280);}
		
		for(y=0;y<70;y++) for(x0=0;x0<3;x0++)
		{
			f=2147483648l;
			for(x1=0;x1<32;x1++)
			{
				if((f & Wm[y][x0])>0)
				{
					w=Wb[x0*32+x1][y]*m;
					for(i=0;i<3;i++) for(j=0;j<3;j++)
					{
						if(p)
						{
							c=Data[(y*4+j)*384+(x0*32+x1)*4+i]&0x00ffffff;
							r=c/65536; g=c/256; g%=256; b=c%256;
						}
						else
						{
							r=204; g=204; b=204;
						}
						r+=w; if(r>255) r=255; if(r<0) r=0;
						g+=w; if(g>255) g=255; if(g<0) g=0;
						b+=w; if(b>255) b=255; if(b<0) b=0;
						gr.setColor(new Color(r,g,b));
						gr.drawRect((x0*32+x1)*4+i, y*4+j, 1, 1);
					}
				}
				f>>=1;
			}
		}
	}

	public void paint(Graphics g)
	{
		int x,y, i,j, r,G,b, c,w;
		g.clearRect(0,0,384,280);
		for(i=0;i<7;i++) dsp_b(i,(Bs[i])?1:0);
		dsp_b(7,0); dsp_b(8,0);
		dsp_wm(Ws,(Bs[6])?false:true);
	}

	public boolean mouseDown(Event e, int mx, int my)
	{
		int		i,n=0;
		boolean	c=false;

		for(i=1;i<6;i++) if(Bs[i]) n=i;
		for(i=1;i<6;i++) Bs[i]=false;
		if(my>297)
		{
			if(inside(mx, 77,103))	{Bs[0]=true; Bs[6]=false; Bs[n]=true; c=true;}
			if(inside(mx,119,145))	{Ws=0; Bs[1]=true; c=true;}
			if(inside(mx,149,175))	{Ws=1; Bs[2]=true; c=true;}
			if(inside(mx,179,205))	{Ws=2; Bs[3]=true; c=true;}
			if(inside(mx,209,235))	{Ws=4; Bs[4]=true; c=true;}
			if(inside(mx,239,265))	{Ws=8; Bs[5]=true; c=true;}
			if(inside(mx,281,307))	{Bs[0]=false; Bs[6]=true; Bs[n]=true; c=true;}
		}
		if(c) repaint();
		return true;
	}
}
//
戻る