Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Advanced BufferedImage manipulation in pure Java  (Read 428 times)
0 Members and 1 Guest are viewing this topic.
Offline Sethir

Senior Newbie


Exp: 3 years



« Posted 2013-12-15 19:04:50 »

Hi guys,
I use BufferedImages to display my sprites and animations in my game. It works really fine and now I want to make things "fancy" Cheesy
On Stack Overflow did I found a way to manipulate an given image into a grayscale one. But how do I ink my images?
Given is a BufferedImage like this png:
given Imagegrayscaledinked
Red is just an example. I want to hand over a color and get the inked image as result.
My grayscale Code looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
   public static BufferedImage getGrayScaleImage(BufferedImage original) {
 
      GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
      GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
      GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
       
      // If the source image has no alpha info use Transparency.OPAQUE instead
     BufferedImage image = graphicsConfiguration.createCompatibleImage(original.getWidth(null), original.getHeight(null), Transparency.BITMASK);
       
      // Copy image to buffered image
     Graphics graphics = image.createGraphics();
       
      // Paint the image onto the buffered image
     graphics.drawImage(original, 0, 0, null);
      graphics.dispose();
       
      // Convert to grayscale
     ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
      ColorConvertOp op = new ColorConvertOp(cs, null);
       
      image = op.filter(image, null);
       
      return image;
   }


I haven't used ColorSpace or ColorConvertOp ever, so I don't how to use them  Huh

My second question is a bit more tricky. Like I said: I use images with transparent backgrounds. If I defeat an enemy, I want it to scatter like glas. Is there a way to do so? If so, how?
Offline Danny02
« Reply #1 - Posted 2013-12-15 21:18:17 »

you can implement the BufferedImageOps abstract class for that.
Offline davedes
« Reply #2 - Posted 2013-12-16 14:27:21 »

Scatter like glass: this is pretty tricky. You could triangulate a quad in a seemingly random way (or just a pre-split way) as if it's a pane of glass that cracked.  Then you'd give each "shard" a force going away from the center of the image. To render a shard you'd need to use TexturePaint to map that part of the image onto the triangle.

Pretty tricky with java2d and may not perform too well when you have lots of effects. It sounds like you're interested in graphics programming, maybe it's time to move up to OpenGL and shaders? Smiley

https://github.com/mattdesl/lwjgl-basics/wiki

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Sethir

Senior Newbie


Exp: 3 years



« Reply #3 - Posted 2013-12-16 19:19:32 »

Okay, I think I'll drop the glass scattering  Grin I don't want to use external libs fo this project.
Offline alesky

Junior Member


Medals: 3
Exp: 15 years


mmm....


« Reply #4 - Posted 2013-12-17 14:39:11 »

hello man

this is some of my code to make operation on images at run time in pure java it can help you

it help you to make this kind of operation on the images BRIGHTEN and TRANSPARENCY using lookup tables

use it is very easy just create the object and run a method

1  
2  
3  
4  
5  
BufferedImage myImmage = ....
BufferedImage effectImmage = null; //image altered by the effect

BrightenEffect effect = new BrightenEffect();
effectImmage  = effect.filterImage(myImmage,200,150,1,1); //BRIGHTEN effect on R and G channel






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  
package it.game.image.effect;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.LookupOp;
import java.awt.image.LookupTable;
import java.awt.image.ShortLookupTable;

public class BrightenEffect{

   short[] noChange;         //used to channel that don't change see the hapha channel
  short[] saturation;         //used to channel that don't change see the hapha channel  
  short[] unsaturation;      //used to channel that don't change see the hapha channel  

   public BrightenEffect(){
      noChange = calculateArrayNoChange();
      saturation = calculateArrayByCostant((short) 255);
      unsaturation = calculateArrayByCostant((short)0);
   }

   public BufferedImage copyImage(BufferedImage image){
      BufferedImage sourceBI = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
      Graphics2D g = (Graphics2D) sourceBI.getGraphics();
      g.drawImage(image, 0, 0, null);
      return sourceBI;
   }
   
   protected short[] calculateArrayByPercentage(double percentage) {
     
      if(percentage == 0) //COLOR_UNSATURATION
        return unsaturation;
      if(percentage == 1) //COLOR_ORIGINAL
        return noChange;      
      if(percentage == 255) //COLOR_SATURATION
        return saturation;
     
      short[] array = new short[256];
      short value;
      for (int i = 0; i < array.length; i++) {
         value = (short) (i*percentage);
         if(value> 255){
            value = 255;
         }
         if(value< 0){
            value = 0;
         }
         array[i] = value;
      }
      return array;
   }      

   private short[] calculateArrayNoChange() {
      short[] array = new short[256];
      for(int i=0; i < 256; i++) {
         array[i] = (short) i;
      }
      return array;
   }  
   
   private short[] calculateArrayByCostant(short constant) {
      short[] array = new short[256];
      for(int i=0; i < 256; i++) {
         array[i] = constant;
      }
      return array;
   }  



   /**
    * create a copy of the image changing the saturation on the 3 base color
    * the values of the color RChannel,GChannel,BChannel must be a value between 0 and 255
    * is possible to change also the transparency of the image changing the alphaChannel
    *
    * if the value of a  channel is 1 no change are done to that color
    *
    * so call the function with the the value (image,1,1,1,1) return an image identical to the original
    * so call the function with the value (image,1,0,0,1) return an image using the original red channel active and removing the other channels
    * so call the function with the value (image,255,0,0,1) return an image completely red
     * so call the function with the value (image,1,1,1,0.1) return an image transparent  
    *
    * etc....
    *
    *
    * @param image
    * @param RChannel
    * @param GChannel
    * @param BChannel
    * @return
    * @throws Exception
    */

   public BufferedImage filterImage(BufferedImage image, double RChannel, double GChannel, double BChannel,double alphaChannel) throws Exception {

      if(RChannel == 1 && GChannel == 1 && BChannel == 1)
         return image;
     
      BufferedImage source = copyImage(image);
     
      short[] R = calculateArrayByPercentage(RChannel);
      short[] G = calculateArrayByPercentage(GChannel);
      short[] B = calculateArrayByPercentage(BChannel);
      short[] A = calculateArrayByPercentage(alphaChannel);        
         
      short[][] table;
   
      table = new short[4][];
      table[0] = R;         //RED
     table[1] = G;         //GREEN
     table[2] = B;         //BLUE
     table[3] = A;    //ALPHA
 
      LookupTable lkTable = new ShortLookupTable(0, table);
      LookupOp operation = new LookupOp(lkTable, null);

      return operation.filter(source, null);
   }      

}










Offline Sethir

Senior Newbie


Exp: 3 years



« Reply #5 - Posted 2013-12-17 19:16:08 »

Wow, thanks. I altered your code a bit to fit my needs, but it works like a charm  Kiss
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!