Recent

Author Topic: [SOLVED] Storing .bmp or .jpg file image, to database, use same code?  (Read 29125 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
Hi everyone,

Please check me if so:

When reading a Blob field with JPEG or bmp image, we have to distinguish if data is JPG or bmp.

However when stream/storing the image from a file, to the database table, we use the same code for bmp or JPG?

Thanks for helping me be clear on this.
« Last Edit: July 21, 2012, 04:18:09 pm by Elmug »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Storing .bmp or .jpg file image, to database, use same code?
« Reply #1 on: July 20, 2012, 01:31:35 pm »
When reading a Blob field with JPEG or bmp image, we have to distinguish if data is JPG or bmp.

No.  You can read the binary contents too. Only for _displaying_ (interpreting) it, you need to know the type.

Quote
However when stream/storing the image from a file, to the database table, we use the same code for bmp or JPG?

Because in this case you are only moving bytes, not looking at what they mean

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Storing .bmp or .jpg file image, to database, use same code?
« Reply #2 on: July 20, 2012, 09:34:27 pm »
Thanks, marcov.

I was asking specifically in applications for images, and actually JPG and bitmap type.

Do we use the same format or code to store a jpeg image as a bitmap image?

I am asking because I have seen various ways in the web, and I still have not managed with JPEG, although I have finally managed with the bitmap type.

Thanks for helping.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Storing .bmp or .jpg file image, to database, use same code?
« Reply #3 on: July 21, 2012, 12:15:54 am »
All images by the end (displayed) would be the same format, but in a storage, each image type has its own way (which is why the size of the same image differs from one format to another).

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Storing .bmp or .jpg file image, to database, use same code?
« Reply #4 on: July 21, 2012, 10:26:47 am »
Saving data to and reading data from BLOB - a blob of binary data - is the programmer's own responsibility. You can do it however you want.

If you store both bmp and jpg files, you can just store the bytes of the image. If you read a blob, you don't know if you're reading bmp or jpg. You could:
1. Store just the bytes of the image in the BLOB. Try to detect the difference between the bmp and jpg file format and act (e.g. look for magic numbers/specific file formats).
2. Store just the bytes of the image in the BLOB. Use another field in the database to store what kind of data is stored in the BLOB
3. Add one or more bytes before the actual image data that indicate what kind of data is stored in the BLOB.

I'd say option 2 seems to be the cleanest and allows the most flexibility.
Apparently the Lazarus DBImage control uses 3, and the dbimage control in Delphi uses option 1, if I understand correctly.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Storing .bmp or .jpg file image, to database, use same code?
« Reply #5 on: July 21, 2012, 10:31:31 am »
2. Store just the bytes of the image in the BLOB. Use another field in the database to store what kind of data is stored in the BLOB
Thumbs up   :D
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Storing .bmp or .jpg file image, to database, use same code?
« Reply #6 on: July 21, 2012, 11:08:18 am »
All images by the end (displayed) would be the same format, but in a storage, each image type has its own way (which is why the size of the same image differs from one format to another).

Not always btw. Some image formats default to lossy compression (like jpg).  Too many image conversions that involve lossy compression might significantly decrease image quality.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Storing .bmp or .jpg file image, to database, use same code?
« Reply #7 on: July 21, 2012, 11:37:03 am »
Not always btw. Some image formats default to lossy compression (like jpg).  Too many image conversions that involve lossy compression might significantly decrease image quality.
This is a good point actually that make the question about the mechanism of storing a BMP or JPG (or any other image format) irrelevant.
I built a small photo mgmt application that stores images in a SQLite db:
- I first load the images in memory into a memory stream;
- for displaying purposes they are converted to JPG;
- and when storing it into the DB I use that original memory stream. No loss in quality.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Storing .bmp or .jpg file image, to database, use same code?
« Reply #8 on: July 21, 2012, 01:41:21 pm »
Quote
Not always btw. Some image formats default to lossy compression (like jpg).  Too many image conversions that involve lossy compression might significantly decrease image quality.
By "same format" I mean the format is array of RGB(A) bytes or similar, not necessarily the "same contents" in the array ;)

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: Storing .bmp or .jpg file image, to database, use same code?
« Reply #9 on: July 21, 2012, 03:17:06 pm »
I thought that the standard method of identifying binary file type was to read the magic number/signature. Systems used to rely on files extensions which for a database would mean another field. Don't think file extensions have been relied on a long time though.

What is the difference between the standard magic number and what Lazarus is doing by adding the extra bytes to the front of the file? Why is Lazarus not using the standard file signature method?

http://en.wikipedia.org/wiki/List_of_file_signatures

Elmug

  • Hero Member
  • *****
  • Posts: 849
Hi everyone,

I have solved the problem and my application CAN now handle either JPEG or BitMap Blobs in the same column, without having to use a column storing the image type, and without having to decipher the image type from the Blob data.

The solution is as follows, using Except coding:
Code: [Select]
// var
//  BlobField: TField;
//  BS: TStream;

  with SQLQuery1 do
    begin
       BlobField := FieldByName('Pic'); {'Pic' is name of column with photo}
       BS := CreateBlobStream(BlobField,bmRead);
       Image1.Picture.Graphic:= TJpegImage.Create; {assume is Jpeg}
    Try
       Image1.Picture.Graphic.LoadFromStream(BS); {error if not Jpeg}
      Except {repeat steps for BitMap}
         BS.Free;
         Image1.Picture.Graphic:= nil; {empty}
         BlobField := FieldByName('Pic'); {'Pic' is name of column with photo}
         BS := CreateBlobStream(BlobField,bmRead);
         Image1.Picture.Graphic:= TBitMap.Create; {bitmap}
         Image1.Picture.Graphic.LoadFromStream(BS);
      end; {Try}
      BS.Free;
    end; {with SQLQuery}


Thanks everyone for helping out!
« Last Edit: July 21, 2012, 04:36:51 pm by Elmug »

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: [SOLVED] Storing .bmp or .jpg file image, to database, use same code?
« Reply #11 on: July 21, 2012, 07:07:59 pm »
I posted something a little while back about storing images in PostgreSQL bytea fields. Never had to worry about the file image type. Used TImage controls and afterScroll events.

http://www.lazarus.freepascal.org/index.php/topic,16205.0.html

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: [SOLVED] Storing .bmp or .jpg file image, to database, use same code?
« Reply #12 on: July 21, 2012, 08:50:51 pm »
Quote
Never had to worry about the file image type

Yes, just checking TPicture.LoadFromStream I believe it does the magic number/signature checking.  That's handy, I don't think Delphi used to do it (maybe newer version do, I don't have Delphi at home so can't check).

So the trick is, use TPicture.LoadFromStream & not TGraphic.LoadFromStream.. :)

 

TinyPortal © 2005-2018