Author Topic: What is the best method to modify images with code.  (Read 5874 times)

Offline icarusfactor

  • int
  • **
  • Posts: 58
    • View Profile
What is the best method to modify images with code.
« on: February 03, 2011, 12:00:15 pm »
 I was wanting to take a Texture, load it into a alternate FrameBuffer, manipulate the texture,save it to Bitmap or convert it back to an Inputstream to create another texture.

Can it be done this way? or do I have another option.

USE CASE:
I have a simple bitmap font engine and want to render the text to an alternate FrameBuffer that has an overlay texture also blited to it, then save it to a Bitmap or to an InputStream to turn it back into a Texture so I don't have to re-render all the blited text over and over again.

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: What is the best method to modify images with code.
« Reply #1 on: February 03, 2011, 12:14:23 pm »
this should help  :)

Offline icarusfactor

  • int
  • **
  • Posts: 58
    • View Profile
Re: What is the best method to modify images with code.
« Reply #2 on: February 03, 2011, 02:21:32 pm »
 Thanks, will try that , looks like it treats a
 texture as a buffer , if it works , that will be
 what I need.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: What is the best method to modify images with code.
« Reply #3 on: February 03, 2011, 08:39:51 pm »
Please note that this method, albeit ported 1-to-1 from desktop jPCT, doesn't work on my sucky phone. I've no idea what it does on other devices.

Offline icarusfactor

  • int
  • **
  • Posts: 58
    • View Profile
Re: What is the best method to modify images with code.
« Reply #4 on: February 06, 2011, 01:20:31 pm »
You are correct Egon , it also does "not" work on my tablet.

I have written the code so no error occurs and should be rendering it
 to the main FrameBuffer for display as a texture, but just remains its
 original all alpha image. Was thinking it was an update problem then
 saw your post saying you could "not" do it either.

Wondering if any way you can access a texture via its int[] or byte[]
 array. Manipulate the the int[] array data, then convert the int[] data
back into texture data.

Will keep trying some other methods. 






 

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: What is the best method to modify images with code.
« Reply #5 on: February 06, 2011, 02:49:58 pm »
Wondering if any way you can access a texture via its int[] or byte[]
 array.
ITextureEffect will work for this, by implementing the apply(int[] dest, int[] source) method.  One of the projects I'm working on now does something similar, where I'm rendering a separate 3D scene (via the software renderer) onto an Image, and then using PixelGrabber in an ITextureEffect, I paint it onto a texture, creating a "game within a game".

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: What is the best method to modify images with code.
« Reply #6 on: February 06, 2011, 04:59:43 pm »
Or render it in a bitmap/drawable and create a texture from that. That's easier if all you want is to pre-render some blitting textures. If they change often, the ITextureEffect is the better choice though.

Offline icarusfactor

  • int
  • **
  • Posts: 58
    • View Profile
Re: What is the best method to modify images with code.
« Reply #7 on: February 06, 2011, 05:15:20 pm »
The Textures will be static documentation.
No change after it is blited to the texture.
Do you have an example of using Bitmap
 & Texture, copying image data back and forth?

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: What is the best method to modify images with code.
« Reply #8 on: February 06, 2011, 07:47:58 pm »
In case anyone else doesn't have a nice test case for you, here are some of the methods that you will be looking at (I just threw this together from the sourcecode for my Animated GIFs project, so it isn't really a complete working example):

Code: [Select]
import android.graphics.Bitmap;

Bitmap image = Bitmap.createBitmap( textureWidth, textureHeight, Bitmap.Config.ARGB_4444 );
int[] pixels = new int[image.getWidth() * image.getHeight()];

// Of course you can draw whatever you like into the pixels array.  Some other useful things:

// To copy the contents of the pixels array onto the Bitmap:
image.setPixels( pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight() );

// If you want to clear the bitmap:
if( useAlpha )
    image.eraseColor( Color.TRANSPARENT );
else
    image.eraseColor( Color.BLACK );

// Or if you have a "background" image that you always want to start with:
background.getPixels( pixels, 0, background.getWidth(), 0, 0,
    background.getWidth(), background.getHeight() );
image.setPixels( pixels, 0, background.getWidth(), 0, 0,
    background.getWidth(), background.getHeight() );

// To create a new Texture from the Bitmap:
Texture newTexture = new Texture( image, useAlpha );

// Then to replace an existing texture with the new one you created:
TextureManager.getInstance().replaceTexture( textureID, newTexture );

Offline icarusfactor

  • int
  • **
  • Posts: 58
    • View Profile
Re: What is the best method to modify images with code.
« Reply #9 on: February 06, 2011, 08:39:20 pm »
Thanks for the reply, That is just what i needed. Will try it out.

Offline icarusfactor

  • int
  • **
  • Posts: 58
    • View Profile
Re: What is the best method to modify images with code.
« Reply #10 on: February 07, 2011, 08:37:03 am »
Thanks, paulscode. I had tried out the snippet code, as long as I made the Bitmap Mutable I had access to each pixel and everything worked fine.