Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: davedes (posted 2013-06-14 10:03:55, viewed 231 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200  
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class BlurTest {

   
   public static void main(String[] args) throws Exception {
      BufferedImage image = ImageIO.read(BlurTest.class.getResource("/data/test.png"));
      
      int w = image.getWidth();
      int h = image.getHeight();
      int[] orig = new int[w * h];
      
      //copy BufferedImage into array
      image.getRGB(0, 0, w, h, orig, 0, w);
      
      int[] blurred = blur(orig, w, h, 3, 3);
      
      image.setRGB(0, 0, w, h, blurred, 0, w);
      
      ImageIO.write(image, "PNG", new File("test3.png"));
   }
   
   /*
    * Copyright (c) 2007, Romain Guy All rights reserved.
    * 
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are
    * met:
    * 
    * * Redistributions of source code must retain the above copyright notice,
    * this list of conditions and the following disclaimer. * Redistributions
    * in binary form must reproduce the above copyright notice, this list of
    * conditions and the following disclaimer in the documentation and/or other
    * materials provided with the distribution. * Neither the name of the
    * TimingFramework project nor the names of its contributors may be used to
    * endorse or promote products derived from this software without specific
    * prior written permission.
    * 
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    */
   /**
    * <p>
    * Blurs the source pixels into the destination pixels. The force of the
    * blur is specified by the radius which must be greater than 0.
    * </p>
    * <p>
    * The source and destination pixels arrays are expected to be in the RGBA
    * format.
    * </p>
    * 
    * @param srcPixels
    *            the source pixels
    * @param dstPixels
    *            the destination pixels
    * @param width
    *            the width of the source picture
    * @param height
    *            the height of the source picture
    * @param radius
    *            the radius of the blur effect
    * @author Romain Guy <romain.guy@mac.com>
    */
   public static void blurPass(int[] srcPixels, int[] dstPixels, int width,
         int height, int radius) {
      final int windowSize = radius * 2 + 1;
      final int radiusPlusOne = radius + 1;
 
      int sumRed;
      int sumGreen;
      int sumBlue;
      int sumAlpha;
 
      int srcIndex = 0;
      int dstIndex;
      int pixel;
 
      int[] sumLookupTable = new int[256 * windowSize];
      for (int i = 0; i < sumLookupTable.length; i++) {
         sumLookupTable[i] = i / windowSize;
      }
 
      int[] indexLookupTable = new int[radiusPlusOne];
      if (radius < width) {
         for (int i = 0; i < indexLookupTable.length; i++) {
            indexLookupTable[i] = i;
         }
      } else {
         for (int i = 0; i < width; i++) {
            indexLookupTable[i] = i;
         }
         for (int i = width; i < indexLookupTable.length; i++) {
            indexLookupTable[i] = width - 1;
         }
      }
 
      for (int y = 0; y < height; y++) {
         sumAlpha = sumRed = sumGreen = sumBlue = 0;
         dstIndex = y;
 
         pixel = srcPixels[srcIndex];
         sumRed += radiusPlusOne * ((pixel >> 24) & 0xFF);
         sumGreen += radiusPlusOne * ((pixel >> 16) & 0xFF);
         sumBlue += radiusPlusOne * ((pixel >> 8) & 0xFF);
         sumAlpha += radiusPlusOne * (pixel & 0xFF);
 
         for (int i = 1; i <= radius; i++) {
            pixel = srcPixels[srcIndex + indexLookupTable[i]];
            sumRed += (pixel >> 24) & 0xFF;
            sumGreen += (pixel >> 16) & 0xFF;
            sumBlue += (pixel >> 8) & 0xFF;
            sumAlpha += pixel & 0xFF;
         }
 
         for (int x = 0; x < width; x++) {
            dstPixels[dstIndex] = sumLookupTable[sumRed] << 24
                  | sumLookupTable[sumGreen] << 16
                  | sumLookupTable[sumBlue] << 8
                  | sumLookupTable[sumAlpha];
            dstIndex += height;
 
            int nextPixelIndex = x + radiusPlusOne;
            if (nextPixelIndex >= width) {
               nextPixelIndex = width - 1;
            }
 
            int previousPixelIndex = x - radius;
            if (previousPixelIndex < 0) {
               previousPixelIndex = 0;
            }
 
            int nextPixel = srcPixels[srcIndex + nextPixelIndex];
            int previousPixel = srcPixels[srcIndex + previousPixelIndex];
 
            sumRed += (nextPixel >> 24) & 0xFF;
            sumRed -= (previousPixel >> 24) & 0xFF;
 
            sumGreen += (nextPixel >> 16) & 0xFF;
            sumGreen -= (previousPixel >> 16) & 0xFF;
 
            sumBlue += (nextPixel >> 8) & 0xFF;
            sumBlue -= (previousPixel >> 8) & 0xFF;
 
            sumAlpha += nextPixel & 0xFF;
            sumAlpha -= previousPixel & 0xFF;
         }
 
         srcIndex += width;
      }
   }
 
   /**
    * Blurs (in both horizontal and vertical directions) the specified RGBA
    * image with the given radius and iterations.
    * 
    * @param inputRGBA
    *            the image pixels, in RGBA format
    * @param width
    *            the width of the image in pixels
    * @param height
    *            the height of the image in pixels
    * @param radius
    *            the radius of the blur effect
    * @param iterations
    *            the number of times to perform the blur; i.e. to increase
    *            quality
    * @return the blurred pixels
    */
   public static int[] blur(int[] inputRGBA, int width, int height,
         int radius, int iterations) {
      int[] srcPixels = new int[width * height];
      int[] dstPixels = new int[width * height];
      
      // copy input into srcPixels
      System.arraycopy(inputRGBA, 0, srcPixels, 0, srcPixels.length);
 
      for (int i = 0; i < iterations; i++) {
         // horizontal pass
         blurPass(srcPixels, dstPixels, width, height, radius);
         // vertical pass
         blurPass(dstPixels, srcPixels, height, width, radius);
      }
 
      // the result is now stored in srcPixels due to the 2nd pass
      return srcPixels;
   }
 
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (80 views)
2014-04-15 18:08:23

BurntPizza (72 views)
2014-04-15 03:46:01

UprightPath (83 views)
2014-04-14 17:39:50

UprightPath (67 views)
2014-04-14 17:35:47

Porlus (83 views)
2014-04-14 15:48:38

tom_mai78101 (107 views)
2014-04-10 04:04:31

BurntPizza (167 views)
2014-04-08 23:06:04

tom_mai78101 (263 views)
2014-04-05 13:34:39

trollwarrior1 (213 views)
2014-04-04 12:06:45

CJLetsGame (222 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!