Converting BufferedImage Into byte[ ] or maybe even String!

Posted by admin | Posted in Code Snippets | Posted on 28-01-2008

5

Profiling applications can be fun. For the last few days I’m working constantly on my final year project and one day I have decided to do quite a strange thing. As my application client (MIDP application) has to display a map and it makes heavy use of web services I have designed and implemented a web service which provides the map tiles for the mobile phone. To cut a long story short main performance bottelneck was to convert an image into byte array and send it over the network as a SOAP message. What my application server does is to cut large image 1024×1024 into small tiles 16×16 pixels and serve them as byte arrays (actually Base64 strings).
As the client is a mobile application all the processing of the image is done on the server side. The speed of such an implementation depends on 3 things, first we have to create the tile, download it and create the image from downloaded data. Guess what? The slowest part of the whole process was to create the tile on the server side!!!.
Just for testing, creating 64×64 matrix of tiles took about 28 fat long seconds on my 2.0 GHz dual core 2GB RAM laptop which of course is unacceptable.
Looking here and there I started profiling to find out where is the main bottelneck.
The main problem with this application is the MIDP, which does its job but unfortuntelly supports only PNG format. This limitation leads to another problem, ImageIO class writes PNG as slow as snails. I have created and profiled three methods to see the difference, in all of them I use same PNG image 1024×104 read before convertion into BufferedImage object, first method:

public byte[] testImageIO(){      try {          baos = new ByteArrayOutputStream();          ImageIO.write(image, "png", baos);      } catch (IOException ex) {          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);      }      return baos.toByteArray();  }

This method with mentioned image size (format .png) takes about 1800 ms to convert. Same method but with “jpeg” attribute inside write method of ImageIO takes about 550 ms (you can see the difference already). As mentioned above althought this cannot be used for my application as I get exception on the client side converting it back into image.
I was looking quite a long time on the internet before I’ve found the answer to my troubles.
Here you have thrid method which uses JPEGImageEncoder to do the converstion:

public byte[] testJPEGEncoder() {      try {          baos = new ByteArrayOutputStream();          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);          encoder.encode(image);      } catch (IOException ex) {          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);      } catch (ImageFormatException ex) {          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);      }      return baos.toByteArray();  }

Guess what!? It still works on my mobile side, it is called JPEGImageEncoder and does no harm to my bytes along the way, but as we are talking about performance here the result from Netbeans profiler is…….
Take a deep breath!
230 ms!
From 18000 ms to 230 ms! Never go back to ImageIO! ;)

Mythical Man Month On My Bookshelf

Posted by admin | Posted in Learning Materials | Posted on 27-01-2008

0


I was reading so often about this one on the net and in other books that it was on top of my reading list quite a long time. I had no idea what to expect from this book and I haven’t found there anything what recently I like the most, like tips on software development, refactorings to patterns or code tunning. Well it’s non of those things but of course it’s not the author’s fault. Definietly I should do some more research before buying this one, as I think this book is not for startups in software industry like me. I belive, people who manage projects and people resources enjoy reading this book much more than those who write software only.
I cannot say I didn’t like it though, overall is always nice to read about such a huge project like soft for IBM mainframe.
Maybe not really what I’m looking for these days but definietly valuable read!
Recommended!

Subversion Reports

Posted by admin | Posted in Tools | Posted on 27-01-2008

0

What a great piece of software for my final year project monthly reports! I have found tonight very little, very nice, very easy and most importantly very free tool on the web. It is called StatSVN and for those who don’t know yet what it is because they didn’ read this post title – it’s a subversion reporting tool. It does very easy job converting the log xml retrieved from the subversion server into human readable HTML document. Whoaaaa, I can’t belive sometimes how small things make me happy! Go ahead and try yourself!
Quick, easy and probably unnecessary tutorial on StatSVN:

1. Download the tool itself from sourceforge.net link,
2. Extract the zip file
3. Read the readme.txt :)
4. Check out your sources
5. Run ’svn log –xml -v > svn.log’ inside your project directory
6. Run java -jar statsvn.jar path_to_your_project_folder\svn.log path_to_your_project_folder
7. Wait
8. Go to the folder where statsvn.jar is and launch index.html
9. Feel guilty when you see no commits for a few days in a month :)

The reprots consists mostly of the diagrams and the table stating the developer of the month which I like the most as I’m the only one working on the project :) ))


Simply wonderful!

Code Complete on my bookshelf

Posted by admin | Posted in Learning Materials | Posted on 10-01-2008

1


Another Amazon’s 5 star rating book on my bookshelf. I have just finished this huge, over 800 pages, heavy as hell book. Well, in my opinion, it could be less than 300 or maybe even 200 pages, as most of it is so obvious that I just couldn’t belive someone has written book about it, hmmnn, maybe I’m wrong and maybe lots of people find it usefull, but still for me just 3 stars ;) .
What I have gained from reading this book? For sure I have stronger muscles on my back as I did carry it on the tube almost everyday :) , but beside that this book is of course not that bad and at the end of the day I would say it was a valuable read. I have enjoyed especially the chapter about code improvemens like refactorings and tuning and that is less than 150 pages! The rest, well, I think it’s good to brush up on some things a bit but if you have written a program with, lets say, at least 500 lines of code you are completly aware what correct intedation, comments, naming variables etc means not only for other people reading your code but especially to you!
One very good thing about this book is the fact that the author included tonnes of references to other books and research papers – heh, reading will never stop!