Slick Forums

Discuss the Slick 2D Library
It is currently Wed Aug 21, 2019 9:30 am

All times are UTC

Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Thu Aug 15, 2013 11:53 pm 

Joined: Thu Aug 15, 2013 11:17 pm
Posts: 11
EDIT : I figured out a (not very proper I must admit) way to solve the solution, see at the end of this post for code & explanations :)


I'm having an issue that has already been posted (but without fix), so I'm reposting it. It happens when with a resized Image(with .getScaledCopy(width,height) method) I call getColor(x,y) method; it seems to return the Color at (x,y) position of the very first (non resized) image.
I'm sorry I don't have precise code for it as it's really messy, maybe this image will help understanding :

To get those pictures i've drawn the image, then with a messy method i generate a polygon that approaches the "true" borders of the picture (transparent pixel excluded), and i draw it. Here we go :

-non resized image :

-resized image :

-resized image, and copy of the image by the following code :
Image img = new Image(imageToConvert.getTexture());


So if someone could help me for this, i'll be really grateful!

PS : i know, my green color is pretty ugly, was just for testing ;)

I figured out how to solve this problem in a not proper way which is converting my image to a bufferedImage, scale it and convert it back. I've been using some various codes I found on various forums. It may not be optimal but it works, so it's okay with me ^^
Here is how I call my method :
Image scaledImage = new Image("data/images/BoatsSets/0/Up.png");
      scaledImage  = resizeImage(scaledImage, 50, 100);

And here is the used methods. As i said, it may be not be optimal and if someone has a better way I would totally use it!

   private static Image resizeImage(Image originalImage, int newWidth, int newHeight){
      int type = BufferedImage.TYPE_INT_ARGB;
      BufferedImage originalBufferedImage = toBufferedImage(originalImage, true);
      java.awt.Image image = originalBufferedImage.getScaledInstance(newWidth, newHeight, java.awt.Image.SCALE_SMOOTH);
      BufferedImage buffered = new BufferedImage(newWidth, newHeight, type);
      buffered.getGraphics().drawImage(image, 0, 0 , null);
      BufferedImage b = new BufferedImage(buffered.getWidth(null),buffered.getHeight(null),BufferedImage.TYPE_INT_ARGB);
      ImageBuffer buf = new ImageBuffer(b.getWidth(),b.getHeight());
      int x,y,rgb;
      for (y = 0;y < b.getHeight();y++) {
         for (x = 0;x < b.getWidth();x++) {
            rgb = b.getRGB(x,y);
      return buf.getImage();
   private static java.awt.image.BufferedImage toBufferedImage(org.newdawn.slick.Image image, boolean hasAlpha) {
      // conver the image into a byte buffer by reading each pixel in turn
      int len = 4 * image.getWidth() * image.getHeight();
      if (!hasAlpha) {
         len = 3 * image.getWidth() * image.getHeight();

      ByteBuffer out = ByteBuffer.allocate(len);
      org.newdawn.slick.Color c;

      for (int y = 0; y < image.getHeight(); y++) {
         for (int x = 0; x<image.getWidth(); x++) {
            c = image.getColor(x, y);

            out.put((byte) (c.r * 255.0f));
            out.put((byte) (c.g * 255.0f));
            out.put((byte) (c.b * 255.0f));
            if (hasAlpha) {
               out.put((byte) (c.a * 255.0f));

      // create a raster of the correct format and fill it with our buffer
      DataBufferByte dataBuffer = new DataBufferByte(out.array(), len);

      PixelInterleavedSampleModel sampleModel;

      ColorModel cm;

      if (hasAlpha) {
         int[] offsets = { 0, 1, 2, 3 };
         sampleModel = new PixelInterleavedSampleModel(
               DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 4,
               4 * image.getWidth(), offsets);

         cm = new ComponentColorModel(ColorSpace
               .getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 },
               true, false, ComponentColorModel.TRANSLUCENT,
      } else {
         int[] offsets = { 0, 1, 2};
         sampleModel = new PixelInterleavedSampleModel(
               DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 3,
               3 * image.getWidth(), offsets);

         cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
               new int[] {8,8,8,0},
      WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));

      // finally create the buffered image based on the data from the texture
      // and spit it through to ImageIO
      return new BufferedImage(cm, raster, false, null);       

Hope this helps some people!

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 7 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group