Trying to Load a BufferedImage from a PNG through JNI

Yes I realize that I can load it directly through Java and not go through JNI, but I need this to test correctly as my C code will aquire images without saving to Disk and I want Java code to manipulate the images without saving to disk. The image Im trying to load is a 800x600 PNG image.
So I have the code
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
import com.sun.media.jai.widget.DisplayJAI;
class ImageLoader{
     static{
          System.loadLibrary("LoadImage");
     private static native byte[] loadImage();
     public static void main(String[] args){
byte[] f = loadImage();
          InputStream s= new ByteArrayInputStream(f);
          BufferedImage bi;
          try {
               bi = ImageIO.read(s);
               displayBufferedImage(bi, "test");
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
      * Utility method for display a BufferedImage.<br>
      * @param  image input image
      * @param  title window title string
     public static void displayBufferedImage(BufferedImage image, String title)
          JFrame frame = new JFrame();
         frame.setTitle(title);
         //frame.getContentPane().add(new DisplayTwoSynchronizedImages(sourceImgBI, resultImgGray));
         frame.getContentPane().add(new JScrollPane(new DisplayJAI(image)));
         //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         //frame.pack();
         frame.setSize(300,300);
         frame.setLocationRelativeTo( null );
         frame.setVisible(true); // show the frame.
     }and I have the JNI c code as
char* result;
JNIEXPORT jbyteArray JNICALL Java_ImageLoader_loadImage
  (JNIEnv * env, jclass jclassj){
               int fd = open("test.png",  O_RDWR, S_IRUSR|S_IWUSR );
               int size = (800*600*4);
               result = (char*)malloc(size);
               read(fd, result, size);
               jbyteArray return_result;
               return_result = env->NewByteArray(800*600);
               env->SetByteArrayRegion(return_result, 0, 800*600, (jbyte*)result);
               //free(result);
               return return_result;
     }but when I try to run the code I get the following error
javax.imageio.IIOException: Error reading PNG image data
     at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1287)
     at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1552)
     at javax.imageio.ImageIO.read(ImageIO.java:1438)
     at javax.imageio.ImageIO.read(ImageIO.java:1342)
     at ImageLoader.main(ImageLoader.java:31)
Caused by: java.io.EOFException: Unexpected end of ZLIB input stream
     at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
     at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
     at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
     at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
     at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
     at java.io.DataInputStream.readFully(DataInputStream.java:195)
     at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1084)
     at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1188)
     at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1280)
     ... 4 moreI think that Im on the right track because Java is recognizing that this is a PNG image, but Im not sure whats wrong. I don't know much about the formats, so I know nothing about PNG's, maybe someone else can help me here.
Note: I ran the code
               BufferedImage bi2 = ImageIO.read(new File("test.png"));
               displayBufferedImage(bi2, "test2");and the image loads fine, so there is no issue with the image itself.

               int fd = open("test.png", O_RDWR, S_IRUSR|S_IWUSR );Why are you opening the image file read/write? Why not read-only?
               int size = (800*600*4);Here you're assuming the image is 800*600. Are you sure that's the case? Also you should write sizeof int here instead of 4.
               result = (char*)malloc(size);Here you aren't checking for the possibility that result is null.
               read(fd, result, size);Here you are ignoring the result returned by read(), so you are ignoring the possibilty that it was 0 or less than 'size'. Either are possible.
               jbyteArray return_result;
               return_result = env->NewByteArray(800*600);Here again you aren't testing for return_result == null.
               env->SetByteArrayRegion(return_result, 0, 800*600, (jbyte*)result);Here you should be checking for Java exceptions. Also you shouldn't be repeating the 800*600 here, make it a constant or a variable somewhere.
               //free(result);You need that free, otherwise you have a memory leak. If the image really is a constant 800*600 in size I would allocate 'result' on the stack so you don't need to housekeep it.
Also you are never closing the input file!
Caused by: java.io.EOFException: Unexpected end of ZLIB input streamSo clearly you didn't read all the image. That could be due to guessing wrong about 800*600 or getting a short read when reading the file.
I think that Im on the right track because Java is recognizing that this is a PNG imageI agree. It starts like a PNG image, so you read something, but it doesn't end like one, so you didn't read it all.> and the image loads fine, so there is no issue with the image itself.
Good test. Note that your Java code makes no assumption about the size of the image.

