Solved

Bitmap to Jpeg back to Bitmap not equal

Posted on 1999-01-05
17
500 Views
Last Modified: 2013-12-03
I am using Win98 and D4. In an attempt to conserve K space, I have converted my Bitmaps which are simple two color (gray and white) pictures to Jpegs. Later on a change of Jpegs back to Bitmaps the bitmap pictures appear to be the same but in fact they are not. As a test of accuracy I  filled the original bitmap with a specific color. This produced an accurate fill.Later I did the same after I converted the jpeg to a bitmap. This revealed that the converted jpeg (now the bitmap) is,in fact, made up of many colors. All this is not easily seen except during an enlargement. Is there a programming sequence that will either maintain the accuracy when the original bitmap is converted into the Jpeg or later restore that accuracy when the Jpeg is converted back into a bitmap?

Delphi3    
0
Comment
Question by:delphi3
  • 7
  • 5
  • 3
  • +2
17 Comments
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
The Jpeg format was not implemented to compress bitmaps without any information loss. You'll always lose some informations if you convert a bitmap to a jpeg to a bitmap. That's no bug, it's thought this way to reduce the images to the smallest possible size.
If you want to compress your images without information loss, you should use another graphics format (but don't ask me which format). Or you should use the zip format.

Regards, Madshi.
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
JPEG is a lossy conpression format, that is information is lost (more specifically it is thrown away) during the compression. The reason behind this is that in most cases the information thrown away makes no difference TO THE EYE. A diff program obviously does notice the difference. The amount of information that is thrown away depends on the quality setting you choose. JPEG does have a 100% quality setting which is supposed to be lossless, but you would need to try it out.

Furthermore JPEG was designed to be good at compressing real world images (ie those with millions of colours). It is not very good at compressing palette images (those with highly quantised color sets) and tends to introduce unacceptable numbers of artifacts into the image (as you noticed :-) )

For bitmaps with two colors JPEG would be a poor choice. An excellent choice would be PKZIP, of TIFF images using either LZW or RLE compession codecs.

Cheers,

Raymond.

0
 
LVL 4

Author Comment

by:delphi3
Comment Utility
Raymond,
Please explain the various items that you recommend in the last line of your answer.
Cost? Down load from where? Can it be combined with Delphi4 where it could be incorporated with the program as it runs. I do have WinZip and it can compress the bitmaps with success, but not during a run.  And are any of those the same as it? Have you used any of them?

Thanks
Delphi3
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 50 total points
Comment Utility
There are a bunch of PKZIP libraries on DSP that are free. Have a browse through them and take your pick. These allow you to create archives on the fly and add files to them and extract files from them at run time.

There is a JPEG library (I assume this is the one you are using) on the D4 CD. Inspecting that should find the control for the quality setting.

As for my last line:

For bitmaps with two colors JPEG would be a poor choice. An excellent choice would be PKZIP, of TIFF images using either LZW
       or RLE compession codecs.

JPEG - Joint Photographic Expert Group
PKZIP - A compressor from PKWare. The format is now a defacto standard and implemented in many different libraries.
TIFF - Tagged Image File Format - a format for storing pictures. It includes a nhumber of different ways of compressing the data.
LZW - Lempel (sp?), Zif & Welch(?) compression algorithm
RLE - Run Length Encoding - a compression algorithm
codec - short for coder/decoder - eg a program that compresses and decompresses data.

I hope this helps,

Cheers,

Raymond.
0
 

Expert Comment

by:ptiemann2
Comment Utility
I don't understand why you rejected rwilson's answer.
He explained perfectly well..

My suggestion is simply GIF. It does a good job.
Is your image rather a photo that uses only black and white or is it an screenshot-like thing or a CAD-result which would have large areas of the same color? In the second case RLE (run-length-encoded) would do a good job for you.
TIFF has a RLE option.

By the way.. instead of GIF you should use PNG.. the "new" standard - 5 years old now.

0
 
LVL 4

Author Comment

by:delphi3
Comment Utility
Raymond,
Good job.

And  Ptiemann2,
Thank you too!

Cheers,
Delphi3

PS  Ptiemann2, You asked why I rejected his answer.....
I now have the fabric instead of just a  thread...... thank you all !!
0
 
LVL 2

Expert Comment

by:gysbert1
Comment Utility
If you need the "Fabric" you can leave a comment on the question without giving any grade, giving the expert another chance to explain better and then grading the answer.

Rejections are noted on the experts record and puts him in a bad light although the actual problem was that the person who asked the question did not fully understand the answer.

Next time just leave a comment and see what happens ...

Gysbert
0
 
LVL 4

Author Comment

by:delphi3
Comment Utility
Gyspert,
 Your comment is well taken, I'll remember that next time, about Raymond being cast in a bad light  because of a  rejected answer.

you wrote:
If you need the "Fabric" you can leave a comment on the question without giving any
     grade, giving the expert another chance to explain better and then grading the answer.

