Author Topic: Save Black-White BMP file [2 color BMP]  (Read 4596 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Save Black-White BMP file [2 color BMP]
« on: November 29, 2021, 11:23:36 am »
Code: QB64: [Select]
  1. 'inspired with SMCNeill Christmas theme program (this is not Christmas theme program)
  2. 'my goal was to enable the storage of two-color bitmaps. This only succeeded after two days,
  3. 'because my sources contained a serious error, after comparing with other sources, I found it.
  4. 'For this to work, the X-axis (width) image resolution must always be divisible by 32.
  5. '
  6.  
  7. image$ = "The Season to Color.JPG" '                      image name for convert to 2 color bmp file
  8. imageO = _LoadImage(image$, 32)
  9.  
  10. Type BW '                                 File Header + Info Header + 2 * _UNSIGNED LONG mask record all in one
  11.     'Header
  12.     signature As String * 2 '                             BM
  13.     FileSize As Long '                                    Size of file
  14.     Res1 As Integer '                                     not used [res = reserved]
  15.     Res2 As Integer '                                     not used [res = reserved]
  16.     DataOffset As Long '                                  62 always in this bitmap type
  17.     'infoHeader                                           -----------------------------
  18.     SizeOfInfoHeader As Long '                            always 40
  19.     Width As Long '                                       image width
  20.     Height As Long '                                      image height
  21.     Planes As Integer '                                   always 1
  22.     BitsPerPixel As Integer '                             1 bit for this use
  23.     Compression As Long '                                 0 = none
  24.     ImageSize As Long '                                   0, this is size for COMPRESSED IMAGE, 0 for uncompressed
  25.     XPixels As Long '                                     pixels/meter X [0]
  26.     YPixels As Long '                                     pixels/meter Y [0]
  27.     ColorsUsed As Long '                                  0 = used all colors in mask
  28.     ImportantColors As Long '                             0 = all
  29.     'color table                                          2 * _unsigned long record ---> 2 colors image
  30.     ColorA As _Unsigned Long
  31.     ColorB As _Unsigned Long
  32.  
  33. Dim BMP1 As BW '
  34. Dim As Long Size, W, H
  35. Wo = _Width(imageO): H = _Height(imageO)
  36.  
  37. Do Until Wo Mod 32 = 0 '                                    resize image so, that width is dividible by 32 [is condition for this image format]
  38.     Wo = Wo + 1
  39. W = Wo
  40.  
  41. image = _NewImage(Wo, H, 32)
  42. clr~& = &HFFFFFFFF '_BackgroundColor(imageO)              for christmas theme - all images using white background, for own use set it as you need
  43. _Dest image
  44. Cls , clr~&
  45.  
  46. _PutImage (0, 0), imageO, image
  47. _FreeImage imageO
  48.  
  49. Size = _Ceil(W * H / 8) + 62 '                           calculate file size (62 bytes is header + infoheader + 2 colors mask)
  50.  
  51. BMP1.signature = "BM"
  52. BMP1.FileSize = Size
  53. 'BMP1.Unused = 0
  54. BMP1.DataOffset = 62
  55. BMP1.SizeOfInfoHeader = 40
  56. BMP1.Width = W
  57. BMP1.Height = H
  58. BMP1.Planes = 0
  59. BMP1.BitsPerPixel = 1
  60. BMP1.Compression = 0
  61. BMP1.ImageSize = Size - 62
  62. BMP1.XPixels = 0
  63. BMP1.YPixels = 0
  64. BMP1.ColorsUsed = 0
  65. BMP1.ImportantColors = 0
  66. BMP1.ColorA = &HFF000000
  67. BMP1.ColorB = &HFFFFFFFF
  68.  
  69. i = 0
  70. S$ = ""
  71.  
  72. Dim outpt(Size - 63 + corr) As _Unsigned _Byte '      array for binaries [0 or 1] if image use color mask 0 or color mask 1
  73. For fill = 0 To UBound(outpt)
  74.     outpt(fill) = 0 '                                 first set all binaries as zero
  75.  
  76. _Source image '                                       This image format write image informations to file from bottom to top
  77. For y = H - 1 To 0 Step -1
  78.     For x = 0 To W - 1
  79.         Bp~& = Point(x, y)
  80.         r = _Red32(Bp~&)
  81.         g = _Green32(Bp~&)
  82.         b = _Blue32(Bp~&)
  83.         If r > 200 Or g > 200 Or b > 200 Then S$ = S$ + "1" Else S$ = S$ + "0" ' Steve's whiting routine....
  84.         If Len(S$) = 8 Then '                                                    You have 8 points writed as 0/1? Create byte from this 8 bites!
  85.             outpt(i) = BINtoDEC(S$) '                                            Write this byte to array outpt
  86.             S$ = ""
  87.             i = i + 1
  88.         End If
  89.     Next
  90.  
  91. If Len(S$) Then
  92.     outpt(i) = BINtoDEC(S$) '                                                   if here are some binaries, create byte from it and write it to array outpt
  93.     S$ = ""
  94.  
  95. If _FileExists("Black-White.bmp") Then Kill "Black-White.bmp"
  96.  
  97. Open "Black-White.bmp" For Binary As #1 '                                       write file, done.
  98. Put #1, 1, BMP1
  99. Put #1, 63, outpt()
  100. ReDim outpt(0)
  101.  
  102. Function BINtoDEC (b As String)
  103.     For Si = 1 To Len(b)
  104.         e$ = Mid$(b$, Si, 1)
  105.         c = Val(e$) '
  106.         Sj = Len(b) - Si
  107.         sBINtoDEC = sBINtoDEC + (c * 2 ^ Sj)
  108.         BINtoDEC = sBINtoDEC
  109.     Next Si
  110.     sBINtoDEC = 0
  111.  

 
Black-White.bmp
« Last Edit: November 29, 2021, 11:34:30 am by Petr »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Save Black-White BMP file [2 color BMP]
« Reply #1 on: November 29, 2021, 12:16:14 pm »
For this to work, the X-axis (width) image resolution must always be divisible by 32.


Are you certain?  BMP files require packing at each line, unless the line is evenly divisible by 4.  What you're seeing is probably related to that.  https://www.qb64.org/forum/index.php?topic=3602.msg129449#msg129449
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Save Black-White BMP file [2 color BMP]
« Reply #2 on: November 29, 2021, 12:23:34 pm »
If i am sure? Not. During the output testing, I deduced that it works with the condition that it is divisible by 32, but of course, I will check whether it will also work with the condition that the x-axis be divisible by four. Thanks for warning.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Save Black-White BMP file [2 color BMP]
« Reply #3 on: November 29, 2021, 12:42:49 pm »
The most outputs (all test pass) is with 32. With 4 are some images uncompatible - unusable. You can test it on row 38 in my source code, if you rewrite it to 4 and use some your JPG file from Christmas theme, with 32 are all usable, with 4 not.

Here is one test - image created with my daughter after BW process:

 
Black-White.bmp

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Save Black-White BMP file [2 color BMP]
« Reply #4 on: November 30, 2021, 12:49:01 pm »
@SMcNeill

Actually, we're both talking about the same thing! Look: If you're encoding a bitmap in this format, I'm talking about 32 bits and you're talking about 4 bytes. And we are home! After all, it's the same (8x4), in this format you encode bits, what one bit, that one pixel.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Save Black-White BMP file [2 color BMP]
« Reply #5 on: November 30, 2021, 01:24:31 pm »
@Petr

You have a very distinct style to your drawing. I've only seen the volley ball thing (2x's: program and old avatar snip) and this little sketch but it seems cool, playful "tell". I wonder if cartoons can be cultural and if you picked up from that.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Save Black-White BMP file [2 color BMP]
« Reply #6 on: December 02, 2021, 11:09:01 am »
@bplus
Thank you for your response. It's like this: Volleyball is a graphic drawn by my hand using a mouse (MX Master). This mouse is excellent, and very sensitive. The picture here is a hand-painted drawing of my younger daughter's hands. The only thing I have to do with this is that I scanned it and converted it into a black and white image. And yes, I do write something in QB64 with pictures of my daughter, but it's too early to talk about it. Namely, it is not the only thing, there is another big program where is MEMSOUND the king and I am developing it at the same time, even though the programs are to different theme and do not affect each other.