You Are Here:

Community: Developer Discussion Boards

#1 Old how to create pixel arrays - 2004-05-15, 20:00

Join Date: May 2004
Posts: 4
zavagoo
Offline
Registered User
Hi,

i want to create pixels array from a png that i can put in a binary file.
I want to use this binary file in my jar instead of using a png file.

Is there any tool that convert a png to an array of shorts?

Thanks.
Reply With Quote

#2 Old 2004-05-16, 18:45

Join Date: Jul 2003
Posts: 1,094
Location: Finland, Tampere
doctordwarf
Offline
Super Contributor
Do some search on the forum. There were several topics describing different ways of doing this
Reply With Quote

#3 Old 2004-05-17, 15:31

Join Date: Sep 2003
Posts: 60
smb101
Offline
Regular Contributor
zavagoo,

I searched for a tool that would convert a png file into an array of shorts but had no success.

I'm gonna start building my own tool for this purpose.... hopefully won't take too long.

If I get it working soon I'll post the code here.
Cheers.
Reply With Quote

#4 Old Hint - 2004-05-17, 20:19

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
Well, we had this discussion a long time ago but now I can't find that thread, but the idea is to use the emulator.
The DirectGraphics.getPixels() function can already get the pixels from a picture in exactly the format you need. If you take the byte array it returns and save it to the RMS using the emulator, then you just have to open the RMS file on your computer (usually in a "jam-apps" directory or something like that, under the application's directory), strip a few RMS headers from it and you're done.

shmoove
Reply With Quote

#5 Old shmoove - 2004-05-22, 13:39

Join Date: Mar 2003
Posts: 26
rikard_wigforss@hotmail.com
Offline
Registered User
Do you remember how many bytes the RecordStore headers were?
Reply With Quote

#6 Old 2004-05-23, 09:21

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
Nope. Sorry. But if you right some dummy byte arrays with values you can easily pick out in the RMS and then look at the file with a hex editor you can probably find that out easily.

shmoove
Reply With Quote

#7 Old 2004-06-01, 15:37

Join Date: May 2004
Posts: 4
zavagoo
Offline
Registered User
Hi,

thanks to all for your answers, especially shmoove and smb101.

If you know about a j2se tool that can generate raw data from a png file, i'm interested. Or even if you can code such a tool (i mean smb101 ;-)), tell me!

In my task list, at one moment i'll have to use such a tool. So if i can't find one, i think i'll try to code it myself, even if i don't know anything about bitmaps, png and graphic datas...

Thank you again folks!

See you.
Reply With Quote

#8 Old It seems to work.... - 2004-06-04, 11:14

Join Date: Sep 2003
Posts: 60
smb101
Offline
Regular Contributor
zavagoo,

Here's some rough code I done last night.... worked with a 32x32 png sprite but will still need some work but basically I used the method described by shmoove.

1) load the .png file

2) grab the pixels

3) write these pixels to the RMS (*** stage 1-3 is done using function grabImagePixels in the code below)

4) find the rms file on you HDD and manually remove the header from the RMS file. Once I had run the code on the emulator I found the RMS file in "c:\Nokia\Devices\Nokia_7210_SDK_v1_0\bin\jam-apps\(project)\RMS". If you edit this and search for the string "*** START HERE ***" and delete everything before and including this. This is to remove the RMS header information which is of no use to you. In addition it seems there may be some record header info(or something like that) so you have to delete some more data from the start.

5) add this new file as a resource in your project

6) use func readPNGBytes to load the file and populate the short[] pixel array.

Sorry about the state of the code... as I said I done it late last night so I didn't have time to tidy up the code.

hopefully you'll be able to use these funcs as a starting point.
Any questions then please pose them here and I'll get back to you asap.

Cheers




import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import com.nokia.mid.ui.*;
import java.io.*;
import javax.microedition.io.*;


