List of usage examples for java.awt.image BufferedImage setRGB
public void setRGB(int x, int y, int rgb)
From source file:de.ailis.wlandsuite.WebExtract.java
/** * Extracts the sprites.//from w w w . ja v a 2s. c o m * * @param sourceDirectory * The input directory * @param targetDirectory * The output directory * @throws IOException * When file operation fails. */ private void extractSprites(final File sourceDirectory, final File targetDirectory) throws IOException { // Extract tilesets final File imagesDirectory = new File(targetDirectory, "images"); imagesDirectory.mkdirs(); String filename = "ic0_9.wlf"; log.info("Reading " + filename); final Sprites sprites; InputStream stream = new FileInputStream(new File(sourceDirectory, filename)); try { sprites = Sprites.read(stream); } finally { stream.close(); } filename = "masks.wlf"; log.info("Reading " + filename); final Masks masks; stream = new FileInputStream(new File(sourceDirectory, filename)); try { masks = Masks.read(stream, 10); } finally { stream.close(); } final int scale = this.scaleFilter.getScaleFactor(); final BufferedImage out; final int outType = this.scaleFilter.getImageType(); if (outType == -1) out = new TransparentEgaImage(10 * 16 * scale, 16 * scale); else out = new BufferedImage(10 * 16 * scale, 16 * scale, BufferedImage.TYPE_INT_ARGB); log.info("Writing sprites"); for (int i = 0; i < 10; i++) { final BufferedImage sprite = this.scaleFilter.scale(sprites.getSprites().get(i)); final BufferedImage mask = this.scaleFilter.scale(masks.getMasks().get(i)); for (int x = 0; x < 16 * scale; x++) { for (int y = 0; y < 16 * scale; y++) { if (mask.getRGB(x, y) == Color.BLACK.getRGB()) out.setRGB(x + i * 16 * scale, y, sprite.getRGB(x, y)); } } } ImageIO.write(out, "png", new File(imagesDirectory, "sprites.png")); }
From source file:com.aquest.emailmarketing.web.controllers.TrackingController.java
/** * Gets the image.// w ww.ja va 2 s . c om * * @param request the request * @param response the response * @param trackingId the tracking id * @return the image * @throws IOException Signals that an I/O exception has occurred. * @throws FileNotFoundException the file not found exception * @throws ParserConfigurationException the parser configuration exception * @throws TransformerException the transformer exception * @throws ServletException the servlet exception * @throws FileUploadException the file upload exception */ @RequestMapping(value = "/openTrack", method = RequestMethod.GET) public void getImage(HttpServletRequest request, HttpServletResponse response, @RequestParam("trackingId") String trackingId) throws IOException, FileNotFoundException, ParserConfigurationException, TransformerException, ServletException, FileUploadException { BufferedImage pixel; Timestamp curTimestamp = new java.sql.Timestamp(Calendar.getInstance().getTime().getTime()); EmailList emailList = emailListService.getEmailListById(trackingId); TrackingResponse trackingResponse = new TrackingResponse(); trackingResponse.setBroadcast_id(emailList.getBroadcast_id()); trackingResponse.setEmail(emailList.getEmail()); trackingResponse.setResponse_type("Open"); trackingResponse.setUnique_id(emailList.getId()); trackingResponse.setResponse_source("Internal Tracking"); trackingResponse.setResponse_time(curTimestamp); trackingResponse.setProcessed_dttm(curTimestamp); trackingResponseService.SaveOrUpdate(trackingResponse); System.out.println(trackingId); // dodati logiku za ubacivanje response-a. pixel = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); pixel.setRGB(0, 0, (0xFF)); response.setContentType("image/png"); OutputStream os = response.getOutputStream(); ImageIO.write(pixel, "png", os); System.out.println("Neko je pristupio!!!"); }
From source file:MainClass.java
public BufferedImage emboss(BufferedImage src) { int width = src.getWidth(); int height = src.getHeight(); BufferedImage dst; dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) { int upperLeft = 0; int lowerRight = 0; if (i > 0 && j > 0) upperLeft = src.getRGB(j - 1, i - 1); if (i < height - 1 && j < width - 1) lowerRight = src.getRGB(j + 1, i + 1); int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255); int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255); int blueDiff = (lowerRight & 255) - (upperLeft & 255); int diff = redDiff; if (Math.abs(greenDiff) > Math.abs(diff)) diff = greenDiff;//from ww w .java2 s .c om if (Math.abs(blueDiff) > Math.abs(diff)) diff = blueDiff; int grayColor = 128 + diff; if (grayColor > 255) grayColor = 255; else if (grayColor < 0) grayColor = 0; int newColor = (grayColor << 16) + (grayColor << 8) + grayColor; dst.setRGB(j, i, newColor); } return dst; }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static BufferedImage compensateLensDistortion(BufferedImage img, double k) { if (img == null) { return null; }// w ww. j a v a 2 s . co m int w = img.getWidth(); int h = img.getHeight(); BufferedImage out = new BufferedImage(w, h, img.getType()); int x0 = (int) Math.floor(w / 2) + 1; int y0 = (int) Math.floor(h / 2) + 1; double ru, theta, ww, rd; int xd, yd; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { ru = Math.sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0)); theta = Math.atan2(y - y0, x - x0); ww = Math.pow(ru / (2 * k) + Math.sqrt((ru * ru) / (4 * k * k) + 1 / (27 * k * k * k)), 1.0 / 3.0); rd = ww - 1 / (3 * k * ww); //nearest neighbour--------------------------------------- xd = (int) Math.round(rd * Math.cos(theta)) + x0; yd = (int) Math.round(rd * Math.sin(theta)) + y0; if (xd >= 0 && yd >= 0 && xd < w && yd < h) { //piksel nowy x,y = piksel stary xd,yd out.setRGB(x, y, img.getRGB(xd, yd)); } //--------------------------------------------------------- } } return out; }
From source file:herramientas.Ecualizacion_histograma.java
public BufferedImage ecualizacion() { BufferedImage resultado = new BufferedImage(img.getWidth(), img.getHeight(), img.getType()); int[] datos_ec = new int[256]; int[] datos_ac = new int[256]; datos_ac[0] = datos[0];//from w w w. ja v a 2s.co m for (int i = 1; i < 256; i++) { datos_ac[i] = datos[i] + datos_ac[i - 1]; } int size = img.getWidth() * img.getHeight(); int m = 256; //2^8, Imgenes de 8 bits. double factor = (double) m / size; for (int i = 0; i < 256; i++) { // datos_ec[i] = Math.max(0, ((int) Math.round((m / size) * datos_ac[i]) - 1)); datos_ec[i] = Math.max(0, ((int) Math.round(factor * datos_ac[i]) - 1)); // System.out.println((Math.round((m / size) * datos_ac[i])) - 1); // System.out.println(datos_ec[i]); } for (int i = 0; i < resultado.getWidth(); i++) { for (int j = 0; j < resultado.getHeight(); j++) { int viejo = new Color(img.getRGB(i, j)).getRed(); int nuevo = datos_ec[viejo]; resultado.setRGB(i, j, new Color(nuevo, nuevo, nuevo).getRGB()); } } return resultado; }
From source file:org.kalypsodeegree_impl.graphics.displayelements.RasterDisplayElement_Impl.java
private BufferedImage getTiledImage(final int screenWidth, final int screenHeight, final double[][] values, final double[][] slopes) { final BufferedImage img = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_ARGB); for (int y = 0; y < screenHeight; y++) { for (int x = 0; x < screenWidth; x++) { final double value = values[y + 1][x + 1]; if (!Double.isNaN(value)) { final Color color = getColor(value); if (color != null) { final double slope = slopes == null ? Double.NaN : slopes[y + 1][x + 1]; if (Double.isNaN(slope)) img.setRGB(x, y, color.getRGB()); else { final Color shadedColor = shadeColor(color, slope); img.setRGB(x, y, shadedColor.getRGB()); }//from www.j ava 2 s . c o m } } } } return img; }
From source file:com.db.comserv.main.utilities.HttpCaller.java
@Override @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "DM_DEFAULT_ENCODING") public HttpResult runRequest(String type, String methodType, URL url, List<Map<String, String>> headers, String requestBody, String sslByPassOption, int connTimeOut, int readTimeout, HttpServletRequest req) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, UnsupportedEncodingException, IOException, UnknownHostException, URISyntaxException { StringBuffer response = new StringBuffer(); HttpResult httpResult = new HttpResult(); boolean gzip = false; final long startNano = System.nanoTime(); try {/*from w w w . ja v a2 s. c o m*/ URL encodedUrl = new URL(Utility.encodeUrl(url.toString())); HttpURLConnection con = (HttpURLConnection) encodedUrl.openConnection(); TrustModifier.relaxHostChecking(con, sslByPassOption); // connection timeout 5s con.setConnectTimeout(connTimeOut); // read timeout 10s con.setReadTimeout(readTimeout * getQueryCost(req)); methodType = methodType.toUpperCase(); con.setRequestMethod(methodType); sLog.debug("Performing '{}' to '{}'", methodType, ServletUtil.filterUrl(url.toString())); // Get headers & set request property for (int i = 0; i < headers.size(); i++) { Map<String, String> header = headers.get(i); con.setRequestProperty(header.get("headerKey").toString(), header.get("headerValue").toString()); sLog.debug("Setting Header '{}' with value '{}'", header.get("headerKey").toString(), ServletUtil.filterHeaderValue(header.get("headerKey").toString(), header.get("headerValue").toString())); } if (con.getRequestProperty("Accept-Encoding") == null) { con.setRequestProperty("Accept-Encoding", "gzip"); } if (requestBody != null && !requestBody.equals("")) { con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.write(Utility.toUtf8Bytes(requestBody)); wr.flush(); wr.close(); } // push response BufferedReader in = null; String inputLine; List<String> contentEncoding = con.getHeaderFields().get("Content-Encoding"); if (contentEncoding != null) { for (String val : contentEncoding) { if ("gzip".equalsIgnoreCase(val)) { sLog.debug("Gzip enabled response"); gzip = true; break; } } } sLog.debug("Response: '{} {}' with headers '{}'", con.getResponseCode(), con.getResponseMessage(), ServletUtil.buildHeadersForLog(con.getHeaderFields())); if (con.getResponseCode() != 200 && con.getResponseCode() != 201) { if (con.getErrorStream() != null) { if (gzip) { in = new BufferedReader( new InputStreamReader(new GZIPInputStream(con.getErrorStream()), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(con.getErrorStream(), "UTF-8")); } } } else { String[] urlParts = url.toString().split("\\."); if (urlParts.length > 1) { String ext = urlParts[urlParts.length - 1]; if (ext.equalsIgnoreCase("png") || ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg") || ext.equalsIgnoreCase("gif")) { BufferedImage imBuff; if (gzip) { imBuff = ImageIO.read(new GZIPInputStream(con.getInputStream())); } else { BufferedInputStream bfs = new BufferedInputStream(con.getInputStream()); imBuff = ImageIO.read(bfs); } BufferedImage newImage = new BufferedImage(imBuff.getWidth(), imBuff.getHeight(), BufferedImage.TYPE_3BYTE_BGR); // converting image to greyScale int width = imBuff.getWidth(); int height = imBuff.getHeight(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color c = new Color(imBuff.getRGB(j, i)); int red = (int) (c.getRed() * 0.21); int green = (int) (c.getGreen() * 0.72); int blue = (int) (c.getBlue() * 0.07); int sum = red + green + blue; Color newColor = new Color(sum, sum, sum); newImage.setRGB(j, i, newColor.getRGB()); } } ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(newImage, "jpg", out); byte[] bytes = out.toByteArray(); byte[] encodedBytes = Base64.encodeBase64(bytes); String base64Src = new String(encodedBytes); int imageSize = ((base64Src.length() * 3) / 4) / 1024; int initialImageSize = imageSize; int maxImageSize = Integer.parseInt(properties.getValue("Reduced_Image_Size")); float quality = 0.9f; if (!(imageSize <= maxImageSize)) { //This means that image size is greater and needs to be reduced. sLog.debug("Image size is greater than " + maxImageSize + " , compressing image."); while (!(imageSize < maxImageSize)) { base64Src = compress(base64Src, quality); imageSize = ((base64Src.length() * 3) / 4) / 1024; quality = quality - 0.1f; DecimalFormat df = new DecimalFormat("#.0"); quality = Float.parseFloat(df.format(quality)); if (quality <= 0.1) { break; } } } sLog.debug("Initial image size was : " + initialImageSize + " Final Image size is : " + imageSize + "Url is : " + url + "quality is :" + quality); String src = "data:image/" + urlParts[urlParts.length - 1] + ";base64," + new String(base64Src); JSONObject joResult = new JSONObject(); joResult.put("Image", src); out.close(); httpResult.setResponseCode(con.getResponseCode()); httpResult.setResponseHeader(con.getHeaderFields()); httpResult.setResponseBody(joResult.toString()); httpResult.setResponseMsg(con.getResponseMessage()); return httpResult; } } if (gzip) { in = new BufferedReader( new InputStreamReader(new GZIPInputStream(con.getInputStream()), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); } } if (in != null) { while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); } httpResult.setResponseCode(con.getResponseCode()); httpResult.setResponseHeader(con.getHeaderFields()); httpResult.setResponseBody(response.toString()); httpResult.setResponseMsg(con.getResponseMessage()); } catch (Exception e) { sLog.error("Failed to received HTTP response after timeout", e); httpResult.setTimeout(true); httpResult.setResponseCode(500); httpResult.setResponseMsg("Internal Server Error Timeout"); return httpResult; } return httpResult; }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static BufferedImage compensateLensDistortion2(BufferedImage img, double k) { if (img == null) { return null; }/*from w w w.j av a 2s . c o m*/ int w = img.getWidth(); int h = img.getHeight(); //BufferedImage out = new BufferedImage(w, h, img.getType()); int x0 = (int) Math.floor(w / 2) + 1; int y0 = (int) Math.floor(h / 2) + 1; double ru, theta, ww, rd; int xd, yd; double rdmax = Math.sqrt((w - x0) * (w - x0) + (h - y0) * (h - y0)); double rumax = rdmax * (1 + k * rdmax * rdmax); //System.out.println("rdmax="+rdmax); //System.out.println("rumax="+rumax); double thetamax = Math.atan2(h - y0, w - x0); int xmax = (int) Math.round(rumax * Math.cos(thetamax)) * 2; int ymax = (int) Math.round(rumax * Math.sin(thetamax)) * 2; //System.out.println("xmax="+xmax); //System.out.println("ymax="+ymax); BufferedImage out = new BufferedImage(xmax, ymax, img.getType()); int newx0 = (int) Math.floor(xmax / 2) + 1; int newy0 = (int) Math.floor(ymax / 2) + 1; int dx = (int) ((xmax - w) / 2) - 1; int dy = (int) ((ymax - h) / 2) - 1; for (int x = 0; x < xmax; x++) { for (int y = 0; y < ymax; y++) { ru = Math.sqrt((x - newx0) * (x - newx0) + (y - newy0) * (y - newy0)); theta = Math.atan2(y - newy0, x - newx0); ww = Math.pow(ru / (2 * k) + Math.sqrt((ru * ru) / (4 * k * k) + 1 / (27 * k * k * k)), 1.0 / 3.0); rd = ww - 1 / (3 * k * ww); //nearest neighbour--------------------------------------- xd = (int) Math.round(rd * Math.cos(theta)) + x0; yd = (int) Math.round(rd * Math.sin(theta)) + y0; if (xd >= 0 && yd >= 0 && xd < w && yd < h) { //piksel nowy x,y = piksel stary xd,yd out.setRGB(x, y, img.getRGB(xd, yd)); } //--------------------------------------------------------- } } return out; }
From source file:edu.oregonstate.eecs.mcplan.domains.firegirl.FireGirlState.java
public void writePng(final File f) throws IOException { final int s = 10; final BufferedImage img = new BufferedImage(params.width * s, 2 * params.height * s, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < params.width; ++i) { final int x = i; for (int j = 0; j < params.height; ++j) { final int y = params.height - j - 1; int rgb = 0; final short age = stand_age[i][j]; final int bage = (int) (255 * age / ((double) params.max_age)); for (int k = 0; k < 3; ++k) { rgb |= ((bage & 0xff) << k * 8); }/*from ww w . j av a2 s . c om*/ for (int ix = 0; ix < s; ++ix) { for (int iy = 0; iy < s; ++iy) { img.setRGB(s * x + ix, s * y + iy, rgb); } } } } for (int i = 0; i < params.width; ++i) { final int x = i; for (int j = 0; j < params.height; ++j) { final int y = 2 * params.height - j - 1; int rgb = 0; final short fuel = fuel_load[i][j]; final int bfuel = (int) (255 * fuel / 1024.0); for (int k = 0; k < 3; ++k) { rgb |= ((bfuel & 0xff) << k * 8); } for (int ix = 0; ix < s; ++ix) { for (int iy = 0; iy < s; ++iy) { img.setRGB(s * x + ix, s * y + iy, rgb); } } } } ImageIO.write(img, "png", f); }
From source file:net.cloudkit.relaxation.CaptchaTest.java
public static void clearNoise(BufferedImage image) { final int width = image.getWidth(); final int height = image.getHeight(); /*//from ww w . j a v a 2 s .c o m for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, Color.WHITE.getRGB()); } } */ for (int x = image.getMinX(); x < width; x++) { for (int y = image.getMinY(); y < height; y++) { int point = image.getRGB(x, y); // int top = (y > 0) ? image.getRGB(x, y - 1) : -1; // ? int right = (x < width - 1) ? image.getRGB(x + 1, y) : -1; // int bottom = (y < height - 1) ? image.getRGB(x, y + 1) : -1; // int left = (x > 0) ? image.getRGB(x - 1, y) : -1; // ? int topRight = ((x < width - 1) && (y > 0)) ? image.getRGB(x + 1, y - 1) : -1; // int topLeft = ((x > 0) && (y > 0)) ? image.getRGB(x - 1, y - 1) : -1; // ? int bottomRight = ((x < width - 1) && (y < height - 1)) ? image.getRGB(x + 1, y + 1) : -1; // int bottomLeft = ((x > 0) && (y < height - 1)) ? image.getRGB(x - 1, y + 1) : -1; int i = 0; i = (top != -1) ? i + 1 : i; i = (right != -1) ? i + 1 : i; i = (bottom != -1) ? i + 1 : i; i = (left != -1) ? i + 1 : i; i = (topRight != -1) ? i + 1 : i; i = (topLeft != -1) ? i + 1 : i; i = (bottomRight != -1) ? i + 1 : i; i = (bottomLeft != -1) ? i + 1 : i; // System.out.println("i:" + i + ", top:" + top + ", right:" + right + ", bottom:" + bottom + ", left:" + left + ", topRight:" + topRight + ", topLeft:" + topLeft + ", bottomRight:" + bottomRight + ", bottomLeft:" + bottomLeft); if (i < 5) { image.setRGB(x, y, Color.WHITE.getRGB()); } /* int leftNearby = 0; if(left != -1) { int secondLeft = (x > 1) ? image.getRGB(x - 2, y) : -1; int threeLeft = (x > 2) ? image.getRGB(x - 3, y) : -1; leftNearby = ((left == -1) ? 0 : 1) + ((secondLeft == -1) ? 0 : 1) + ((threeLeft == -1) ? 0 : 1); } int rightNearby = 0; if(right != -1) { int secondRight = (x + 1 < width - 1) ? image.getRGB(x + 2, y) : -1; int threeRight = (x + 2 < width - 1) ? image.getRGB(x + 3, y) : -1; rightNearby = ((right == -1) ? 0 : 1) + ((secondRight == -1) ? 0 : 1) + ((threeRight == -1) ? 0 : 1); } int topNearby = 0; if(top != -1) { int secondTop = (y > 1) ? image.getRGB(x, y - 2) : -1; int threeTop = (y > 2) ? image.getRGB(x, y - 3) : -1; topNearby = ((top == -1) ? 0 : 1) + ((secondTop == -1) ? 0 : 1) + ((threeTop == -1) ? 0 : 1); } int bottomNearby = 0; if(bottom != -1) { int secondBottom = (y + 1 < height - 1) ? image.getRGB(x, y + 2) : -1; int threeBottom = (y + 2 < height - 1) ? image.getRGB(x, y + 3) : -1; bottomNearby = ((bottom == -1) ? 0 : 1) + ((secondBottom == -1) ? 0 : 1) + ((threeBottom == -1) ? 0 : 0); } System.out.println(leftNearby + " " + rightNearby + " " + topNearby + " " + bottomNearby); if (leftNearby != 2 && rightNearby != 2 && topNearby != 2 && bottomNearby != 2) { image.setRGB(x, y, Color.WHITE.getRGB()); } */ /* System.out.println(point - top); System.out.println(point - right); System.out.println(point - bottom); System.out.println(point - left); System.out.println(point - topRight); System.out.println(point - topLeft); System.out.println(point - bottomRight); System.out.println(point - bottomLeft); */ // if (point != top && point != right && point != bottom && point != left && point != topRight && point != topLeft && point != bottomRight && point != bottomLeft) { // image.setRGB(x, y, Color.WHITE.getRGB()); // } /* Color color = new Color(image.getRGB(x, y)); if((color.getBlue() < 120) || ((color.getRed() + color.getGreen() + color.getBlue()) < 50)) { // image.setRGB(x, y, Color.WHITE.getRGB()); } else if((color.getRed() + color.getGreen() + color.getBlue()) < 400) { image.setRGB(x, y, Color.BLACK.getRGB()); } */ Color color = new Color(image.getRGB(x, y)); if ((color.getRed() + color.getGreen() + color.getBlue()) < 600) { image.setRGB(x, y, Color.BLACK.getRGB()); } else { image.setRGB(x, y, Color.WHITE.getRGB()); } } } }