Similar Messages

  • I downloaded several cds to my external hard drive in .mp3 format. Later, I copied them to iTunes. The songs only play when the external hard drive is plugged in. I tried to load the songs from iTunes to my iPhone and the songs don't play.

    I downloaded several cds to my external hard drive in .mp3 format. Later, I copied them to iTunes. The songs only play when the external hard drive is plugged in. I tried to load the songs from iTunes to my iPhone and the songs don't play. I thought iTunes allowed me to transfer music to my iPhone and play it. What am I doing wrong?

    Hi skoorb1,
    When adding music to iTunes, you can set it to either make a local copy of the file, or simply point to the original location of the file (for example, on an external hard drive). If it is set to do the latter, it will not be able to play or transfer those files when the external hard drive is not plugged in. You may find the following article helpful:
    iTunes: About the Add to Library, Import, and Convert functions
    http://support.apple.com/kb/ht1347
    Regards,
    - Brenden

  • My iphoto will not open for some reason. the cursor just keeps running...been using this for 3 years with no problem. It started doing this after I tried to load some photos from a camera I have successfully used before

    I cannot open my Iphoto on my macbook pro. I have used it for three years with no problem. However, after trying to load some pictures from my camera it seems to have frozen up. I force quit it and have not been able to get past the opening blank window since ...cursor still blinking as if it is downloading or trying to open.

    What version of iPhoto?

  • HT5622 Trying to load an app from the itunes store and asked to review the "Terms and Conditions and Apple Privacy Policy" as it has changed.  There is no "Agree" selection available.  Any suggestions?  This appears to be associated with the iOS7 upgrade.

    Trying to load an app from the itunes store and asked to review the "Terms and Conditions and Apple Privacy Policy" as it has changed.  There is no "Agree" selection available.  Any suggestions?  This appears to be associated with the iOS7 upgrade. Thanks

    I have the same problem with my iphone 4 . I solve it with giving permission to the cookies on safari from the settings.

  • Trying to Load CVS 2 from Harddrive

    Trying to load cvs2 from my harddrive and will not pickup second cd from my hard drive what can I do?

    I am sorry Input93 but which Adobe software or service are you referring too?

  • TS3212 I just purchased a new iphone 4s and I am trying to load some music from my itues. I am getting a message that says not enough space. I checked my phone and it shows i have 5.1G available. do you know how to fix this problem?

    I just purchased a new iphone 4S and I'm trying to loan some music from itunes. I keep getting a message that says not enough space on my phone. I checked my phone and I have 5.1G available. I deselected all the songs on my itunes to try and get it to sync and I keep getting the same message. Any tips?

    Hey csumner1,
    Thanks for the question. I understand you are receiving a "Not enough free space" alert when attempting to sync your new iPhone 4s. The following article may help to resolve your issue:
    iOS: "Not enough free space" alert when trying to sync
    http://support.apple.com/kb/TS1503
    Resolution
    Try disconnecting and reconnecting your device, then syncing again to solve this issue.
    Orphaned files may remain on your iOS device if it is physically disconnected while syncing music, podcasts, videos, or photos. This can prevent iTunes from syncing the iOS device on subsequent sync sessions. When this happens, the Capacity indicator in iTunes may report a large amount of "Other" disk usage for the iOS device. To resolve this issue:
    1. Turn off the music or photo sync option in iTunes for the device.
    2. Click Apply to sync the changes to the device.
    3. Turn the music and photo sync options for the device on again.
    4. Click Apply again to try to sync the device to iTunes.
              - If the 'Not enough free space' alert appears, continue to step 5.
    5. Turn off the automatic syncing functions for the iOS device. To do this:
              - Select the iOS device from the iTunes window and click the Summary tab.
              - Deselect "Automatically sync when this device is connected" and select the "Sync only checked songs and videos" checkbox.
              - Click Apply to sync the changes to the device.
              - Reduce the amount of data that is being synced to the device and resync the device. For example, if syncing your Music library exceeds the memory capacity of the device, choose "Selected playlists" to transfer rather than "All songs and playlists" under the Music tab in iTunes.
    If the 'Not enough free space' alert appears, continue to step 6.
    6. Restore using iTunes.
    Thanks,
    Matt M.

  • I am trying to load my contacts from outlook but not getting through

    I am unable to load my list of contact from outlook unto my iphone 5s

    What method of syncing contacts are you trying?  Are you using iTunes to sync Contacts?  Are you using iCloud Control Panel to link Outlook to iCloud, and then syncing Contacts from iCloud to your iPhone?

  • Error when trying to load iPhoto library from External HD

    I copied my iPhoto library to my 3TB Seagate External HD to save space on my MacBook Air, but when I try to load that library (either by holding option as I open it and select the one in my HD or by double clicking the one in my HD) I get this error message: "Your photo library is either in use by another application or has become unreadable." My iPhoto library is taking up too much space, please help!

    What format is that external drive?

  • Tries to load a page from site but never loads, Then need to close Mozilla and reopen it. Told that it is already running, not, then often have to do a hard turn off to shut it down. Why is this happening, and what can be done?

    This happened many times today. There is no specific page. I usually have a page open and try to redirect from that page. It shows "connecting", but never does. I have run many scans which all turn up clean. Why is this happening and what can be done to fix it?

    Maybe here:
    http://www.windowserrorhelps.com/itunes-error-11-how-can-i-fix-itunes-error-11-i nstantly
    Is the iPod jailbroken?

  • Master Data Load to APO from SAP MDM through SAP PI

    Hi SDners,
    This is a Parts Master Data Solution for one of the Largest Auto Manufacturer where SAP MDM will be the central hub of all the Global and Local attributes of Parts . The subscribing system is SAP SCM APO . Please advice if there is any possibility of direct integration between APO -MDM through PI. Does PI has the standard IDOC types to communicate with APO.
    Also does APO has some RFC/BAPI to do master data load for Product Master /n/sapapo/mat1
    Thanks,
    Prabuddha

    Hi,
    Check the LUWs in SM58 in source system and then execute it. Else check in BD87 and push the IDocs manually in ECC.
    Thanks
    Reddy
    Edited by: Surendra Reddy on Feb 24, 2009 10:59 AM

  • Unable to load the data from PSA to INFOCUBE

    Hi BI Experts, good afternoon.
        I am loading 3 years data( Full load ) from R/3 to Infocube.
       So loaded the data by monthwise. So i created 36 info packages.
      Everything is fine. But i got a error in Jan 2005 and Mar 2005. It is the same error in both months. That is Caller 01and caller 02 errors( Means invalid characteristics are there PSA data )
    So i deleted both PSA and Data target Requests and again i loaded the data only to PSA.
      Here i got data in PSA without fail.
      Then i tried to load the data from PSA to Infocube MANUALLY.
    But its not happening.
      One message came this
           SID 60,758 is smaller than the compress SID of cube ZIC_C03; no        request booking.
       Please give me the solution how to solve this problem.
      Thanks & Regards
         Anjali

    Hi Teja,
       Thanks for the good response.
      How can i check whether it is already compressed or not?
      Pls give me the reply.
      Thanks
              Anjali

  • Loading Null values from source to target

    Hello all,
    Please help me in this issue....
    I am trying to load data from source ( state column, varchar50) into target (state column,varchar10).....
    in my source i have 10records for state...... 9states with lenght<10 and 1 NULL value......
    when i am trying to load the records from source to target...i am getting this error.....
    12899 : 72000 : java.sql.SQLException: ORA-12899: value too large for column "TARGET"."STATE" (actual: 11, maximum: 10)
    why is taking the lenght of NULL as 11...........
    Thanks

    there might be some special characters or international characters instead of Varchar2(10 byte ) try varchar2(10 char) and try again. As some of the unicode symbols or characters can take two byes even though they are single characters

  • Error while loading the Hierarchy from R/3 to BI.

    Dear all ,
    I am trying to load a hierarchy FROM R/3 to BI . When I am executing the Info package its showing an below Error.
    Hierarchy object is u201CWBS elementu201D
    The level of the node ID 01494179 does not suit the lev. of the higher lev. node
    The level of the node ID 01494178 does not suit the lev. of the higher lev. node
    The level of the node ID 01493822 does not suit the lev. of the higher lev. node
    Could you please let me know what would be the reasonu2026u2026?
    Regards,
    Raghu

    Hi
    looks like there is some inconsistency in hierchy data you are tring to laod.
    Check hierarchy in r3 if its ok, if yes try deleting complete hierarchy and reloading it.
    You may also chose to adjust it manually
    Regards
    Sudeep

  • Error while loading the data from ODS to InfoCube

    hai
    Im trying to load the data from ODS to InfoCube for particular year .
    But it says that there is a source system problem .
    why it is like that .
    pls tell me
    i ll assing the points
    rizwan

    Hi Rizwan,
    you didn't mention the error message in details. there could be a few places to be checked:
    - check if BW itself source system is active and in tact and reactivate if necessary
    - check if update rule is active and reactivate if necessary
    - check if ODS is active and reactivate if necessary
    Regards,
    Lilly

  • Problem While loading Master data from R/3

    Hi All,
    When i am trying to load masters data from R/3 datasources to BW Info objects in Info provider menu data is coming to PSA but going to dta targets. the data is matching with the data in R/ 3 but
    Missing message: Request received
    Missing message: Number of sent records
    Missing message: Selection completed
    Data Package 1 : arrived in BW ; Processing : Selected number does not agree with transferred n
    Info IDoc 1 : sent, not arrived ; IDoc ready for dispatch (ALE service)
    and error message says ERROR 4 in the update.
    Can you please help me in this regard . Its Urgent.

    Hi,
    Check for pending IDOCS in BD87 or SM58 and execute them.
    In RSMO, select the particular load you want to monitor.
    In the menu bar, Environment >>> Transact. RFC >>> Select whichever is required, BW or Source System.
    In the next screen you can select the Execute button and the IDOCS will be displayed.
    Refer this Tcodes for IDOC status.
    WE02 Display IDoc
    WE05 IDoc Lists
    WE06 Active IDoc monitoring
    WE07 IDoc statistics
    WE08 Status File Interface
    WE09 Search for IDoc in Database
    WE10 Search for IDoc in Archive
    WE23 Verification of IDoc processing
    IDOC
    www.erpgenie.com/sapgenie/docs/ale_whitepaper.doc
    Check Note 561880 - Requests hang because IDocs are not processed.
    OR
    Transact RFC error 
    tRFC Error - status running Yellow for long time (Transact RFC will be enabled in Status tab in RSMO).
    Step 1:  Goto Details, Status get the IDoc number,and go to BD87 in R/3,place the cursor in the RED IDoc entroes in tRFC
    queue thats under outbound processing and click on display the IDOC which is on the menu bar.
    Step 2: In the next screen click on Display tRFC calls (will take you to SM58 particular TRFC call)
    place the cursor on the particular Transaction ID and go to EDIT in the menu bar --> press 'Execute LUW'
    (Display tRFC calls (will take you to SM58 particular TRFC call) ---> select the TrasnID ---> EDIT ---> Execute LUW)
    Rather than going to SM58 and executing LUW directly it is safer to go through BD87 giving the IDOC name as it will take you
    to the particular TRFC request for that Idoc.
    Thanks,
    JituK

