📄 bmp3.java
字号:
pixel = width * height;
pixel += height;
for (i = 0 ; i < height ; i++)
{
pixel -= height * 2;
for (j = 0 ; j < width ; j++)
{
fout.write (q [i] [j]);
fout.write (qg [i] [j]);
fout.write (qb [i] [j]);
pixels [pixel++] = (255 << 24) | (qb [i] [j] << 16) | (qg [i] [j] << 8) | (q [i] [j]);
}
}
img1 = createImage (new MemoryImageSource (width, height, pixels, 0, width));
condition_de = 1;
repaint ();
fout.close ();
}
static public void write_map () throws Exception
{
p ("\nWriting Compressed File..");
OutputStream fout = new FileOutputStream (fnamed + ".lee");
InputStream fs = new FileInputStream (fnames);
for (i = 0 ; i < 54 ; i++)
fout.write (fs.read ()); // copying header
fs.close ();
for (i = 0 ; i < limit ; i++)
{
fout.write (map [i]);
fout.write (mapg [i]);
fout.write (mapb [i]);
}
fout.close ();
}
public void q_bmp () throws Exception
//writes q arrya to bmp
{
OutputStream fout = new FileOutputStream (fnamed);
p ("\nOutput Bmp is being stored in" + fnamed);
InputStream fs = new FileInputStream (fnames);
lsizevalue.setText (String.valueOf (fs.available ()));
lcompsizevalue.setText (String.valueOf ((limit * 3) + (limit * 3) + 54));
for (i = 0 ; i < 54 ; i++)
fout.write (fs.read ()); // copying header
fs.close ();
pixel = width * height;
pixel += height;
for (i = 0 ; i < height ; i++)
{
pixel -= height * 2;
for (j = 0 ; j < width ; j++)
{
fout.write (q [i] [j]);
fout.write (qg [i] [j]);
fout.write (qb [i] [j]);
pixels1 [pixel++] = (255 << 24) | (qb [i] [j] << 16) | (qg [i] [j] << 8) | (q [i] [j]);
}
}
img1 = createImage (new MemoryImageSource (width, height, pixels1, 0, width));
repaint ();
p ("\nBMP Writing completed...");
fout.close ();
} // end of rd2 fn
public void map_to_q () throws Exception
//Writes the mapping of code word int q as bmp
{
rcnt = 0;
ccnt = 0;
int colno, rowno;
for (rcnt = 0 ; rcnt < limit ; rcnt++)
{
colno = rcnt % (height / ppixel); //for col no of cell
rowno = rcnt / (width / ppixel); //for row no
ccnt = 0;
for (j = 0 ; j < ppixel ; j++)
{
for (i = 0 ; i < ppixel ; i++)
{
q [(rowno * ppixel) + j] [(colno * ppixel) + i] = c [map [rcnt]] [ccnt];
qg [(rowno * ppixel) + j] [(colno * ppixel) + i] = cg [mapg [rcnt]] [ccnt];
qb [(rowno * ppixel) + j] [(colno * ppixel) + i] = cb [mapb [rcnt]] [ccnt];
ccnt++;
}
}
ccnt = 0;
}
// p("\nMap to queue completed writing.......");
q_bmp ();
}
public void match_save () throws Exception
//finds the match
{
double d, tempd, toterror = 0.0;
int index = 0;
double dg, tempdg, toterrorg = 0.0;
int indexg = 0;
double db, tempdb, toterrorb = 0.0;
int indexb = 0;
int limit1 = limit;
//limit1/=2;
for (i = 0 ; i < limit ; i++)
{
d = 10000000.2;
dg = 10000000.2;
db = 10000000.2;
for (j = 0 ; j < N ; j++)
{
tempd = 0.0;
tempdg = 0.0;
tempdb = 0.0;
for (k = 0 ; k < blocksize ; k++)
{
tempd += sq (x [i] [k] - c [j] [k]);
tempdg += sq (xg [i] [k] - cg [j] [k]);
tempdb += sq (xb [i] [k] - cb [j] [k]);
}
if (tempd < d)
{
d = tempd;
index = j;
}
if (tempdg < dg)
{
dg = tempdg;
indexg = j;
}
if (tempdb < db)
{
db = tempdb;
indexb = j;
}
}
//assign cell to an index
map [i] = index;
toterror += d;
mapg [i] = indexg;
toterrorg += dg;
mapb [i] = indexb;
toterrorb += db;
}
p ("\nErrors in Red : " + toterror / limit1);
p ("\nErrors in Green : " + toterrorg / limit1);
p ("\nErrors in Blue : " + toterrorb / limit1);
write_map ();
map_to_q ();
}
public void open () throws Exception
{
FileDialog fd = new FileDialog (this, "Open Source Image", FileDialog.LOAD);
fd.setVisible (true);
fnames = fd.getDirectory () + fd.getFile ();
txt.setText (fnames);
}
public void save () throws IOException
{
FileDialog fd = new FileDialog (this, "Save Target Image", FileDialog.SAVE);
fd.setVisible (true);
fnamed = fd.getDirectory () + fd.getFile ();
tct.setText (fnamed);
}
public void lbg () throws Exception
{
//Graphics g;
//g.drawLine(100,100,200,200);
p ("\nLBG algo is being Used...");
InputStream fin = new FileInputStream (fnames);
rdimg (fin);
fin.close ();
//start of random picking of code book
rcnt = 0;
for (i = 0 ; i < limit ; i += blocksize)
{
for (j = 0 ; j < blocksize ; j++)
{
c [rcnt] [j] = x [i] [j];
cg [rcnt] [j] = xg [i] [j];
cb [rcnt] [j] = xb [i] [j];
}
rcnt++;
}
N = rcnt;
for (i = 0 ; i < N ; i++)
{
int temp = 0, tempg = 0, tempb = 0;
for (j = 0 ; j < blocksize ; j++)
{
temp += c [i] [j];
tempg += cg [i] [j];
tempb += cb [i] [j];
}
temp /= blocksize;
tempg /= blocksize;
tempb /= blocksize;
for (j = 0 ; j < blocksize ; j++)
{
c [i] [j] = temp;
cg [i] [j] = tempg;
cb [i] [j] = tempb;
}
}
write_code ();
read_code ();
match_save ();
error_img ();
//write_map();
p ("\nAlgorithm Completed Successfully");
}
public void mlbg () throws Exception
{
p ("\nModified LBG algo is being Used...");
InputStream fin = new FileInputStream (fnames);
rdimg (fin);
fin.close ();
//start of random picking of code book
rcnt = 0;
for (i = 0 ; i < N ; i++)
{
for (j = 0 ; j < blocksize ; j++)
{
c [rcnt] [j] = x [i] [j];
cg [rcnt] [j] = xg [i] [j];
cb [rcnt] [j] = xb [i] [j];
}
rcnt++;
}
N = rcnt;
write_code ();
read_code ();
match_save ();
error_img ();
//write_map();
p ("\nAlgorithm Completed Successfully");
}
public void kmean () throws Exception
{
p ("\nK-Mean algo is being Used...");
InputStream fin = new FileInputStream (fnames);
rdimg (fin);
fin.close ();
//start of random picking of code book
rcnt = 0;
for (i = 0 ; i < limit ; i += blocksize)
{
for (j = 0 ; j < blocksize ; j++)
{
c [rcnt] [j] = x [i] [j];
cg [rcnt] [j] = xg [i] [j];
cb [rcnt] [j] = xb [i] [j];
}
rcnt++;
}
N = rcnt;
write_code ();
read_code ();
match_save ();
error_img ();
p ("\nAlgorithm Completed Successfully");
}
public void pnn () throws Exception
{
p ("\nPNN algo is being Used...");
InputStream fin = new FileInputStream (fnames);
rdimg (fin);
fin.close ();
//start of random picking of code book
calcu ();
double dminouter, d, dall;
int indexouter1, indexouter2, counter, indexall1, indexall2;
boolean visited[] = new boolean [4096]; //for reduction
boolean complete = false;
/*Start of pairing of three colors*/
//For REd pixels
complete = false;
for (i = 0 ; i < limit ; i++)
{
visited [i] = false;
}
while (complete == false)
{
dall = 200000.0;
indexall1 = 0;
indexall2 = 1;
for (k = 0 ; k < limit ; k++)
{
dminouter = 200000.0;
indexouter1 = k;
//for(k=0;k<limit;k++)
//{
//k=0;
indexouter1 = k;
//find first not visited 0 cannot be visited
for (i = 0 ; i < limit ; i++)
if (visited [i] == false && i != k)
break;
//calculate difference between 0 and i
indexouter2 = i;
d = 0;
for (j = 0 ; j < blocksize ; j++)
d += x [indexouter1] [j] - x [indexouter2] [j];
//if(d<dminouter)
dminouter = d;
//search for smaller distance with other not visited
for (i = indexouter2 + 1 ; i < limit ; i++)
{
if (visited [i] == false)
{
d = 0;
for (j = 0 ; j < blocksize ; j++)
{
d += x [indexouter1] [j] - x [i] [j];
}
if (d < dminouter)
{
dminouter = d;
indexouter2 = i;
}
}
//douter=
}
if (dminouter < dall)
{
dall = dminouter;
indexall1 = k;
indexall2 = indexouter2;
}
} //end of for loop of k
indexouter1 = indexall1; //modified to all comparisons
indexouter2 = indexall2;
visited [indexouter2] = true;
for (j = 0 ; j < blocksize ; j++)
{
x [indexouter1] [j] = (x [indexouter1] [j] + x [indexouter2] [j]) / 2;
}
counter = 0;
for (i = 0 ; i < limit ; i++)
{
if (visited [i] == false)
{
counter++;
}
} //end of counting loops
p ("\nRemaining blocks for RED: " + counter);
if (counter <= N)
{
complete = true;
}
} //end of while complete loop
p ("\nCopying paired codewords for RED..");
rcnt = 0;
for (i = 0 ; i < limit ; i++)
{
if (visited [i] == false)
{
for (j = 0 ; j < blocksize ; j++)
{
c [rcnt] [j] = x [i] [j];
//cg[rcnt][j]=xg[i][j];
//cb[rcnt][j]=xb[i][j];
}
rcnt++;
}
}
/*
//For GREEN pixels
complete=false;
for(i=0;i<limit;i++)
{
visited[i]=false;
}
while(complete==false)
{
dall=200000.0;
indexall1=0;
indexall2=1;
for(k=0;k<limit;k++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -