How to check jpg image: Warning: JPEG library error (8 bit), "Premature end of JPEG file"."

39 views (last 30 days)
Hi,
I have a problem with loading jpg images. I'm running a C++ model and a Matlab program side by side (on Ubuntu).
Basically, the C++ model outputs jpg's and the Matlab program tries to read them as soon as they are available. Speed is important: I want to load them as soon as they are output by C++/
My code looks something like this:
while 1
listJPG = dir('folder/*.jpg');
if length(listJPG) ~= 0 && listJPG.bytes > 10
try
images = imread(listJPG(1).name)
break
end
end
pause(.001)
end
Most of the times it works fine, however, sometimes I get the following warning: Warning: JPEG library error (8 bit), "Premature end of JPEG file"."
My questions: 1. What does this error mean, is it not reading the entire image because it is not completely saved yet? 2. How do I prevent this from happening, without using a longer pause, as the code needs to be as fast as possible.
Thanks!

Answers (1)

Walter Roberson
Walter Roberson on 19 Jun 2012
None of the operating systems that MATLAB runs on support the ability to detect when a file is "ready for reading". With various degrees of difficulty and privilege you can detect whether a file is open by something right at the time you run the query, but as processes are entitled to open and close and reopen files, you cannot tell whether a file is really ready, completely finished with.
In MS Windows, sometimes if a file happens to be open for writing then an attempt to open the file for reading will be denied. This is, however, not a standardized behaviour, and the details of it conflict with POSIX.1 (which MS Windows XP SP2 and later to be conformant to) as it is a behaviour that is only defined when Mandatory Access Controls (MAC) are implemented, which MS Windows does not have.
There is a Trusted Linux that implements Mandatory Access Controls, but you are unlikely to be using it. It is a SuSE derivative; I think it might have been NASA Ames that was the project lead for it.
Absent Mandatory Access Controls, it is legal for a process to open a file that another process has open, so if something is writing the file, Linux and OS-X are not going to stop you from reading from it even though it is incomplete.
Linux has directory change notification operations, which are able to tell you when a new file has been created, or when a file has been changed, or some other things -- but not when a file is "ready". I believe OS-X also has those change notifications (though I am not sure they use the same API: might depend on the Linux train you are using.)
I would suggest that the easiest way to deal with this might be to change the process that creates the files. If it creates them somewhere else and moves (or renames) them into your directory when it is finished with them, then you do not need to worry about them being ready: they would not appear until they were. Likewise if the creating process were to create them under a different extension and rename them to .jpg when it is done with them, you would have the same advantage.
Don't forget to rename or delete the file out of the directory when you finish with it inside MATLAB or else you are going to keep seeing it as if it was new...
  1 Comment
Kerwin
Kerwin on 20 Jun 2012
Thank you for the detailed answer! I will go for the moving solution, as this should be trivial to implement.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!