Maybe you are looking for

  • Search Remote Files... or ...How Do I Delete All of My LCK Files?

    hey everyone. i need to delete a bunch of .LCK files from my server so Dreamweaver will stop telling me someone else has stuff checked-out and that i need to enable blah blah blah. i can't just check everything in because A. that would take forever a

  • Query Designer Issue - Bucket

    Hi , I have got a new reuirement, Could you please help me out how can we achieve this. The data in the cube looks below: Product Customer Start Date End Date Amount P1 Cust1 23-Apr-12 13-May-12 20 P1 Cust2 05-May-12 13-May-12 40 P2 Cust3 12-Sep-12 1

  • We could not complete your request. there was an error in the app store. please try again later. (20)

    Hi all, i am getting the below message when trying to download the imove and iphoto update from the app store app on my mac book. I am able to download this on my imac with the same apple id and password, but when i try to do this on my mac book i ge

  • PI731: FTP-FCC-Receiver with blank line at the beginning

    Hi, In my example I always get first a blank line but I do not want this blank line. I need the field FILENAME only as a variable name for the file name schema. Mapping: Communication Channel (Receiver / FTP / File Content Conversion) Result (with bl

  • "Dazzle" effect in slideshow DVD

    I've burned an iMovie slideshow onto a DVD using iDVD. When I run the slideshow on my TV, I get a very annoying "dazzle" effect that spoils the photos. The problem seems focussed on horizontal lines. Also, it is worse when I use the Ken Burns effect