Jump to content
dkprojektai

jpg validation

Recommended Posts

Hello, I have this code in thread:

 

Try
  MotionBitmap1.Canvas.Lock;
  jpeg:= TJPEGImage.Create;
  jpeg.Assign(MotionBitmap1);
  jpeg.SaveToFile('D:\picture.jpg');
finally
  MotionBitmap1.Canvas.Unlock;
  Try FreeAndNil(jpeg); except end;
end;

 

I'd like to find out is it saved correctly and the image is in correct format and etc. 

 

Is there any solution to validate file?

 

Share this post


Link to post

I'd want to fix the code first. There should be two try/finally blocks and no try/except block. 

 

MotionBitmap1.Canvas.Lock;

try

  jpeg := TJPEGImage.Create;

  try

    jpeg.Assign(MotionBitmap1);

    jpeg.SaveToFile('D:\picture.jpg');

  finally

    jpeg.Free;

  end;

finally 

  MotionBitmap1.Canvas.Unlock;

end;

 

And I don't know whether or not the lock/unlock is necessary. Why did you include it?

 

As for checking whether or not it worked, there's no point. What failure more do you have in mind? 

  • Like 1

Share this post


Link to post
Quote

And I don't know whether or not the lock/unlock is necessary. Why did you include it?

 

As for checking whether or not it worked, there's no point. What failure more do you have in mind? 

1. because it's in thread;

2. this saved picture later uses external dll library, and it sometimes make failure. That is why I thought - I need double check jpg. Unfortunately I did not found any solution on the net. 

Share this post


Link to post
3 hours ago, David Heffernan said:

And I don't know whether or not the lock/unlock is necessary. Why did you include it?

He said the code is running in a thread.  Locking a TBitmap's Canvas is required when working with a TBitmap in a thread, otherwise the main VCL thread can swoop in at any time and reclaim the GDI resources being used by the TBitmap behind its back.

 

Edited by Remy Lebeau

Share this post


Link to post
3 hours ago, dkprojektai said:

this saved picture later uses external dll library

I think you mean the external DLL uses your jpeg, right?

3 hours ago, dkprojektai said:

and it sometimes make failure

What failure exactly? You need to be more specific.

3 hours ago, dkprojektai said:

That is why I thought - I need double check jpg.

What makes you think TJPEGImage creates invalid JPG files?  Perhaps the real problem is an error in how the DLL uses the JPG file.

Share this post


Link to post
9 hours ago, Remy Lebeau said:

What makes you think TJPEGImage creates invalid JPG files?

My 2 cents are... not releasing write lock / not closing file, or improper flushing to disk. I personally never had any issues with TJPEGImage like this.

 

@dkprojektai If you really want to make sure, try reloading the saved file with the same, TJPEGImage object and catch any exceptions. As I stated above though, I would be surprised if there would be any. I think @Remy Lebeau is on the right track here and the DLL is not handling the file correctly.

At the end of the day it might be a special requirement in the header / pattern what we don't know about.

Share this post


Link to post
1 hour ago, dkprojektai said:

Software runs on many PC's without any problems. This issue I have only on 1 PC. It's Windows 7 32bit.

TJPEGImage is using external methods, like jpeg_CreateDecompress. I don't know if this is being supplied by the compiler or the OS the application is running on, but in this case I don't think it's an issue, as:

- if it would be an issue with Win7 x86, it would have affected an awful lot of applications, including browsers too and would have quickly been patched

- if the compiler is attaching these methods to the EXE, the application would fail everywhere.

 

Is the application failing with all Win7 x86 OSes, or just one particular PC? If only one PC, is it failing constantly, or just from time to time?

I'd look into the configuration of the machine. Ideas: AV locks the file to scan it before the DLL tries to access it; UAC blocking the placement of the file; worn hard drive; any other application hooking into yours / system calls, etc.

Share this post


Link to post
Guest
19 hours ago, dkprojektai said:

2. this saved picture later uses external dll library, and it sometimes make failure. That is why I thought - I need double check jpg. Unfortunately I did not found any solution on the net. 

I saw stuff like that many times when one or more clients reported unexplained failure/AV/errors (what so ever), %99 it was disk operation (save/load) caused by wrong permission or antivirus installed with modified settings from its default.

 

What do you mean by "make failure" ?

is it the image corrupt or that dll complaining and you took its word, as it is popular mistake, a software will complain of bad image file when the problem was couldn't open the file.

If there is antivirus with strange behavior/settings ( like upload samples to its labs) it will block the file just written or will be written until finishing its scan or upload, in that case the dll will not be able to access the file and MAY BE it will report bad image file instead can't open/read.

 

First : change your code to this to minimize the duration lock on bitmap and separate assigning from saving

  jpeg := TJPEGImage.Create;
  try

    MotionBitmap1.Canvas.Lock;
    try
      jpeg.Assign(MotionBitmap1);
    finally
      MotionBitmap1.Canvas.Unlock;
    end;

    jpeg.SaveToFile('D:\picture.jpg');
  finally
    jpeg.Free;
  end;

in case an antivirus holding the file hostage,

Second test that dll with

A) corrupt image file

B) with file been opened without read share permission to make sure the dll is reporting different and accurate failure, in each case.

 

Hope that help.

Share this post


Link to post
10 minutes ago, Kas Ob. said:

%99 it was disk operation (save/load) caused by wrong permission or antivirus installed [...]

I do not envy AV programmers. Most of us love to blame them for everything 😄

  • Like 1

Share this post


Link to post
Guest
11 minutes ago, aehimself said:

I do not envy AV programmers. Most of us love to blame them for everything 😄

The usual escape goat 🙂

Share this post


Link to post
Guest
40 minutes ago, dkprojektai said:

11.jpg - corrupted. Can someone check?

The forum doesn't show images names so which is 11.jpg ? and both for me looks fine, should those have same size ? 

 

if the problem is reproducible on one client then the easiest way to build special version and send him, let it save the bmp before compression and save the jpg after the compression in different folder then when that dll complain "make failure" rename the folder and create new one later you will have many cases to study.

But first check on you PC this :

Have you fed that dll a wrong file (like text file ) and in another test ( feed it the the client EXE itself ) and see how it does report that.

 

and you didn't answer the question : What does "make failure" mean ? and what do you get (error message box, crash , an entry in log file ....)?

 

,for all what i know corrupt jpg images mostly look like those here:

https://superuser.com/questions/276154/automating-the-scanning-of-graphics-files-for-corruption

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×