My reply:

 But I can't recall seeing the option to 'comment' before  the options to accept or reject  his proposed  answer.  At that point in time   in the attempt for a solution I did NOT understand his answer and did not know if he would further explain.  Once paid with points the deal is completed and we all go on our merry way... true? He has no further obligations to me or my question or to submit a further comment after I accept his answer.

In the future, for me. if the option to comment  is there before  accepting/ rejecting  a proposed answer, I'll use it in when I need more information. That would be fair to all, including you.

Thanks
Delphi3

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:gysbert1
Comment Utility
The option is there, although not obvious enough according to me.

You normally have the option to grade AND leave a comment (Type the comment in the window at the bottom). If you do not select any grade (not a grade and not reject i.e. nothing is checked) ,type a comment and then submit a comment will be posted without affecting the status of the question.

I learned this the hard way myself ...

Have a look next time and try it.

PS. It is better to reject as you did than to accept an answer which is unsatisfactory.

0
 
LVL 4

Author Comment

by:delphi3
Comment Utility
Gysbert1,

Thanks for your help, an experience  IS  the best teacher.  Now I include myself as having been  taught by the experienced, at least you were kind enough to share .......my grade to you,  for your kindness  is an  'A'. Sorry no points.

Delphi3

PS since you are on the line, I was thinking that I might convert all the pixel color  := char #  to a read, write file of char, since there are  14 pictures, all the same size, all having  the same 2 colors.  Using char would?? maybe??  reduce the size of the stored file. Or a  similar scheme for simple compression.  My motto "Never give up, beat it to death'  with a 'try this'  until something happens for the good.  And a  further reason for using something else, I do not have any code connect the programs that Raymond recommended to mine.....  

D3
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
Delphi3 - Even after grading and accepting an answer it is common for the thread to continue, usually about some nitty-gritty detail or other related topic. I do not turn off email notification after my answer is accepted for this purpose.

Did you have a look on DSP for the PKZip libraries?

Converting the colour to char #'s would probably expand your images by factor of 8 or so as the bitmap is most likely a 2 colour palette image where each pixel is represented by one bit (and if they aren't they should be then they'll be nice and small :-)

Cheers,

Raymond.
0
 
LVL 4

Author Comment

by:delphi3
Comment Utility
An investigative report for those interested. My investigations into using a program  to translate the bitmap into char  has reduced the number of K by  2/3. That is, what was 321K bitmap is now a 107K data file.txt.  And the translation of the datafile.txt produces an exact reproduction of the original bitmap, all bits and colors in place.

 Yes,  I had to use 'write' char instead of 'writeln'char when building the textfile. Using writeln produced a textfile with the same #K as the bitmap. In the  translator of the textfile  the use of 'read' char instead of  'readln' char.

Yes Raymond, I still have to take a look at  your suggestion to use DSP for the PKZip libraries. I have to discover a site to download it in a no cost version and then how to connect it to my program.

Delphi3
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
delphi3: I think I know what your problem is: Your bitmap is a 24 bit BMP, isn't it? If so load it into Paint and save it back again as a 16 colour BMP - you should be pleasantly surprised at the size of the resulting BMP. You could do a 2 colour one but I'm not sure if Paint handles this though Paint Shop Pro and their ilk probably should.

Although PKZip would do a better job again...

Raymond.
0
 
LVL 4

Author Comment

by:delphi3
Comment Utility
Hey Raymond,
 Wow! You made a discovery. Yes, I was in 24 color bmp and a move to 16 color bmp reduces the load from the 321K to 54K. A 2 color does not seem to be an option with Paint and a possible move to monochrome to reduce the K even further  seems to need  an adjustment that I have not  thought through. (going to save the .bmp as  a .gif made it even smaller.... 2K  but I don't know if Delphi has a runtime conversion tool to return it to a bitmap. The conversion of the gif to bmp is precise.

This discovery now makes my situation more manageable. Super job. And A BIG THANKS  for staying with me.

Delphi3  
0
 
LVL 2

Expert Comment

by:gysbert1
Comment Utility
There are two different bitmap formats available.
  1) RGB Bitmaps
  2) RLE Bitmaps (256 colors or less)

RLE (Run Length Encoded) bitmaps work best on non-photographic images. Data is compressed by encoding the run length of each color. When say 20 white pixels in a row occur it will save 20*16 (assuming pallette entry 16 is white) in stead of 16,16,16,16, .... saving a lot of storage. If this condition never occurs it does not help of course.

As far as I can remember Delphi can open either type. (?)

Using RLE should reduce your storage even more.
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
Glad I could help!

Raymond.
0
 
LVL 4

Author Comment

by:delphi3
Comment Utility
Raymond,
Since my experimentation with  xx.gif got a further reduction in size than anything else I went looking through other solutions in Experts-Exchange for an answer to what program could be incorporated into D4 that would do the job of converting xx.gif to xx.bmp. and xx.bmp to xx.gif. during runtime

BlackMan's answer  recommended to that person's question (not mine) to go to the site:

 http://www.melander.dk/delphi/gifimage/

and now together with the 16 colors you recommended the 54K xx.bmp changed to a xx.gif,  the net K used is a 1K.  Unbelievable!!

Thanks again to you all.
Delphi3
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now