public void build_bricks() { ImagePlus imp; ImagePlus orgimp; ImageStack stack; FileInfo finfo; if (lvImgTitle.isEmpty()) return; orgimp = WindowManager.getImage(lvImgTitle.get(0)); imp = orgimp; finfo = imp.getFileInfo(); if (finfo == null) return; int[] dims = imp.getDimensions(); int imageW = dims[0]; int imageH = dims[1]; int nCh = dims[2]; int imageD = dims[3]; int nFrame = dims[4]; int bdepth = imp.getBitDepth(); double xspc = finfo.pixelWidth; double yspc = finfo.pixelHeight; double zspc = finfo.pixelDepth; double z_aspect = Math.max(xspc, yspc) / zspc; int orgW = imageW; int orgH = imageH; int orgD = imageD; double orgxspc = xspc; double orgyspc = yspc; double orgzspc = zspc; lv = lvImgTitle.size(); if (filetype == "JPEG") { for (int l = 0; l < lv; l++) { if (WindowManager.getImage(lvImgTitle.get(l)).getBitDepth() != 8) { IJ.error("A SOURCE IMAGE MUST BE 8BIT GLAYSCALE"); return; } } } // calculate levels /* int baseXY = 256; int baseZ = 256; if (z_aspect < 0.5) baseZ = 128; if (z_aspect > 2.0) baseXY = 128; if (z_aspect >= 0.5 && z_aspect < 1.0) baseZ = (int)(baseZ*z_aspect); if (z_aspect > 1.0 && z_aspect <= 2.0) baseXY = (int)(baseXY/z_aspect); IJ.log("Z_aspect: " + z_aspect); IJ.log("BaseXY: " + baseXY); IJ.log("BaseZ: " + baseZ); */ int baseXY = 256; int baseZ = 128; int dbXY = Math.max(orgW, orgH) / baseXY; if (Math.max(orgW, orgH) % baseXY > 0) dbXY *= 2; int dbZ = orgD / baseZ; if (orgD % baseZ > 0) dbZ *= 2; lv = Math.max(log2(dbXY), log2(dbZ)) + 1; int ww = orgW; int hh = orgH; int dd = orgD; for (int l = 0; l < lv; l++) { int bwnum = ww / baseXY; if (ww % baseXY > 0) bwnum++; int bhnum = hh / baseXY; if (hh % baseXY > 0) bhnum++; int bdnum = dd / baseZ; if (dd % baseZ > 0) bdnum++; if (bwnum % 2 == 0) bwnum++; if (bhnum % 2 == 0) bhnum++; if (bdnum % 2 == 0) bdnum++; int bw = (bwnum <= 1) ? ww : ww / bwnum + 1 + (ww % bwnum > 0 ? 1 : 0); int bh = (bhnum <= 1) ? hh : hh / bhnum + 1 + (hh % bhnum > 0 ? 1 : 0); int bd = (bdnum <= 1) ? dd : dd / bdnum + 1 + (dd % bdnum > 0 ? 1 : 0); bwlist.add(bw); bhlist.add(bh); bdlist.add(bd); IJ.log("LEVEL: " + l); IJ.log(" width: " + ww); IJ.log(" hight: " + hh); IJ.log(" depth: " + dd); IJ.log(" bw: " + bw); IJ.log(" bh: " + bh); IJ.log(" bd: " + bd); int xyl2 = Math.max(ww, hh) / baseXY; if (Math.max(ww, hh) % baseXY > 0) xyl2 *= 2; if (lv - 1 - log2(xyl2) <= l) { ww /= 2; hh /= 2; } IJ.log(" xyl2: " + (lv - 1 - log2(xyl2))); int zl2 = dd / baseZ; if (dd % baseZ > 0) zl2 *= 2; if (lv - 1 - log2(zl2) <= l) dd /= 2; IJ.log(" zl2: " + (lv - 1 - log2(zl2))); if (l < lv - 1) { lvImgTitle.add(lvImgTitle.get(0) + "_level" + (l + 1)); IJ.selectWindow(lvImgTitle.get(0)); IJ.run( "Scale...", "x=- y=- z=- width=" + ww + " height=" + hh + " depth=" + dd + " interpolation=Bicubic average process create title=" + lvImgTitle.get(l + 1)); } } for (int l = 0; l < lv; l++) { IJ.log(lvImgTitle.get(l)); } Document doc = newXMLDocument(); Element root = doc.createElement("BRK"); root.setAttribute("version", "1.0"); root.setAttribute("nLevel", String.valueOf(lv)); root.setAttribute("nChannel", String.valueOf(nCh)); root.setAttribute("nFrame", String.valueOf(nFrame)); doc.appendChild(root); for (int l = 0; l < lv; l++) { IJ.showProgress(0.0); int[] dims2 = imp.getDimensions(); IJ.log( "W: " + String.valueOf(dims2[0]) + " H: " + String.valueOf(dims2[1]) + " C: " + String.valueOf(dims2[2]) + " D: " + String.valueOf(dims2[3]) + " T: " + String.valueOf(dims2[4]) + " b: " + String.valueOf(bdepth)); bw = bwlist.get(l).intValue(); bh = bhlist.get(l).intValue(); bd = bdlist.get(l).intValue(); boolean force_pow2 = false; /* if(IsPowerOf2(bw) && IsPowerOf2(bh) && IsPowerOf2(bd)) force_pow2 = true; if(force_pow2){ //force pow2 if(Pow2(bw) > bw) bw = Pow2(bw)/2; if(Pow2(bh) > bh) bh = Pow2(bh)/2; if(Pow2(bd) > bd) bd = Pow2(bd)/2; } if(bw > imageW) bw = (Pow2(imageW) == imageW) ? imageW : Pow2(imageW)/2; if(bh > imageH) bh = (Pow2(imageH) == imageH) ? imageH : Pow2(imageH)/2; if(bd > imageD) bd = (Pow2(imageD) == imageD) ? imageD : Pow2(imageD)/2; */ if (bw > imageW) bw = imageW; if (bh > imageH) bh = imageH; if (bd > imageD) bd = imageD; if (bw <= 1 || bh <= 1 || bd <= 1) break; if (filetype == "JPEG" && (bw < 8 || bh < 8)) break; Element lvnode = doc.createElement("Level"); lvnode.setAttribute("lv", String.valueOf(l)); lvnode.setAttribute("imageW", String.valueOf(imageW)); lvnode.setAttribute("imageH", String.valueOf(imageH)); lvnode.setAttribute("imageD", String.valueOf(imageD)); lvnode.setAttribute("xspc", String.valueOf(xspc)); lvnode.setAttribute("yspc", String.valueOf(yspc)); lvnode.setAttribute("zspc", String.valueOf(zspc)); lvnode.setAttribute("bitDepth", String.valueOf(bdepth)); root.appendChild(lvnode); Element brksnode = doc.createElement("Bricks"); brksnode.setAttribute("brick_baseW", String.valueOf(bw)); brksnode.setAttribute("brick_baseH", String.valueOf(bh)); brksnode.setAttribute("brick_baseD", String.valueOf(bd)); lvnode.appendChild(brksnode); ArrayList<Brick> bricks = new ArrayList<Brick>(); int mw, mh, md, mw2, mh2, md2; double tx0, ty0, tz0, tx1, ty1, tz1; double bx0, by0, bz0, bx1, by1, bz1; for (int k = 0; k < imageD; k += bd) { if (k > 0) k--; for (int j = 0; j < imageH; j += bh) { if (j > 0) j--; for (int i = 0; i < imageW; i += bw) { if (i > 0) i--; mw = Math.min(bw, imageW - i); mh = Math.min(bh, imageH - j); md = Math.min(bd, imageD - k); if (force_pow2) { mw2 = Pow2(mw); mh2 = Pow2(mh); md2 = Pow2(md); } else { mw2 = mw; mh2 = mh; md2 = md; } if (filetype == "JPEG") { if (mw2 < 8) mw2 = 8; if (mh2 < 8) mh2 = 8; } tx0 = i == 0 ? 0.0d : ((mw2 - mw + 0.5d) / mw2); ty0 = j == 0 ? 0.0d : ((mh2 - mh + 0.5d) / mh2); tz0 = k == 0 ? 0.0d : ((md2 - md + 0.5d) / md2); tx1 = 1.0d - 0.5d / mw2; if (mw < bw) tx1 = 1.0d; if (imageW - i == bw) tx1 = 1.0d; ty1 = 1.0d - 0.5d / mh2; if (mh < bh) ty1 = 1.0d; if (imageH - j == bh) ty1 = 1.0d; tz1 = 1.0d - 0.5d / md2; if (md < bd) tz1 = 1.0d; if (imageD - k == bd) tz1 = 1.0d; bx0 = i == 0 ? 0.0d : (i + 0.5d) / (double) imageW; by0 = j == 0 ? 0.0d : (j + 0.5d) / (double) imageH; bz0 = k == 0 ? 0.0d : (k + 0.5d) / (double) imageD; bx1 = Math.min((i + bw - 0.5d) / (double) imageW, 1.0d); if (imageW - i == bw) bx1 = 1.0d; by1 = Math.min((j + bh - 0.5d) / (double) imageH, 1.0d); if (imageH - j == bh) by1 = 1.0d; bz1 = Math.min((k + bd - 0.5d) / (double) imageD, 1.0d); if (imageD - k == bd) bz1 = 1.0d; int x, y, z; x = i - (mw2 - mw); y = j - (mh2 - mh); z = k - (md2 - md); bricks.add( new Brick( x, y, z, mw2, mh2, md2, 0, 0, tx0, ty0, tz0, tx1, ty1, tz1, bx0, by0, bz0, bx1, by1, bz1)); } } } Element fsnode = doc.createElement("Files"); lvnode.appendChild(fsnode); stack = imp.getStack(); int totalbricknum = nFrame * nCh * bricks.size(); int curbricknum = 0; for (int f = 0; f < nFrame; f++) { for (int ch = 0; ch < nCh; ch++) { int sizelimit = bdsizelimit * 1024 * 1024; int bytecount = 0; int filecount = 0; int pd_bufsize = Math.max(sizelimit, bw * bh * bd * bdepth / 8); byte[] packed_data = new byte[pd_bufsize]; String base_dataname = basename + "_Lv" + String.valueOf(l) + "_Ch" + String.valueOf(ch) + "_Fr" + String.valueOf(f); String current_dataname = base_dataname + "_data" + filecount; Brick b_first = bricks.get(0); if (b_first.z_ != 0) IJ.log("warning"); int st_z = b_first.z_; int ed_z = b_first.z_ + b_first.d_; LinkedList<ImageProcessor> iplist = new LinkedList<ImageProcessor>(); for (int s = st_z; s < ed_z; s++) iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1))); // ImagePlus test; // ImageStack tsst; // test = NewImage.createByteImage("test", imageW, imageH, imageD, // NewImage.FILL_BLACK); // tsst = test.getStack(); for (int i = 0; i < bricks.size(); i++) { Brick b = bricks.get(i); if (ed_z > b.z_ || st_z < b.z_ + b.d_) { if (b.z_ > st_z) { for (int s = 0; s < b.z_ - st_z; s++) iplist.pollFirst(); st_z = b.z_; } else if (b.z_ < st_z) { IJ.log("warning"); for (int s = st_z - 1; s > b.z_; s--) iplist.addFirst(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1))); st_z = b.z_; } if (b.z_ + b.d_ > ed_z) { for (int s = ed_z; s < b.z_ + b.d_; s++) iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1))); ed_z = b.z_ + b.d_; } else if (b.z_ + b.d_ < ed_z) { IJ.log("warning"); for (int s = 0; s < ed_z - (b.z_ + b.d_); s++) iplist.pollLast(); ed_z = b.z_ + b.d_; } } else { IJ.log("warning"); iplist.clear(); st_z = b.z_; ed_z = b.z_ + b.d_; for (int s = st_z; s < ed_z; s++) iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1))); } if (iplist.size() != b.d_) { IJ.log("Stack Error"); return; } // int zz = st_z; int bsize = 0; byte[] bdata = new byte[b.w_ * b.h_ * b.d_ * bdepth / 8]; Iterator<ImageProcessor> ipite = iplist.iterator(); while (ipite.hasNext()) { // ImageProcessor tsip = tsst.getProcessor(zz+1); ImageProcessor ip = ipite.next(); ip.setRoi(b.x_, b.y_, b.w_, b.h_); if (bdepth == 8) { byte[] data = (byte[]) ip.crop().getPixels(); System.arraycopy(data, 0, bdata, bsize, data.length); bsize += data.length; } else if (bdepth == 16) { ByteBuffer buffer = ByteBuffer.allocate(b.w_ * b.h_ * bdepth / 8); buffer.order(ByteOrder.LITTLE_ENDIAN); short[] data = (short[]) ip.crop().getPixels(); for (short e : data) buffer.putShort(e); System.arraycopy(buffer.array(), 0, bdata, bsize, buffer.array().length); bsize += buffer.array().length; } else if (bdepth == 32) { ByteBuffer buffer = ByteBuffer.allocate(b.w_ * b.h_ * bdepth / 8); buffer.order(ByteOrder.LITTLE_ENDIAN); float[] data = (float[]) ip.crop().getPixels(); for (float e : data) buffer.putFloat(e); System.arraycopy(buffer.array(), 0, bdata, bsize, buffer.array().length); bsize += buffer.array().length; } } String filename = basename + "_Lv" + String.valueOf(l) + "_Ch" + String.valueOf(ch) + "_Fr" + String.valueOf(f) + "_ID" + String.valueOf(i); int offset = bytecount; int datasize = bdata.length; if (filetype == "RAW") { int dummy = -1; // do nothing } if (filetype == "JPEG" && bdepth == 8) { try { DataBufferByte db = new DataBufferByte(bdata, datasize); Raster raster = Raster.createPackedRaster(db, b.w_, b.h_ * b.d_, 8, null); BufferedImage img = new BufferedImage(b.w_, b.h_ * b.d_, BufferedImage.TYPE_BYTE_GRAY); img.setData(raster); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(baos); String format = "jpg"; Iterator<javax.imageio.ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); javax.imageio.ImageWriter writer = iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality((float) jpeg_quality * 0.01f); writer.setOutput(ios); writer.write(null, new IIOImage(img, null, null), iwp); // ImageIO.write(img, format, baos); bdata = baos.toByteArray(); datasize = bdata.length; } catch (IOException e) { e.printStackTrace(); return; } } if (filetype == "ZLIB") { byte[] tmpdata = new byte[b.w_ * b.h_ * b.d_ * bdepth / 8]; Deflater compresser = new Deflater(); compresser.setInput(bdata); compresser.setLevel(Deflater.DEFAULT_COMPRESSION); compresser.setStrategy(Deflater.DEFAULT_STRATEGY); compresser.finish(); datasize = compresser.deflate(tmpdata); bdata = tmpdata; compresser.end(); } if (bytecount + datasize > sizelimit && bytecount > 0) { BufferedOutputStream fis = null; try { File file = new File(directory + current_dataname); fis = new BufferedOutputStream(new FileOutputStream(file)); fis.write(packed_data, 0, bytecount); } catch (IOException e) { e.printStackTrace(); return; } finally { try { if (fis != null) fis.close(); } catch (IOException e) { e.printStackTrace(); return; } } filecount++; current_dataname = base_dataname + "_data" + filecount; bytecount = 0; offset = 0; System.arraycopy(bdata, 0, packed_data, bytecount, datasize); bytecount += datasize; } else { System.arraycopy(bdata, 0, packed_data, bytecount, datasize); bytecount += datasize; } Element filenode = doc.createElement("File"); filenode.setAttribute("filename", current_dataname); filenode.setAttribute("channel", String.valueOf(ch)); filenode.setAttribute("frame", String.valueOf(f)); filenode.setAttribute("brickID", String.valueOf(i)); filenode.setAttribute("offset", String.valueOf(offset)); filenode.setAttribute("datasize", String.valueOf(datasize)); filenode.setAttribute("filetype", String.valueOf(filetype)); fsnode.appendChild(filenode); curbricknum++; IJ.showProgress((double) (curbricknum) / (double) (totalbricknum)); } if (bytecount > 0) { BufferedOutputStream fis = null; try { File file = new File(directory + current_dataname); fis = new BufferedOutputStream(new FileOutputStream(file)); fis.write(packed_data, 0, bytecount); } catch (IOException e) { e.printStackTrace(); return; } finally { try { if (fis != null) fis.close(); } catch (IOException e) { e.printStackTrace(); return; } } } } } for (int i = 0; i < bricks.size(); i++) { Brick b = bricks.get(i); Element bricknode = doc.createElement("Brick"); bricknode.setAttribute("id", String.valueOf(i)); bricknode.setAttribute("st_x", String.valueOf(b.x_)); bricknode.setAttribute("st_y", String.valueOf(b.y_)); bricknode.setAttribute("st_z", String.valueOf(b.z_)); bricknode.setAttribute("width", String.valueOf(b.w_)); bricknode.setAttribute("height", String.valueOf(b.h_)); bricknode.setAttribute("depth", String.valueOf(b.d_)); brksnode.appendChild(bricknode); Element tboxnode = doc.createElement("tbox"); tboxnode.setAttribute("x0", String.valueOf(b.tx0_)); tboxnode.setAttribute("y0", String.valueOf(b.ty0_)); tboxnode.setAttribute("z0", String.valueOf(b.tz0_)); tboxnode.setAttribute("x1", String.valueOf(b.tx1_)); tboxnode.setAttribute("y1", String.valueOf(b.ty1_)); tboxnode.setAttribute("z1", String.valueOf(b.tz1_)); bricknode.appendChild(tboxnode); Element bboxnode = doc.createElement("bbox"); bboxnode.setAttribute("x0", String.valueOf(b.bx0_)); bboxnode.setAttribute("y0", String.valueOf(b.by0_)); bboxnode.setAttribute("z0", String.valueOf(b.bz0_)); bboxnode.setAttribute("x1", String.valueOf(b.bx1_)); bboxnode.setAttribute("y1", String.valueOf(b.by1_)); bboxnode.setAttribute("z1", String.valueOf(b.bz1_)); bricknode.appendChild(bboxnode); } if (l < lv - 1) { imp = WindowManager.getImage(lvImgTitle.get(l + 1)); int[] newdims = imp.getDimensions(); imageW = newdims[0]; imageH = newdims[1]; imageD = newdims[3]; xspc = orgxspc * ((double) orgW / (double) imageW); yspc = orgyspc * ((double) orgH / (double) imageH); zspc = orgzspc * ((double) orgD / (double) imageD); bdepth = imp.getBitDepth(); } } File newXMLfile = new File(directory + basename + ".vvd"); writeXML(newXMLfile, doc); for (int l = 1; l < lv; l++) { imp = WindowManager.getImage(lvImgTitle.get(l)); imp.changes = false; imp.close(); } }
public void actionPerformed(ActionEvent e) { String arg = (String) e.getActionCommand(); /** ******************** Open an Image **************** */ if (e.getSource() == m_Open) { int returnVal = jfc.showOpenDialog(IPToolKit.this); if (returnVal == JFileChooser.APPROVE_OPTION) { file = jfc.getSelectedFile(); String path = file.getAbsolutePath(); // image = Toolkit.getDefaultToolkit().getImage(path); try { try { inImage = new FileImageInputStream(file); len = (int) inImage.length(); byteArray = new byte[len]; // System.out.println(len); inImage.read(byteArray, 0, len); image = Toolkit.getDefaultToolkit().createImage(byteArray); MediaTracker t = new MediaTracker(this); t.addImage(image, 0); try { t.waitForID(0); } catch (Exception eeee) { System.out.println(eeee); } w = image.getWidth(null); h = image.getHeight(null); // System.out.println(w+"\t"+h); } catch (Exception fnfe) { JOptionPane.showMessageDialog(this, "File: Not Found"); } } catch (Exception ice) { JOptionPane.showMessageDialog(this, "File I/O Error"); } } if (image != null) { pix_temp = new int[h * w]; ImageIcon icon = new ImageIcon(image); lbl_img.setIcon(icon); setVisible(true); } pix_temp = pixel_grab(image, w, h); pix_temp = pix_pack(pix_temp, w, h); grayImageDisplay(w, h, pix_temp); } /** ****************** Add one image to another ************** */ if (e.getSource() == mAddImg) { OpenImage2(); pix_temp1 = new int[w * h]; pix_res = new int[w * h]; pix_temp1 = pixel_grab(image1, w1, h1); pix_temp1 = pix_pack(pix_temp1, w1, h1); for (int s = 0; s < w * h; s++) { pix_res[s] = pix_temp[s] + pix_temp1[s]; } ; pix_temp1 = saturate(pix_temp1, w, h); imageResultDisplay(w, h, pix_res); } /** * *********************** Add Constant value and then use Satuartion technique *************** */ if (e.getSource() == mAddSat) { String add_value; Image img_add = image; pixel_add = new int[w * h]; pixel_result = pixel_grab(img_add, w, h); add_value = JOptionPane.showInputDialog("Enter Value you wish to add to the image"); int number = Integer.parseInt(add_value); for (int r = 0; r < w * h; r++) { pixel_add[r] = number + pixel_result[r]; } pixel_add = saturate(pixel_add, w, h); pixel_add = pix_pack(pixel_add, w, h); imageResultDisplay(w, h, pixel_add); } /** ************ Add constant by Wrap Around technique ********** */ if (e.getSource() == mAddWrap) { String add_value; Image img_add = image; pixel_add = new int[w * h]; pixel_result = pixel_grab(img_add, w, h); add_value = JOptionPane.showInputDialog("Enter Value you wish to add to the image"); int number = Integer.parseInt(add_value); for (int r = 0; r < w * h; r++) { pixel_add[r] = number + pixel_result[r]; } pixel_add = WrapAround(pixel_add, w, h); pixel_add = pix_pack(pixel_add, w, h); imageResultDisplay(w, h, pixel_add); } /** ****************** Subtract one image from another ************** */ if (e.getSource() == mSubImg) { OpenImage2(); pix_temp1 = new int[w * h]; pix_res = new int[w * h]; pix_temp1 = pixel_grab(image1, w1, h1); pix_temp1 = pix_pack(pix_temp1, w1, h1); for (int s = 0; s < w * h; s++) { pix_res[s] = pix_temp[s] - pix_temp1[s]; } pix_temp1 = saturate(pix_temp1, w1, h1); imageResultDisplay(w1, h1, pix_res); } /** * *********************** Subtract Constant value and then use Satuartion technique * *************** */ if (e.getSource() == mSubSat) { String sub_value; Image img_add = image; pixel_sub = new int[w * h]; pixel_result = pixel_grab(img_add, w, h); sub_value = JOptionPane.showInputDialog("Enter Value you wish to subtract from the image"); int number = Integer.parseInt(sub_value); for (int r = 0; r < w * h; r++) { pixel_sub[r] = pixel_result[r] - number; } pixel_sub = saturate(pixel_sub, w, h); pixel_sub = pix_pack(pixel_sub, w, h); imageResultDisplay(w, h, pixel_sub); } /** ************ Subtract constant by Wrap Around technique ********** */ if (e.getSource() == mSubWrap) { String sub_value; Image img_add = image; pixel_sub = new int[w * h]; pixel_result = pixel_grab(img_add, w, h); sub_value = JOptionPane.showInputDialog("Enter Value you wish to add to the image"); int number = Integer.parseInt(sub_value); for (int r = 0; r < w * h; r++) { pixel_sub[r] = pixel_result[r] - number; } pixel_sub = WrapAround(pixel_sub, w, h); pixel_sub = pix_pack(pixel_sub, w, h); imageResultDisplay(w, h, pixel_sub); } /** ****************** Multiply one image with another ************** */ if (e.getSource() == mMulImg) { OpenImage2(); pix_temp1 = new int[w * h]; pix_res = new int[w * h]; pix_temp1 = pixel_grab(image1, w1, h1); pix_temp1 = pix_pack(pix_temp1, w1, h1); for (int s = 0; s < w * h; s++) { pix_res[s] = pix_temp[s] * pix_temp1[s]; } pix_temp1 = saturate(pix_temp1, w1, h1); imageResultDisplay(w1, h1, pix_res); } /** * *********************** Multiply Constant value and then use Satuartion technique * *************** */ if (e.getSource() == mMulSat) { String mul_value; Image img_mul = image; pixel_mul = new int[w * h]; pixel_result = pixel_grab(img_mul, w, h); mul_value = JOptionPane.showInputDialog("Enter Value you wish to multiply to the image"); int number = Integer.parseInt(mul_value); for (int r = 0; r < w * h; r++) { pixel_mul[r] = pixel_result[r] * number; } pixel_mul = saturate(pixel_mul, w, h); pixel_mul = pix_pack(pixel_mul, w, h); imageResultDisplay(w, h, pixel_mul); } /** ************ Multiply constant by Wrap Around technique ********** */ if (e.getSource() == mMulWrap) { String mul_value; Image img_mul = image; pixel_mul = new int[w * h]; pixel_result = pixel_grab(img_mul, w, h); mul_value = JOptionPane.showInputDialog("Enter Value you wish to multiply to the image"); int number = Integer.parseInt(mul_value); for (int r = 0; r < w * h; r++) { pixel_mul[r] = pixel_result[r] * number; } pixel_mul = WrapAround(pixel_mul, w, h); pixel_mul = pix_pack(pixel_mul, w, h); imageResultDisplay(w, h, pixel_mul); } /** ****************** Divide one image by another ************** */ if (e.getSource() == mDivImg) { OpenImage2(); pix_temp1 = new int[w * h]; pix_res = new int[w * h]; pix_temp1 = pixel_grab(image1, w1, h1); pix_temp1 = pix_pack(pix_temp1, w1, h1); for (int s = 0; s < w * h; s++) { pix_res[s] = pix_temp1[s] / pix_temp[s]; } // pix_temp1 = saturate(pix_temp1,w1,h1); pix_temp1 = WrapAround(pix_temp1, w, h); imageResultDisplay(w1, h1, pix_res); } /** * *********************** Divide by Constant value and then use Saturation technique * *************** */ if (e.getSource() == mDivSat) { String div_value; Image img_add = image; pixel_div = new int[w * h]; pixel_result = pixel_grab(img_add, w, h); div_value = JOptionPane.showInputDialog("Enter value to divide the image by"); int number = Integer.parseInt(div_value); for (int r = 0; r < w * h; r++) { pixel_div[r] = pixel_result[r] / number; } pixel_div = saturate(pixel_div, w, h); pixel_div = pix_pack(pixel_div, w, h); imageResultDisplay(w, h, pixel_div); } /** ************ Divide constant by Wrap Around technique ********** */ if (e.getSource() == mDivWrap) { String div_value; Image img_add = image; pixel_div = new int[w * h]; pixel_result = pixel_grab(img_add, w, h); div_value = JOptionPane.showInputDialog("Enter value to divide image by"); int number = Integer.parseInt(div_value); for (int r = 0; r < w * h; r++) { pixel_div[r] = pixel_result[r] / number; } pixel_div = WrapAround(pixel_div, w, h); pixel_div = pix_pack(pixel_div, w, h); imageResultDisplay(w, h, pixel_div); } /** ******************** Thresholding ************* */ if (e.getSource() == mThreshold) { String thresh_value; Image img_add = image; pixel_result = pixel_grab(img_add, w, h); thresh_value = JOptionPane.showInputDialog("Enter value to threshold the image"); int number = Integer.parseInt(thresh_value); for (int r = 0; r < w * h; r++) { if (pixel_result[r] >= number) pixel_result[r] = 255; else if (pixel_result[r] <= number) pixel_result[r] = 0; } pixel_result = pix_pack(pixel_result, w, h); imageResultDisplay(w, h, pixel_result); } if (e.getSource() == m_Save) { FileImageOutputStream src_img; int returnVal = jfc.showSaveDialog(IPToolKit.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File fileR = jfc.getSelectedFile(); String fileToSave = file.getAbsolutePath(); if (!fileToSave.toLowerCase().endsWith(".jpg")) { fileR = new File(fileToSave + ".jpg"); } try { src_img = new FileImageOutputStream(fileR); buf_img = getImageFromArray(pixel_result, w, h); ImageIO.write(buf_img, "jpg", src_img); } catch (IOException ex8) { System.out.println(ex8); } } // } } /** ***************************** Increase brightness **************** */ if (e.getSource() == mBright) { String bright_value; Image img_add = image; pixel_result = pixel_grab(img_add, w, h); bright_value = JOptionPane.showInputDialog("Enter value to increase brightness"); int number = Integer.parseInt(bright_value); for (int r = 0; r < w * h; r++) { pixel_result[r] = pixel_result[r] + number; } pixel_result = saturate(pixel_result, w, h); pixel_result = pix_pack(pixel_result, w, h); imageResultDisplay(w, h, pixel_result); } /** ****************** Contrast Strtching *************** */ if (e.getSource() == mContrast) { String cont_value1, cont_value2; Image img_add = image; pixel_cont = new int[w * h]; pixel_result = pixel_grab(img_add, w, h); cont_value1 = JOptionPane.showInputDialog("Enter lower limit for contrast stretch"); int number = Integer.parseInt(cont_value1); cont_value2 = JOptionPane.showInputDialog("Enter higher limit for contrast stretch"); int number1 = Integer.parseInt(cont_value2); for (int r = 0; r < w * h; r++) { if (pixel_result[r] <= number1) { pixel_cont[r] = pixel_result[r] - 50; if (pixel_cont[r] < 0) pixel_cont[r] = 0; } else if (pixel_result[r] >= number) { pixel_cont[r] = pixel_result[r] + 50; if (pixel_cont[r] > 255) pixel_cont[r] = 255; } else pixel_cont[r] = pixel_result[r]; } pixel_cont = saturate(pixel_cont, w, h); pixel_cont = pix_pack(pixel_cont, w, h); imageResultDisplay(w, h, pixel_cont); } /** ******************* Low Pass Filter ****************** */ if (e.getSource() == mlowPass) { int mask[][] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}; pixel_result = pixel_grab(image, w, h); int pix_tempLow[][] = new int[h][w]; pix_tempLow = OneD_ArrayToTwoD_Array(w, h, pixel_result); pix_tempLow = MaskOperation(w, h, pix_tempLow, mask); int pix_temp1D[] = new int[(w) * (h)]; pix_temp1D = TwoD_ArrayToOneD_Array(w, h, pix_tempLow); pix_temp1D = restrict(pix_temp1D); pix_temp1D = pix_pack(pix_temp1D, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_temp1D); } /** ********************* High Pass Filter *************** */ if (e.getSource() == mhighPass) { int mask[][] = {{-1, -1, -1}, {-1, 8, -1}, {-1, -1, -1}}; pixel_result = pixel_grab(image, w, h); int pix_tempLow[][] = new int[h][w]; pix_tempLow = OneD_ArrayToTwoD_Array(w, h, pixel_result); pix_tempLow = MaskOperation(w, h, pix_tempLow, mask); int pix_temp1D[] = new int[(w) * (h)]; pix_temp1D = TwoD_ArrayToOneD_Array(w, h, pix_tempLow); pix_temp1D = saturate(pix_temp1D, w - 2, h - 2); pix_temp1D = restrict(pix_temp1D); pix_temp1D = pix_pack(pix_temp1D, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_temp1D); } /** *************************** High Boost Filter **************** */ if (e.getSource() == mhighBoost) { int mask[][] = {{-1, -1, -1}, {-1, 9, -1}, {-1, -1, -1}}; pixel_result = pixel_grab(image, w, h); int pix_tempLow[][] = new int[h][w]; pix_tempLow = OneD_ArrayToTwoD_Array(w, h, pixel_result); pix_tempLow = MaskOperation(w, h, pix_tempLow, mask); int pix_temp1D[] = new int[(w) * (h)]; pix_temp1D = TwoD_ArrayToOneD_Array(w, h, pix_tempLow); pix_temp1D = saturate(pix_temp1D, w - 2, h - 2); pix_temp1D = restrict(pix_temp1D); pix_temp1D = pix_pack(pix_temp1D, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_temp1D); } if (e.getSource() == m_Save) { for (int i = 0; i < w * h; i++) { System.out.println(pixel_result[i]); } FileImageOutputStream src_img; int returnVal = jfc.showSaveDialog(IPToolKit.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File fileR = jfc.getSelectedFile(); String fileToSave = file.getAbsolutePath(); if (!fileToSave.toLowerCase().endsWith(".jpg")) { fileR = new File(fileToSave + ".jpg"); } try { src_img = new FileImageOutputStream(fileR); buf_img = getImageFromArray(pixel_result, w, h); ImageIO.write(buf_img, "jpg", src_img); } catch (IOException ex8) { System.out.println(ex8); } } } /** ********************* Invert and image ************ */ if (e.getSource() == mInvert) { Image img_Invert = image; pixel_result = new int[w * h]; pixel_result = pixel_grab(img_Invert, w, h); for (int r = 0; r < w * h; r++) { pixel_result[r] = 255 - pixel_result[r]; } // pixel_div = saturate(pixel_div,w,h); pixel_result = pix_pack(pixel_result, w, h); imageResultDisplay(w, h, pixel_result); } /** *************************** Median Filter **************** */ if (arg.equals("Median")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres[] = new int[h * w]; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = median_filter(p2d); pres = TwoD_ArrayToOneD_Array(w, h, pp2d); pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Prewits Horizontal Edge Detection ******************* */ if (arg.equals("Prewits Horizontal")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres[] = new int[h * w]; int mask[][] = {{-1, -1, -1}, {0, 0, 0}, {1, 1, 1}}; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = MaskOperation(w, h, p2d, mask); pres = TwoD_ArrayToOneD_Array(w, h, pp2d); pres = saturate(pres, w - 2, h - 2); pres = restrict(pres); pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Prewits Vertical Edge Detection ******************* */ if (arg.equals("Prewits Vertical")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres[] = new int[h * w]; int mask[][] = {{-1, 0, 1}, {-1, 0, 1}, {-1, 0, 1}}; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = MaskOperation(w, h, p2d, mask); pres = TwoD_ArrayToOneD_Array(w, h, pp2d); pres = saturate(pres, w - 2, h - 2); pres = restrict(pres); pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Prewits Edge Detection ******************* */ if (arg.equals("Prewits Both")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres1[] = new int[h * w]; int pres2[] = new int[h * w]; int pres[] = new int[h * w]; int mask1[][] = {{-1, -1, -1}, {0, 0, 0}, {1, 1, 1}}; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = MaskOperation(w, h, p2d, mask1); pres1 = TwoD_ArrayToOneD_Array(w, h, pp2d); pres1 = saturate(pres1, w - 2, h - 2); pres1 = restrict(pres1); int mask2[][] = {{-1, 0, 1}, {-1, 0, 1}, {-1, 0, 1}}; p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = MaskOperation(w, h, p2d, mask2); pres2 = TwoD_ArrayToOneD_Array(w, h, pp2d); pres2 = saturate(pres2, w - 2, h - 2); pres2 = restrict(pres2); for (int i = 0; i < ((h - 2) * (w - 2)); i++) { pres[i] = pres1[i] + pres2[i]; } pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Sobel Horizontal Edge Detection ******************* */ if (arg.equals("Sobel Horizontal")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres[] = new int[h * w]; int mask[][] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}}; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = MaskOperation(w, h, p2d, mask); pres = TwoD_ArrayToOneD_Array(w, h, pp2d); pres = saturate(pres, w - 2, h - 2); pres = restrict(pres); pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Sobel Vertical Edge Detection ******************* */ if (arg.equals("Sobel Vertical")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres[] = new int[h * w]; int mask[][] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = MaskOperation(w, h, p2d, mask); pres = TwoD_ArrayToOneD_Array(w, h, pp2d); pres = saturate(pres, w - 2, h - 2); pres = restrict(pres); pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Sobel Edge Detection ******************* */ if (arg.equals("Sobel Both")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres1[] = new int[h * w]; int pres2[] = new int[h * w]; int pres[] = new int[h * w]; int mask1[][] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}}; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = MaskOperation(w, h, p2d, mask1); pres1 = TwoD_ArrayToOneD_Array(w, h, pp2d); pres1 = saturate(pres1, w - 2, h - 2); pres1 = restrict(pres1); int mask2[][] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}; p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = MaskOperation(w, h, p2d, mask2); pres2 = TwoD_ArrayToOneD_Array(w, h, pp2d); pres2 = saturate(pres2, w - 2, h - 2); pres2 = restrict(pres2); for (int i = 0; i < ((h - 2) * (w - 2)); i++) { pres[i] = pres1[i] + pres2[i]; } pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Roberts Edge Detection ******************* */ if (arg.equals("Roberts")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres[] = new int[h * w]; int mask[][] = {{1, 0}, {0, -1}}; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); for (int i = 1; i < h - 1; i++) { for (int j = 1; j < w - 1; j++) { pp2d[i][j] = ((mask[0][0] * p2d[i - 1][j - 1] + mask[1][0] * p2d[i + 1][j - 1]) + (mask[0][1] * p2d[i - 1][j + 1] + mask[1][1] * p2d[i + 1][j + 1])) / 2; } } pres = TwoD_ArrayToOneD_Array(w, h, pp2d); pres = saturate(pres, w - 2, h - 2); pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Mean Filter ******************* */ if (arg.equals("Mean")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres[] = new int[h * w]; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = mean_filt(p2d); pres = TwoD_ArrayToOneD_Array(w, h, pp2d); pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Laplacian Edge Detector ******************* */ if (arg.equals("Laplacian")) { int[] pix_grabed = new int[w * h]; int[][] p2d = new int[h][w]; int[][] pp2d = new int[h][w]; int pix_pcked[] = new int[w * h]; int pres[] = new int[h * w]; int mask[][] = {{0, -2, 0}, {-2, 8, -2}, {0, -2, 0}}; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); pp2d = MaskOperation(w, h, p2d, mask); pres = TwoD_ArrayToOneD_Array(w, h, pp2d); pres = saturate(pres, w - 2, h - 2); pres = restrict(pres); pix_pcked = pix_pack(pres, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pix_pcked); } /** ********************** Histogram Equalization ******************* */ if (arg.equals("Histogram Equalization")) { int[] pix_grabed = new int[h * w]; int pres[] = new int[h * w]; int cnt_pix[] = new int[256]; int pixx[] = new int[h * w]; int pix_pcked[] = new int[h * w]; int pix_res[] = new int[h * w]; int len = h * w; int res = 0; pix_grabed = pixel_grab(image, w, h); for (int i = 0; i < h * w; i++) { pixx[i] = pix_grabed[i]; } for (int i = 0; i < 256; i++) { cnt_pix[i] = 0; } for (int i = 0; i < len; i++) { int ind = pixx[i]; cnt_pix[ind]++; } for (int i = 0; i < w * h; i++) { float a = 0; for (int j = 0; j < (pixx[i] + 1); j++) { float b = (float) cnt_pix[j]; float c = (float) (h * w); a = a + (b / c); } res = (int) (a * 255); if (res > 255) res = 255; pix_res[i] = (0xff000000 | (res << 16) | (res << 8) | res); } pix_pcked = pix_pack(pix_res, w, h); imageResultDisplay(w, h, pix_pcked); } if (arg.equals("Connected Component")) { int pix_grabed[] = new int[h * w]; int p2d[][] = new int[h][w]; conect_input = new int[h][w]; conect_output = new int[h][w]; int x1 = x_cor; int y1 = y_cor; pix_grabed = pixel_grab(image, w, h); p2d = OneD_ArrayToTwoD_Array(w, h, pix_grabed); s = 0; t = 0; obj_size = 0; intensity = p2d[y1][x1]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { conect_input[i][j] = p2d[i][j]; conect_output[i][j] = 0; } } connect(y1, x1); int pixx[] = new int[h * w]; int pp[] = new int[h * w]; pixx = TwoD_ArrayToOneD_Array(w, h, conect_output); pixx = saturate(pixx, w - 2, h - 2); pp = pix_pack(pixx, w - 2, h - 2); imageResultDisplay(w - 2, h - 2, pp); } /** ********************** Blending ******************* */ if (arg.equals("Blending")) { int size; int hei, wid; if (h1 > h) hei = h1; else hei = h; if (w1 > w) wid = w1; else wid = w; size = hei * wid; int pix_img1[] = new int[size]; int pix_img2[] = new int[size]; int pix_res[] = new int[size]; int pixx[] = new int[h1 * w1]; double x = 0.5; pix_img1 = pixel_grab(image, w, h); OpenImage2(); pix_img2 = pixel_grab(image1, w1, h1); // grayImageDisplay(w1,h1,pixx) for (int i = 0; i < size; i++) { pix_res[i] = (int) ((x * pix_img1[i]) + ((1 - x) * (pix_img2[i]))); } int pix_pcked[] = new int[size]; pix_pcked = pix_pack(pix_res, wid, hei); imageResultDisplay(wid, hei, pix_pcked); } /** ****************** Quit OR Exit **************** */ if (arg.equals("Exit")) { System.exit(0); } }