B4A Library Jpeg library

Cameras on devices are getting higher in resolution but displaying those images is not possible owing to memory limitations on the device. This library can decode arbitrary areas of large images without having to read the entire image into memory.

Unfortunately this can be rather slow. One reason is because Jpegs are optimally compressed with variable length coding and so it is not possible to locate and "jump" to the required point in the file to begin decoding because this cannot be found without decompressing the entire file up to that point.

However the main reason this process can be slow is because versions of Android before 2.2 interpret the bytecodes of their programs at runtime. Versions 2.2 of Android and later have a Just In Time compiler that compiles the bytecodes to native code and, according to reports that I have read, provide a speed increase of 4 or 5 times. The CPUs used by typical Android devices are increasing in their processing speed which again should give a processing speed advantage to the most recent devices. So locating the spaceman's head in the demo takes about 7 seconds on my 600Mhz Android 2.1 phone and comes down to around one second on a 1Ghz Android 2.2 or later device.

EDIT :- Version 1.1 now posted. See post #22 for details.

Note that if you like to test it in rapid debug mode then you need to add #DebuggerForceStandardAssets: true to your project.
 

Attachments

  • Jpeg1.1.zip
    344 KB · Views: 1,830
Last edited by a moderator:

derez

Expert
Licensed User
Longtime User
Andrew
The attached test doesn't work for me :BangHead:

Can you please look at it ?
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
Dim jpg As Jpeg
Dim in As InputStream
Dim bmp As Bitmap
Dim iv As ImageView

End Sub

Sub Activity_Create(FirstTime As Boolean)

activity.LoadLayout("j1")
jpg.Initialize("jpg")

in = File.OpenInput(File.DirAssets, "NIM0207.jpg")   
bmp = Jpg.LoadJpegSmaller(in, 2)
in = File.OpenInput(File.DirAssets,"NIM0207.jpg")
bmp = jpg.LoadJpegArea(in,100,100,220,220)

iv.SetBackgroundImage(bmp)
End Sub

It fails on the LoadJpegArea with an error saying "Array index out of bound" :confused:
 

Attachments

  • jpeg_test.zip
    311 KB · Views: 391

derez

Expert
Licensed User
Longtime User
Well, I did it also and it works now. Who could think of that but you ?
 

derez

Expert
Licensed User
Longtime User
Andrew
this test was for checking the time to load (as you said it is long).
For the navigation program loading of full bitmap of 1000x1000 is done in less than 1 sec, and four maps of that size in less than 2 sec. , using the standard bitmaps.
In B4ppc the jpeg library worked much faster and did improve the map replacements process (even with high Q). Here , as it is - it can't be used for it.

The library is still very useful for loading of pictures and I'll probably use it soon for that.
Thanks for you endless efforts.
 

agraham

Expert
Licensed User
Longtime User
Who could think of that but you ?
Not magic :) nor even particularly clever on my part! First thing I did was look at the Logcat output for the exception details and saw it was in the decoder in the library so I guessed there was some sort of format error in the file.

Its always worth looking there after an exception as Basic4android prints a stack trace to it if it catches an exception the application hasn't caught.

I guess you noticed the library comments about Android 2.2 and later plus faster processors. I'm hoping to get some indication soon as to what the actual speed-up will be on 2.2 with the Jitter and a 1Ghz CPU.
 

agraham

Expert
Licensed User
Longtime User
In the first post I postulated that Android versions later than 2.1 might be a lot faster owing to them Jitting the code. I have now received my Motorola Xoom :) and have found that it is about ten times faster than my ZTE Blade. It can locate the spacesman's head in the demo in about 700mS against the seven seconds for the Blade! :sign0162:
 

Biscuit

Member
Licensed User
Longtime User
This library is great however, I think I've found a bug or I'm just not using it correctly (not for the first time...). I am using the 'side effect' feature of being able to use it to quickly extract the ARGB values from a bitmap so it's not a bug with its core purpose.

I have attached a demo project showing what I'm doing and some images showing what I get. I am using the library to be able to quickly get at the underlying ARGB values for a bitmap but the results I get seem to show the original image 1/4 of the size with a green hue, repeated twice and the lower half of the image is black (actually the ARGB are 0).

I've tried this on the emulator and an HTC Wildfire and the results are the same on both.

Cheers
 

Attachments

  • PossibleJpegBug.zip
    40.5 KB · Views: 265
  • Original Bitmap.jpg
    Original Bitmap.jpg
    35.4 KB · Views: 250
  • Displayed Bitmap.jpg
    Displayed Bitmap.jpg
    37 KB · Views: 271

mistermentality

Active Member
Licensed User
Longtime User
I feel an idiot for asking but on the forum dll listings the jpeg library says it is v1.1 and the help page also says v1.1 if you search for the jpeg help via forum search but I can only find this thread having the jpeg library attached and that library is v1.0 so dumb question but am I missing a post somewhere that has a version 1.1 attached?

Dave
 

agraham

Expert
Licensed User
Longtime User
There is a version 1.1 but I decided not to go to the bother of issuing it as the only difference over 1.0 is a more informative exception message in the rare case of an error in the decoder(like "Jpeg decoder error" rather than "Array index out of bounds"). I've no idea why I might have sent you that xml as normally I would post the new version archive and then zip up the xml and email to you. One of lifes (many) mysteries!
 
Top