Save picture as monochrome BMP?

I have a need to save a picture as a monochrome bmp. I know how to SaveAsWindowsBMP, but this will not work as the packaging printer I am trying to create the BMP for will only accept a monochrome bmp. Any ideas or help with this would be great.

Thanks in advance!

Tom

You will have to convert the picture to greyscale yourself. Here’s a post on the old forums with a greyscaling method: http://forums.realsoftware.com/viewtopic.php?p=235221&sid=fd1b8a373e3f8c11aa379a9471d48f73#p235221

[code]Function ToBlackAndWhite(bmp as picture) As picture
For x as integer = 0 To bmp.Width
For y as integer = 0 To bmp.Height
bmp.RGBSurface.Pixel(x,y) = RGB((bmp.RGBSurface.Pixel(x, y).red + bmp.RGBSurface.Pixel(x, y).Green + bmp.RGBSurface.Pixel(x, y).Blue) / 3, _
(bmp.RGBSurface.Pixel(x, y).red + bmp.RGBSurface.Pixel(x, y).Green + bmp.RGBSurface.Pixel(x, y).Blue) / 3, _
(bmp.RGBSurface.Pixel(x, y).red + bmp.RGBSurface.Pixel(x, y).Green + bmp.RGBSurface.Pixel(x, y).Blue) / 3)
Next y
Next x

Return bmp
End Function
[/code]

Simply use the function as a picture as in :

Canvas2.Backdrop = ToBlackAndWhite(Canvas1.backdrop)

use the method suggested in the first topic… it creates a MUCH better grayscale the the DIV BY 3 method

by default the suggested method creates a 256 level gray scale… but you can AND the result with &HF0 to create a 16 level

Whatever the method used to convert the picture and the bit depth of it, the major hurdle will be saving the picture to file. Xojo saves BMP in RGB format only. So if the file format required is the uncompressed bitmap known as BW that was used by early versions of Windows, it will not be possible with simple Xojo code.

I recently had to use a specially made DDL using VB .NET to save in 256 indexed color BMP.
https://forum.xojo.com/15942-picture-saveaswindowsbmp-not-transparent

You may want to try and save a picture file with Xojo and see if the printer accepts that.

Some printers require 16 color (4 bit ) monochrome BMP. I looked in the Microsoft forums and this format seems impossible to generate with current versions of their software. This may require creating your own method for saving.

See http://en.wikipedia.org/wiki/BMP_file_format

Thank you all for the great replies. I will review them all and reply back with best method that works.

I have to agree with Michel here, I think the printer may actually want the BMP data to formatted in a very specific way. Thankfully the BMP format is not that complicated, but it may mean that you’ll need to manually generate a BMP file.

In regards to converting a picture to greyscale, my personal favorite method is CIE*Lab or a technique to replicate Infra-red images.

[quote=142049:@Michel Bujardet][code]Function ToBlackAndWhite(bmp as picture) As picture
For x as integer = 0 To bmp.Width
For y as integer = 0 To bmp.Height
bmp.RGBSurface.Pixel(x,y) = RGB((bmp.RGBSurface.Pixel(x, y).red + bmp.RGBSurface.Pixel(x, y).Green + bmp.RGBSurface.Pixel(x, y).Blue) / 3, _
(bmp.RGBSurface.Pixel(x, y).red + bmp.RGBSurface.Pixel(x, y).Green + bmp.RGBSurface.Pixel(x, y).Blue) / 3, _
(bmp.RGBSurface.Pixel(x, y).red + bmp.RGBSurface.Pixel(x, y).Green + bmp.RGBSurface.Pixel(x, y).Blue) / 3)
Next y
Next x

Return bmp
End Function
[/code]

Simply use the function as a picture as in :

Canvas2.Backdrop = ToBlackAndWhite(Canvas1.backdrop)

I believe you get a better result by weighting the R G and B percentages used for Grey

