Color conversation & JPG

Just starting out? Need help? Post your questions and find answers here.
Garfield9992003
User
User
Posts: 15
Joined: Tue Apr 26, 2005 8:13 am

Color conversation & JPG

Post by Garfield9992003 »

Hi.

I´ve got 2 problems:

1.) How can i convert a 24-bit color bitmap (i´ve got it in a buffer and in a file) to 8-bit grayscale and 1-bit black/white image? Is there a fast solution? (perhaps assembler?)
I dont want to convert an 24-bit color image to a 24bit grayscale image :)

2.) How can I save these image (8-bit and 1-bit) to JPG or compressed BMP? Saveimage save them always as 24 bit uncompressed image. Is there a workaround?

Does someone have a hint, a DLL or some code?

Regards

Frank
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Post by Blade »

Jpeg algorithm is smart enough to skip missing data, such as color from a BW image, so saving a BW image in 24 bit is ALMOST the same as saving it in 8 bit.
For the same reason I've never seen in the jpeg specs about saving just BW images...

So if jpeg quality is good for you, use it without worry. :)

The best option would be using PNG at 8 and 1 bit, but our PNG implementation is missing this ability (and many other too, i.e. alpha channel)


@Fred:
please please please please please please please ;)
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

look at http://www.smalleranimals.com/isource.htm

reasonable cheap

/ZuedTheSwede
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

50 bucks? what you've been smoking today?
look at sourceforge for DEVIL (or OpenIL - old name).
Image manipulation lib!.

I have ported it to PB (im telling this so you know it works ok in PB of course). But source is private :lol: sorry.
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

50 bucks? what you've been smoking today?
... same old pot?


Yes dagcrack is right, you could use devIL..But imgsource is a commercial alternative.. ( smoking or not!)

btw - devIL doesnt support 1-bit images, -imgsource does..


ZuedTheSwede
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

yay your quote got mixed up.
Yes but smoking or not who needs 1 bits images and for what exactly?, theres one reason of why developers doesnt support "something"... might be because that "something" is something that no one exept by a weird joe will use!.

So why adding more and more stuff no one could ever use...
:? Anyway DevIL is opensourced (although the license states that you'll need to release the edited source to the public, for free of course) so you could write the exporter you need - but in that case write it in PB heh..

Anyway if DevIL cant do what you want, theres still other freeware libs out there.. why paying when you could have it with out any charge? (maybe just add the author/s on the credits.. that'd be enough on most cases).
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

yay your quote got mixed up.
.. I blame my smoking habbits
Yes but smoking or not who needs 1 bits images and for what exactly?
That you have to ask Garfield9992003 about.(old fax software maybe?)
theres one reason of why developers doesnt support "something"... might be because that "something" is something that no one exept by a weird joe will use!.
Of course.. :D if "something" never is asked for, then "something" will never appear.. does it../look at how purebasic is developing.. new "somethings" in almost every new release.. wierd or not!

I hope that mr.dagcrack is more happy with my qouting this time 8)

ZuedTheSwede
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Ah the quoting quote was just a quoting joke! :? well that explaination will make sense on the given time.

For now.. FAXES?! that reminds me our NIC from here.. they request you a FAX to change your "person" and "entity" email... ehh lame no?, because AFAIK faxes has no legal validity.. they are not even valid to clean your... nose!.

:P
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

lol dagrack.. I give up....surrenders.. whatever..
Please enlight me more, as I come from a planet far far away ;)

We will meet again.. in another time and thread.. may the force be with you as I suspect that evil faxmachines will try to rise against you after this: and I quote again.
because AFAIK faxes has no legal validity.. they are not even valid to clean your... nose!.
:roll:

/zuedTheSwede
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

:? Well an email can be much more secure than a fax and lets not talk about confyness! and price! :p

Okey "let the force be with you"? latest starwars chapter yay!
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

I know this isn't exactly what you want, and it is a little slow, but I converted some code I found on the net. It has procedures to convert color to grayscale/black and white. Maybe someone can optimize and improve it. :)

Code: Select all

