Author Topic: Direct Bitmap reading  (Read 4790 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
Direct Bitmap reading
« on: August 15, 2018, 09:39:26 am »
Hi.
This program shows a small part of what _LOADIMAGE  do when reading BMP files. The program will appear without the _LOADIMAGE command. Read BMP images in 1-bit, 4-bit, 8-bit and 24-bit formats. It's just for information for those who wonder how to load the BMP file without the LOADIMAGE command. I've figured out how to fix reading CUR files, which is exactly what I'm going to do now.

Code: QB64: [Select]
  1. 'program try load 1 bit (2 color) / 4 bit (16 color) / 8 bit (256 color) / 24 bit BMP picture without _LOADIMAGE:
  2. 'i writed it, because i am interested about it...    Petr
  3. 'some images are viewed correctly, some not with the same program. I am sorry, but i do not know why.
  4. 'all knowledge for writing this program so as it now is, i read from english wikipedia (Czech contains not enought informations). I found, that some
  5. 'records in english wikipedia are not correctly and then muss reapiring some head types in this program to work.....
  6. 'not writed for speed. Just for fun.
  7.  
  8. 'UPGRADED: Repaired bug reading 1bit / 4 bit BMPs. X axis muss be dividible by 4! So if X pointer is already so big as is image width, but is not
  9. 'dividible by 32 (4 bytes x 8 bites), then CAN NOT BE RESTARTED BACK TO 0! Only if is X MOD 32 = 0 AND X >= image WIDTH, then can be X restarted back
  10. 'to zero. I have read about this attribute on the Czech forums about BMP format, this is not even neited writed in english wikipedia.
  11.  
  12. 'I can add support for 32 bit BMPs if you send me some picture in this format.
  13.  
  14. S:
  15. runit = runit + 1
  16. SELECT CASE runit
  17.     CASE 1:
  18.         PRINT "Opening Black/White, 1bit recorded BMP file kitten1.BMP"
  19.         file$ = "kitten1.bmp"
  20.     CASE 2:
  21.         PRINT "Opening 16 color BMP file kitten4.BMP"
  22.         file$ = "kitten4.bmp"
  23.     CASE 3:
  24.         PRINT "Opening 256 color BMP file kitten8.BMP"
  25.         file$ = "kitten8.bmp"
  26.     CASE 4:
  27.         PRINT "Opening 24bit (hi color) BMP file kitten24.BMP"
  28.         file$ = "kitten24.bmp"
  29.     CASE 5:
  30.         PRINT "Opening 1bit (2 colors) BMP file MONKEY1.BMP"
  31.         file$ = "monkey1.bmp"
  32.     CASE 6:
  33.         PRINT "Opening 16 color file MONKEY16.BMP"
  34.         file$ = "monkey16.bmp"
  35.     CASE 7:
  36.         PRINT "Opening 256 color file MONKEY256.BMP"
  37.         file$ = "monkey256.bmp"
  38.     CASE 8: PRINT "Opening 24bit file MONKEY.BMP"
  39.         file$ = "monkey.bmp"
  40.     CASE 9: PRINT "End."
  41.         SLEEP 2
  42.         SYSTEM
  43.  
  44. TYPE BMP
  45.     id AS STRING * 2
  46.     size AS LONG
  47.     res1 AS INTEGER
  48.     res2 AS INTEGER
  49.     offset AS LONG
  50.     'bitmapfileheader end
  51.  
  52.     headersize AS LONG '12 bytes
  53.     width AS LONG
  54.     height AS LONG
  55.     planes AS INTEGER 'always 1
  56.     bpp AS INTEGER 'bits per pixel
  57.     crc AS LONG 'compression if is used
  58.     imgSize AS LONG ' image data block size
  59.     hr AS LONG 'horizontal resolution in pixels / meter
  60.     vr AS LONG 'vertical resolution in pixels / meter
  61.     numPalClrs AS LONG ' number of colors in color palette
  62.     impClr AS LONG 'number of important colors used or 0
  63.  
  64. DIM SHARED BMP AS BMP, x AS INTEGER, y AS INTEGER, PA AS LONG, PB AS LONG, PC AS LONG
  65. IF _FILEEXISTS(file$) THEN OPEN file$ FOR BINARY AS #1 ELSE PRINT "File "; UCASE$(file$); " not exist.": SLEEP 3: END
  66. GET #1, , BMP
  67.  
  68.  
  69. SELECT CASE BMP.bpp
  70.     CASE 24: C = 32
  71.     CASE 8: C = 256: Looop = 255
  72.     CASE 4: C = 256: Looop = 3
  73.     CASE 1: C = 256: Looop = 1
  74.  
  75. m& = _NEWIMAGE(BMP.width, BMP.height, C)
  76. IF BMP.bpp <= 8 THEN
  77.     FOR LoadPalette = 0 TO Looop
  78.         GET #1, , PA
  79.         _PALETTECOLOR LoadPalette, PA, m&
  80.     NEXT
  81.  
  82. PRINT "File size returned with LOF:"; LOF(1)
  83. PRINT "File identity:"; BMP.id$
  84. PRINT "File size writed in file:"; BMP.size
  85. PRINT "Image data start offset:"; BMP.offset
  86. PRINT "Header size in bytes:"; BMP.headersize
  87. PRINT "Image width:"; BMP.width
  88. PRINT "Image height:"; BMP.height
  89. PRINT "Planes:"; BMP.planes
  90. PRINT "Bites per pixel:"; BMP.bpp
  91. PRINT "Compression: "; BMP.crc
  92. PRINT "Image size: "; BMP.imgSize
  93. PRINT "Vertical resolution: (px/M) "; BMP.vr
  94. PRINT "Horizontal resolution: (px/M) "; BMP.hr
  95. PRINT "Number colors in palette: "; BMP.numPalClrs
  96. PRINT "Number of important colors used: "; BMP.impClr
  97. PRINT "Reserved 1: "; BMP.res1
  98. PRINT "Reserved 2: "; BMP.res2
  99.  
  100. PRINT "Current position:"; SEEK(1)
  101. PRINT "Press any key"
  102.  
  103. x = 1: y = BMP.height
  104. SEEK #1, BMP.offset + 1
  105. SA = SEEK(1)
  106.  
  107.     SELECT CASE BMP.bpp
  108.  
  109.         CASE 1 'B/W pictures
  110.             IF y = 0 THEN y = BMP.height - 1
  111.             REDIM clr1 AS STRING * 4
  112.             clr1$ = SPACE$(4)
  113.             GET #1, , clr1$
  114.             binary$ = ""
  115.             FOR all = 1 TO 4
  116.                 binary$ = binary$ + DECtoBIN$(ASC(clr1$, all))
  117.             NEXT all
  118.  
  119.             FOR drw = 1 TO LEN(binary$)
  120.                 commnd$ = MID$(binary$, drw, 1)
  121.                 IF commnd$ = "1" THEN PSET (x, y)
  122.                 x = x + 1
  123.                 IF x >= BMP.width THEN
  124.                     IF x MOD 32 = 0 THEN x = 0: y = y - 1
  125.                 END IF
  126.             NEXT drw
  127.  
  128.         CASE 4 '16 color bitmap
  129.             IF y = 0 THEN y = BMP.height - 1
  130.  
  131.             REDIM clr4 AS STRING * 4
  132.             clr4$ = SPACE$(4)
  133.             GET #1, , clr4$
  134.             binary$ = ""
  135.             FOR all = 1 TO 4
  136.                 binary$ = binary$ + DECtoBIN$(ASC(clr4, all))
  137.             NEXT all
  138.  
  139.             FOR SET = 1 TO LEN(binary$) STEP 4
  140.                 value$ = MID$(binary$, SET, 4)
  141.                 clr = BINtoDEC(value$)
  142.                 PSET (x, y), clr
  143.                 x = x + 1: IF x >= BMP.width THEN
  144.                     IF x MOD 32 / 4 = 0 THEN x = 0: y = y - 1
  145.                 END IF
  146.             NEXT SET
  147.  
  148.  
  149.         CASE 8 '256 color bitmap
  150.             IF y = 0 THEN y = BMP.height - 1
  151.             REDIM clr AS STRING * 1
  152.             GET #1, , clr
  153.             PSET (x, y), ASC(clr)
  154.             x = x + 1: IF x >= BMP.width THEN
  155.                 IF x MOD 4 = 0 THEN
  156.                     x = 0: y = y - 1
  157.                 END IF
  158.             END IF
  159.             '            PSET (x, y), ASC(clr)
  160.  
  161.         CASE 24 '24 bit bitmap
  162.             IF y = 0 THEN y = BMP.height - 1
  163.             REDIM cl AS STRING * 3
  164.             GET #1, , cl
  165.             b = ASC(cl, 1)
  166.             g = ASC(cl, 2)
  167.             r = ASC(cl, 3)
  168.  
  169.  
  170.             x = x + 1
  171.             IF x >= BMP.width THEN
  172.                 IF x MOD 4 = 0 THEN
  173.                     x = 0: y = y - 1
  174.                 END IF
  175.             END IF
  176.             PSET (x, y), _RGB32(r, g, b)
  177.     END SELECT
  178.  
  179.  
  180.  
  181.  
  182. FUNCTION DECtoBIN$ (vstup)
  183.     FOR rj = 7 TO 0 STEP -1
  184.         IF vstup AND 2 ^ rj THEN DECtoBIN$ = DECtoBIN$ + "1" ELSE DECtoBIN$ = DECtoBIN$ + "0"
  185.     NEXT rj
  186.  
  187. FUNCTION BINtoDEC (b AS STRING)
  188.     FOR Si = 1 TO LEN(b)
  189.         e$ = MID$(b$, Si, 1)
  190.         c = VAL(e$) '
  191.         Sj = LEN(b) - Si
  192.         BINtoDEC = BINtoDEC + (c * 2 ^ Sj)
  193.     NEXT Si
  194.  
* bmps.zip (Filesize: 621.87 KB, Downloads: 348)