( See: http://www.libtiff.org/man/tiff2bw.1.html )

and, as the percentages in that note do not actually add up to 100%, I use:-

RED 30.0 %
GREEN 59.0 %
BLUE 11.0 %

[quote=142131:@Chris Carter]I believe you get a better result by weighting the R G and B percentages used for Grey

( See: http://www.libtiff.org/man/tiff2bw.1.html )

and, as the percentages in that note do not actually add up to 100%, I use:-

RED 30.0 %
GREEN 59.0 %
BLUE 11.0 %[/quote]

You are right. For a photographic usage, the human perception of colors must be taken into account so the human response curve to the different light wave length can be reproduced. Human perception is most sensitive to green. This is the reason behind the creation of black and white panchromatic films Panchromatic film - Wikipedia

Indeed it does not hurt.

The issue here is much less the perfectible quality of colors interpretation than the need to generate a file compatible with that particular printer.

An RGB BMP containing a combinations of color rendering as levels of grey is not the same thing as a true monochrome file format. Same thing not long ago when I needed BMP in 256 indexed colors that neither Xojo nor MBS could do.

Taken from Wikipedia :

As it stands, chances are unfortunately that the current RGB BMP file format used by Xojo will not do.

[quote=142141:@Michel Bujardet]
As it stands, chances are unfortunately that the current RGB BMP file format used by Xojo will not do.[/quote]

Is there any format that can save Picture to greyscale or bitmap file in Xojo?
I have been searching and reading, but this is still unclear. Are all Picture file formats 24bit (I can find no info on how using SaveAsGif, if it is even working…)?

I am saving a bunch of A4 page size monochrome images to disk (in 600dpi, roughly 4000x7000 pixels), for later manipulation. SaveAsPNG takes about 3 seconds to save for each page, jpeg slightly less and that is a bit to slow to be comfortable when writing many of them. BMP is the fastest I can make work, but as it saves in 24bit, the filesize is huge, about 100MB each, which I don’t like. As I only use 1bit data it is quite silly to waste time and space writing 24 bit files…

Do I have to try writing the monocrome data to the files myself?
(Uncompressed gif:s seems doable. Maybe. BMP, not so easy…)

I am on OS X. What to do?

[quote=160912:@Roger Jönsson]Is there any format that can save Picture to greyscale or bitmap file in Xojo?
I have been searching and reading, but this is still unclear. Are all Picture file formats 24bit (I can find no info on how using SaveAsGif, if it is even working…)?

I am saving a bunch of A4 page size monochrome images to disk (in 600dpi, roughly 4000x7000 pixels), for later manipulation. SaveAsPNG takes about 3 seconds to save for each page, jpeg slightly less and that is a bit to slow to be comfortable when writing many of them. BMP is the fastest I can make work, but as it saves in 24bit, the filesize is huge, about 100MB each, which I don’t like. As I only use 1bit data it is quite silly to waste time and space writing 24 bit files…

Do I have to try writing the monocrome data to the files myself?
(Uncompressed gif:s seems doable. Maybe. BMP, not so easy…)

I am on OS X. What to do?[/quote]

Xojo does not know how to save BMP in anything but 24 bit. Monochome and indexed color is not available.

The most compact, therefore probably fastest one, is GIF. But you will have to remove color information yourself. The method I suggested above is the simplest.

I use sips in AppleScript to make grayscale Pictures,

try
	set targetfolder to "/Volumes/Axel_1/GrayPics/" -- your folder here
	set theImage to (choose file) as text
	set AppleScript's text item delimiters to ":"
	set filename to last text item of theImage
	set AppleScript's text item delimiters to "."
	set extension to last text item of filename
	set filename to first text item of filename
	set theImage to quoted form of POSIX path of theImage
	set cmd to "/usr/bin/sips -m '/System/Library/ColorSync/Profiles/Generic Gray Profile.icc' " & " " & theImage & " -o '" & targetfolder & filename & "_grey." & extension & "'"
	do shell script cmd
on error errorMessage number errorNumber
	display dialog errorMessage
end try

But, if I understand it right, I have to write the file do disk first?
I cant read the original image that from memory without saving a BMP or other file format? -Then it would not save any time. Maybe writing BMP to a ramdisk could speed it up, though.

[quote=160929:@Roger Jönsson]But, if I understand it right, I have to write the file do disk first?
I cant read the original image that from memory without saving a BMP or other file format? -Then it would not save any time. Maybe writing BMP to a ramdisk could speed it up, though.[/quote]

If you use the method I posted to turn the picture to monochrome, and then use SaveAsGif, you save directly. Should be fairly fast. Won’t you just try ?
http://documentation.xojo.com/index.php/Picture.Save

Thanks,
-I will, tonight (after work).
Don’t I need to set pallet info for the gif or is Xojo automatically making a suitable pallet from the data passed?

This would also be a solution for me:
http://en.wikipedia.org/wiki/Netpbm_format
Portable BitMap 0–1 (black & white)
Portable GrayMap 0–255 (gray scale)

It seems compatible with with the software used for further manipulation and is a very simple format to write.

here is a version with multiple selection, try how fast it works.
I usually use the built-in OS X tools because the picture quality is better than in Xojo

try
	set targetfolder to "/Volumes/Axel_1/GrayPics/" -- your folder here
	set theImage to (choose file with multiple selections allowed)
	repeat with i from 1 to the count of theImage
		set this_item to item i of theImage
		set this_item to this_item as text
		set AppleScript's text item delimiters to ":"
		set filename to last text item of this_item
		set AppleScript's text item delimiters to "."
		set extension to last text item of filename
		set filename to first text item of filename
		set this_item to quoted form of POSIX path of this_item
		set cmd to "/usr/bin/sips -m '/System/Library/ColorSync/Profiles/Generic Gray Profile.icc' " & " " & this_item & " -o '" & targetfolder & filename & "_grey." & extension & "'"
		do shell script cmd
	end repeat
on error errorMessage number errorNumber
	display dialog errorMessage
end try

[quote=160934:@Roger Jönsson]Thanks,
-I will, tonight (after work).
Don’t I need to set pallet info for the gif or is Xojo automatically making a suitable pallet from the data passed?

This would also be a solution for me:
http://en.wikipedia.org/wiki/Netpbm_format
Portable BitMap 0–1 (black & white)
Portable GrayMap 0–255 (gray scale)

It seems compatible with with the software used for further manipulation and is a very simple format to write.[/quote]

In monochrome, I do not think you need a palette. Only in indexed colors, I think. As default, Xojo always works in RGB.

Okay, the good news.

You’re on OS X, that’s always a good start :wink: Secondly, Xojo uses CGImages (internally) and these are compatible with CGImageDestination, which will allow you to create images in many formats, with all kinds of goodness.

Now the not so good news.
If you’re not familiar with it, it can be difficult to understand and get it to work correctly, especially if you want to save Greyscale images. MBS & MacOSLib have the functions available for you.

I feel guilty launching you into the deep end, but I don’t have the time to write a full blown tutorial on how to at the moment. Hopefully a nudge in the right direction will help.

:slight_smile: If I can handle what you suggest I wont know until I tried.
I will try the other alternatives first and see where it takes me.