View Single Post
 
Old 09-13-2019, 02:15 AM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 217
Exclamation IsValidJPG no longer working

Hi all I have been using the following function to detect 'bad' jpg/jpeg images but it has suddenly stopped working, it is in a library that rarely gets altered. the error is the red line which states 'the left hand side can not be assigned to' like I said the code has not altered, can anybody help or have a better solution for detecting good/bad jpg's

Code:
function IsValidJPG(FileName: String): Boolean;
var
    H, W: Integer;
    Jpg: TJPEGImage;
    Bmp: TBitmap;
    F: File of Word;
    Head: Word;
    p: pInteger;
    pB: pByte;
    pE: Cardinal;
    pColor: Integer;
    Label fin;
begin
    Result := FALSE;

    if (FileExists(FileName))
    then begin
      AssignFile(F, FileName);
      try
        Reset(F);
        Read(F, Head);
      finally
        CloseFile(F);
      end;

      if (Head <> $D8FF) then exit;

      try
        Jpg := TJpegImage.Create;
        Bmp := TBitmap.Create;

        // The First check, does it load?
        jpg.LoadFromFile(FileName);

        // Yes, now draw it out to a BMP for manipulation
        H := Jpg.Height;
        W := Jpg.Width;
        Bmp.Height := H;
        Bmp.Width :=  W;
        Bmp.Canvas.Draw(0, 0, Jpg);

        // Grab the First line of the Image, in binary
        // if the color is invalid, the Image is bad.
        p := Bmp.ScanLine [H - 1];
        pColor := p^;
        if (pColor = $808080)
        then begin
          FreeAndNil(Jpg);
          FreeAndNil(Bmp);
          Result := FALSE;
          exit;
        end;

        // Now jump to the last scanline
        // Check every four dwords of last scanline for a suspect blue Value
        // It goes backwards through the last line of the image and checks to
        // see if the color is less than the constant $E1.
        // If it is, then result is changed from FALSE to TRUE and the routine
        // exits with IsValidJPEG returning TRUE. Otherwise, if all the pixels
        // in that line have a color greater than or equal too $E1 (suspect blue???),
        // then the result remains FALSE and IsValidJPEG returns false...
        pE := Cardinal(p) + Cardinal(H shr 1);
        Cardinal(pB) := Cardinal(p);
        while (Cardinal(pB) < pE)
        do begin
          if (pB^ < $E1)
          then begin
            Result := TRUE;
            Break;
          end
          else begin
             inc(pB);
             if (pB^ >=  $E1)
             then  begin
               inc(pB);
               if (pB^ >=  $E1)
               then begin
                 Result := TRUE;
                 Break;
               end;
               dec(pB);
             end;
             dec(pB);
          end;
          inc(pB, 16);
        end;
      finally
        FreeAndNil(Jpg);
        FreeAndNil(Bmp);
      end;
    end;
end;

Last edited by Zaaka; 09-15-2019 at 07:08 PM.
Reply With Quote