public class MainCanvas extends FullCanvas
{
protected short imgPixels[]; // orig pixel aray
protected short readPixels[]; // file copy pixel array
protected int width, height;

public MainCanvas()
{
width = 0; height = 0;
}

//******************************************************************************

public void start(MyBackgroundMain MIDlet)
{
grabImagePixels("test.png"); // loads the img into imgPixels and sets width and height vals
readPNGBytes("OutPut.txt", width, height); // reads the file and puts pixels into readPixels
// will be the same dim as the original sprite

Display.getDisplay(MIDlet).setCurrent(this);
}

//******************************************************************************

public void paint(Graphics g)
{
// blank screen
g.fillRect(0,0,128,128);
DirectGraphics dg = DirectUtils.getDirectGraphics(g);

// draw original pixel array
dg.drawPixels(imgPixels, false, 0, width, 0, 0, width, height, 0, DirectGraphics.TYPE_USHORT_4444_ARGB);
g.setColor(0xAAAAAA);
g.drawString("Original", 40, 10, g.TOP|g.LEFT);

// draw pixel array from file
dg.drawPixels(readPixels, false, 0, width, 0, 50, width, height, 0, DirectGraphics.TYPE_USHORT_4444_ARGB);
g.setColor(0xAAAAAA);
g.drawString("Copy", 40, 60, g.TOP|g.LEFT);
}


//******************************************************************************
public void grabImagePixels(String file)
{
Image mainImg=null, LoadImg=null;

// load orig img
try {
LoadImg = Image.createImage("/"+file);
height = LoadImg.getHeight();
width = LoadImg.getWidth();
System.out.println("LOAD = "+file);
}
catch (IOException e) {
System.out.println("ERROR! /"+file+" Failure");
}


// grab pixel array
imgPixels = new short[width*height];

mainImg = DirectUtils.createImage(width, height, 0);
DirectGraphics dg = DirectUtils.getDirectGraphics(mainImg.getGraphics());

dg.drawImage(LoadImg, 0, 0, Graphics.TOP | Graphics.LEFT, 0);
dg.getPixels(imgPixels, 0, width, 0, 0, width, height, DirectGraphics.TYPE_USHORT_4444_ARGB);

// write pixel array to RMS
PersistantData store;
store = new PersistantData();
store.writeToStore(width, height, imgPixels);

// don't need the images now.
mainImg=null;
LoadImg=null;
System.gc();
}

//******************************************************************************
void readPNGBytes(String filename, int width, int height)
{
// load sprite from resource
System.out.println("LOAD PNG BYTES = "+ filename);
InputStream is = this.getClass().getResourceAsStream("/"+filename);

int totalSize = width * height;
readPixels = new short[totalSize];

try {

for (int i = 0; i < totalSize; i++)
{
// read 2 bytes at a time which make up 1 short value
byte one = (byte)is.read();
byte two = (byte)is.read();
readPixels[i] = (short) ((one << 8) + two);
}
}
catch (IOException e)
{}
}
//******************************************************************************
}



/* This file was created by Nokia Developer's Suite for J2ME(TM) */
import java.util.*;
import javax.microedition.rms.*;


public class PersistantData
{
private RecordStore recordStore;

static private final String RECORD_LABEL = "PNGOutput";

//******************************************************************************
public PersistantData()
{
}
//******************************************************************************
private void initStore()
{
try
{
recordStore = RecordStore.openRecordStore(RECORD_LABEL, true);
if (recordStore.getNumRecords() > 0)
{
// delete and start again
recordStore.closeRecordStore();
recordStore.deleteRecordStore(RECORD_LABEL);
recordStore = RecordStore.openRecordStore(RECORD_LABEL, true);
}
}
catch (RecordStoreException e)
{
e.printStackTrace();
closeStore();
}
}
//******************************************************************************
public void writeToStore(int w, int h, short[] pixels)
{
System.out.println("writeStore()");
initStore();
try
{
int startPos=0;
int srcSize = pixels.length;

byte[] newArray = new byte[srcSize*2]; //2 bytes for 1 short

for (int i=0; i<srcSize; i++)
{
newArray[startPos] = (byte) ((pixels[i] & 0xFF00) >> 8); // high byte first
newArray[startPos+1] = (byte) (pixels[i] & 0x00FF); // low byte next

startPos+=2;
}

String startText = "***START HERE***";
recordStore.addRecord(startText.getBytes(), 0, startText.length());
recordStore.addRecord(newArray, 0, newArray.length);
}
catch (RecordStoreException e) {
e.printStackTrace();
closeStore();
}
closeStore();
}
//******************************************************************************
private void closeStore()
{
try {
recordStore.closeRecordStore();
}
catch (RecordStoreException e) {
e.printStackTrace();
}
}
//******************************************************************************
}
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d134434X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