UsePNGImageDecoder() 
UseTIFFImageDecoder() 
UseJPEGImageDecoder() 
UseJPEGImageEncoder() 
Procedure ConvertColorToGray(cImage$) 
  LoadImage(0, cImage$) 
  StartDrawing(ImageOutput()) 
  Dim newColor(ImageWidth(), ImageHeight()) 
  For x = 0 To ImageWidth() - 1 
    For y = 0 To ImageHeight() - 1 
      PixelColor = Point(x, y) 
      Red  = PixelColor &$FF 
      Green = PixelColor >>8 &$FF 
      Blue = PixelColor >>16 &$FF 
      Gray = Round(0.299 * Red + 0.587 * Green +  0.114 * Blue, 0) 
      newColor(x, y) = RGB(Gray, Gray, Gray) 
    Next y 
  Next x 
  StopDrawing() 
  CreateImage(1, ImageWidth(), ImageHeight()) 
  StartDrawing(ImageOutput()) 
  For x = 0 To ImageWidth() - 1 
    For y = 0 To ImageHeight() - 1 
      Locate(x, y) 
      Plot(x, y, newColor(x, y)) 
    Next y 
  Next x 
  StopDrawing() 
  SetGadgetAttribute(1, #PB_ScrollArea_InnerWidth, ImageWidth()) 
  SetGadgetAttribute(1, #PB_ScrollArea_InnerHeight, ImageHeight()) 
  SetGadgetState(2, UseImage(1)) 
  SaveImage(1, "c:\image_gray.jpg", #PB_ImagePlugin_JPEG) 
  ProcedureReturn 
EndProcedure 

Procedure ConvertColorToBW(cImage$) 
  LoadImage(0, cImage$) 
  StartDrawing(ImageOutput()) 
  Dim newColor(ImageWidth(), ImageHeight()) 
  For x = 0 To ImageWidth() - 1 
    For y = 0 To ImageHeight() - 1 
      PixelColor = Point(x, y) 
      Red  = PixelColor &$FF 
      Green = PixelColor >>8 &$FF 
      Blue = PixelColor >>16 &$FF 
      Gray = Round(0.299 * Red + 0.587 * Green +  0.114 * Blue, 0) 
      If Gray < 128 
        newColor(x, y) = RGB(0, 0, 0) 
      Else 
        newColor(x, y) = RGB(255, 255, 255) 
      EndIf 
    Next y 
  Next x 
  StopDrawing() 
  CreateImage(1, ImageWidth(), ImageHeight()) 
  StartDrawing(ImageOutput()) 
  For x = 0 To ImageWidth() - 1 
    For y = 0 To ImageHeight() - 1 
      Locate(x, y) 
      Plot(x, y, newColor(x, y)) 
    Next y 
  Next x 
  StopDrawing() 
  SetGadgetAttribute(1, #PB_ScrollArea_InnerWidth, ImageWidth()) 
  SetGadgetAttribute(1, #PB_ScrollArea_InnerHeight, ImageHeight()) 
  SetGadgetState(2, UseImage(1)) 
  SaveImage(1, "c:\image_bw.jpg", #PB_ImagePlugin_JPEG) 
  ProcedureReturn 
EndProcedure 

quit = #False 
If OpenWindow(0, 0, 0, 700, 500, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Grayscale") And CreateGadgetList(WindowID(0)) 
  CreateMenu(0, WindowID()) 
  MenuTitle("Image") 
  MenuItem(0, "Image to Grayscale...") 
  MenuItem(1, "Image to B/W...") 
  MenuBar() 
  MenuItem(3, "Exit") 
  ScrollAreaGadget(1, 0, 0, 700, 475, 700, 475, 10) 
  ImageGadget(2, 0, 0, 100, 100, 0) 
  CloseGadgetList() 
  Repeat 
    event = WaitWindowEvent() 
    Select event 
      Case #PB_EventMenu 
        Select EventMenuID() 
          Case 0 
            openFile$ = (OpenFileRequester("Select image", "c:\", "Images |*.tif;*.tiff;*.bmp;*png;*jpg;*jpeg", 0)) 
            If openFile$ 
              ConvertColorToGray(openFile$) 
            EndIf 
          Case 1 
            openFile$ = (OpenFileRequester("Select image", "c:\", "Images |*.tif;*.tiff;*.bmp;*png;*jpg;*jpeg", 0)) 
            If openFile$ 
              ConvertColorToBW(openFile$) 
            EndIf 
          Case 3 
            quit = #True 
        EndSelect 
      Case #PB_EventCloseWindow 
        quit = #True 
    EndSelect 
  Until quit 
EndIf 
End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Garfield9992003
User
User
Posts: 15
Joined: Tue Apr 26, 2005 8:13 am

Post by Garfield9992003 »

Thanks @ all

I´ll take a look at the imgSource-dll. It sounds good. It´s relly important that i could save the images in *small* files.

@Sparkie - i´ve converted the images from color to bw/gray with PokeB and PeekB. (First allocate memorysize) Thats fast. I could put code here, if u want.

Regards

Frank
Post Reply