[C++] Comment lire les données d'une image ?

Comment lire les données d'une image ? [C++] - Programmation

Marsh Posté le 10-07-2001 à 01:10:43    

Salut !
 
Je voudrais lire une image BMP 8 bits en dégradé de gris format Windows, mais je ne sais pas comment faire. Je voudrais juste récupéré les intensités de noir pour les stockées dans une matrice.
 
Si vous avez des idées, des tuts ou encore mieux du code, ca m'intéresse :)
 
@+

 

[edtdd]--Message édité par Arlo13--[/edtdd]


---------------
"Software is like sex it's better when it's free !!!! "
Reply

Marsh Posté le 10-07-2001 à 01:10:43   

Reply

Marsh Posté le 10-07-2001 à 10:24:09    

Tiré de la doc de Win32:

Code :
  1. Bitmaps should be saved in a file that uses the established Windows format and assigned a name with the three-character .BMP extension. The established Windows format consists of a BITMAPFILEHEADER structure followed by a BITMAPINFOHEADER structure. An array of RGBQUAD structures (also called a color table) follows the BITMAPINFOHEADER structure. The color table is followed by a second array of indexes into the color table (the actual bitmap data).
  2. The Windows format is shown in the following illustration.
  3. The members of the BITMAPFILEHEADER structure identify the file; specify the size of the file, in bytes; and specify the offset from the first byte in the header to the first byte of bitmap data. The members of the BITMAPINFOHEADER structure specify the bitmap's width and height, in pixels; the color format (count of color planes and color bits per pixel) of the display device on which the bitmap was created; whether the bitmap data was compressed before storage and the type of compression used; the number of bytes of bitmap data; the resolution of the display device on which the bitmap was created; and the number of colors represented in the data. The RGBQUAD structures specify the RGB intensity values for each of the colors in the device's palette. The color-index array maps indices values from the RGBQUAD array to pixels in a rectangular region on the display.
  4. The following hexadecimal output shows the contents of the file REDBRICK.BMP.
  5. 0000    42 4d 76 02 00 00 00 00  00 00 76 00 00 00 28 00
  6. 0010    00 00 20 00 00 00 20 00  00 00 01 00 04 00 00 00
  7. 0020    00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
  8. 0030    00 00 00 00 00 00 00 00  00 00 00 00 80 00 00 80
  9. 0040    00 00 00 80 80 00 80 00  00 00 80 00 80 00 80 80
  10. 0050    00 00 80 80 80 00 c0 c0  c0 00 00 00 ff 00 00 ff
  11. 0060    00 00 00 ff ff 00 ff 00  00 00 ff 00 ff 00 ff ff
  12. 0070    00 00 ff ff ff 00 00 00  00 00 00 00 00 00 00 00
  13. 0080    00 00 00 00 00 00 00 00  00 00 00 00 00 00 09 00
  14. 0090    00 00 00 00 00 00 11 11  01 19 11 01 10 10 09 09
  15. 00a0    01 09 11 11 01 90 11 01  19 09 09 91 11 10 09 11
  16. 00b0    09 11 19 10 90 11 19 01  19 19 10 10 11 10 09 01
  17. 00c0    91 10 91 09 10 10 90 99  11 11 11 11 19 00 09 01
  18. 00d0    91 01 01 19 00 99 11 10  11 91 99 11 09 90 09 91
  19. 00e0    01 11 11 11 91 10 09 19  01 00 11 90 91 10 09 01
  20. 00f0    11 99 10 01 11 11 91 11  11 19 10 11 99 10 09 10
  21. 0100    01 11 11 11 19 10 11 09  09 10 19 10 10 10 09 01
  22. 0110    11 19 00 01 10 19 10 11  11 01 99 01 11 90 09 19
  23. 0120    11 91 11 91 01 11 19 10  99 00 01 19 09 10 09 19
  24. 0130    10 91 11 01 11 11 91 01  91 19 11 00 99 90 09 01
  25. 0140    01 99 19 01 91 10 19 91  91 09 11 99 11 10 09 91
  26. 0150    11 10 11 91 99 10 90 11  01 11 11 19 11 90 09 11
  27. 0160    00 19 10 11 01 11 99 99  99 99 99 99 99 99 09 99
  28. 0170    99 99 99 99 99 99 00 00  00 00 00 00 00 00 00 00
  29. 0180    00 00 00 00 00 00 90 00  00 00 00 00 00 00 00 00
  30. 0190    00 00 00 00 00 00 99 11  11 11 19 10 19 19 11 09
  31. 01a0    10 90 91 90 91 00 91 19  19 09 01 10 09 01 11 11
  32. 01b0    91 11 11 11 10 00 91 11  01 19 10 11 10 01 01 11
  33. 01c0    90 11 11 11 91 00 99 09  19 10 11 90 09 90 91 01
  34. 01d0    19 09 91 11 01 00 90 10  19 11 00 11 11 00 10 11
  35. 01e0    01 10 11 19 11 00 90 19  10 91 01 90 19 99 00 11
  36. 01f0    91 01 11 01 91 00 99 09  09 01 10 11 91 01 10 91
  37. 0200    99 11 10 90 91 00 91 11  00 10 11 01 10 19 19 09
  38. 0210    10 00 99 01 01 00 91 01  19 91 19 91 11 09 10 11
  39. 0220    00 91 00 10 90 00 99 01  11 10 09 10 10 19 09 01
  40. 0230    91 90 11 09 11 00 90 99  11 11 11 90 19 01 19 01
  41. 0240    91 01 01 19 09 00 91 10  11 91 99 09 09 90 11 91
  42. 0250    01 19 11 11 91 00 91 19  01 00 11 00 91 10 11 01
  43. 0260    11 11 10 01 11 00 99 99  99 99 99 99 99 99 99 99
  44. 0270    99 99 99 99 99 90
  45. The following table shows the data bytes associated with the structures in a bitmap file.
  46. Structure Corresponding bytes
  47. BITMAPFILEHEADER 0x00 - 0x0D
  48. BITMAPINFOHEADER 0x0E - 0x31
  49. RGBQUAD array 0x32 - 0x75
  50. Color-index array 0x76 - 0x275


 

Code :
  1. typedef struct tagBITMAPFILEHEADER { // bmfh  
  2.         WORD    bfType;
  3.         DWORD   bfSize;
  4.         WORD    bfReserved1;
  5.         WORD    bfReserved2;
  6.         DWORD   bfOffBits;
  7. } BITMAPFILEHEADER;
  8. The BITMAPFILEHEADER structure contains information about the type, size, and layout of a file that contains a device-independent bitmap (DIB).
  9. Members
  10. bfType
  11. Specifies the file type. It must be BM.
  12. bfSize
  13. Specifies the size, in bytes, of the bitmap file.
  14. bfReserved1
  15. Reserved; must be zero.
  16. bfReserved2
  17. Reserved; must be zero.
  18. bfOffBits
  19. Specifies the offset, in bytes, from the BITMAPFILEHEADER structure to the bitmap bits.
  20. Remarks
  21. A BITMAPINFO or BITMAPCOREINFO structure immediately follows the BITMAPFILEHEADER structure in the DIB file.


 

Code :
  1. typedef struct tagBITMAPINFOHEADER{ // bmih  
  2.    DWORD  biSize;
  3.    LONG   biWidth;
  4.    LONG   biHeight;
  5.    WORD   biPlanes;
  6.    WORD   biBitCount
  7.    DWORD  biCompression;
  8.    DWORD  biSizeImage;
  9.    LONG   biXPelsPerMeter;
  10.    LONG   biYPelsPerMeter;
  11.    DWORD  biClrUsed;
  12.    DWORD  biClrImportant;
  13. } BITMAPINFOHEADER;
  14. The BITMAPINFOHEADER structure contains information about the dimensions and color format of a device-independent bitmap (DIB).
  15. Members
  16. biSize
  17. Specifies the number of bytes required by the structure.
  18. biWidth
  19. Specifies the width of the bitmap, in pixels.
  20. biHeight
  21. Specifies the height of the bitmap, in pixels. If biHeight is positive, the bitmap is a bottom-up DIB and its origin is the lower left corner. If biHeight is negative, the bitmap is a top-down DIB and its origin is the upper left corner.
  22. biPlanes
  23. Specifies the number of planes for the target device. This value must be set to 1.
  24. biBitCount
  25. Specifies the number of bits per pixel. This value must be 1, 4, 8, 16, 24, or 32.
  26. biCompression
  27. Specifies the type of compression for a compressed bottom-up bitmap (top-down DIBs cannot be compressed). It can be one of the following values:
  28. Value Description
  29. BI_RGB An uncompressed format.
  30. BI_RLE8 A run-length encoded (RLE) format for bitmaps with 8 bits per pixel. The compression format is a two-byte format consisting of a count byte followed by a byte containing a color index. For more information, see the following Remarks section.
  31. BI_RLE4 An RLE format for bitmaps with 4 bits per pixel. The compression format is a two-byte format consisting of a count byte followed by two word-length color indices. For more information, see the following Remarks section.
  32. BI_BITFIELDS Specifies that the bitmap is not compressed and that the color table consists of three doubleword color masks that specify the red, green, and blue components, respectively, of each pixel. This is valid when used with 16- and 32-bits-per-pixel bitmaps.
  33. biSizeImage
  34. Specifies the size, in bytes, of the image. This may be set to 0 for BI_RGB bitmaps.
  35. biXPelsPerMeter
  36. Specifies the horizontal resolution, in pixels per meter, of the target device for the bitmap. An application can use this value to select a bitmap from a resource group that best matches the characteristics of the current device.
  37. biYPelsPerMeter
  38. Specifies the vertical resolution, in pixels per meter, of the target device for the bitmap.
  39. biClrUsed
  40. Specifies the number of color indices in the color table that are actually used by the bitmap. If this value is zero, the bitmap uses the maximum number of colors corresponding to the value of the biBitCount member for the compression mode specified by biCompression.
  41. If biClrUsed is nonzero and the biBitCount member is less than 16, the biClrUsed member specifies the actual number of colors the graphics engine or device driver accesses. If biBitCount is 16 or greater, then biClrUsed member specifies the size of the color table used to optimize performance of Windows color palettes. If biBitCount equals 16 or 32, the optimal color palette starts immediately following the three doubleword masks.
  42. If the bitmap is a packed bitmap (a bitmap in which the bitmap array immediately follows the BITMAPINFO header and which is referenced by a single pointer), the biClrUsed member must be either 0 or the actual size of the color table.
  43. biClrImportant
  44. Specifies the number of color indices that are considered important for displaying the bitmap. If this value is zero, all colors are important.
  45. Remarks
  46. The BITMAPINFO structure combines the BITMAPINFOHEADER structure and a color table to provide a complete definition of the dimensions and colors of a DIB. For more information about DIBs, see the description of the BITMAPINFO data structure.
  47. An application should use the information stored in the biSize member to locate the color table in a BITMAPINFO structure, as follows:
  48. pColor = ((LPSTR)pBitmapInfo +
  49.     (WORD)(pBitmapInfo->bmiHeader.biSize));
  50. Windows supports formats for compressing bitmaps that define their colors with eight or four bits per pixel. Compression reduces the disk and memory storage required for the bitmap. The following paragraphs describe these formats.
  51. When the biCompression member is BI_RLE8, the bitmap is compressed by using a run-length encoding (RLE) format for an 8-bit bitmap. This format can be compressed in encoded or absolute modes. Both modes can occur anywhere in the same bitmap.
  52. · Encoded mode consists of two bytes: the first byte specifies the number of consecutive pixels to be drawn using the color index contained in the second byte. In addition, the first byte of the pair can be set to zero to indicate an escape that denotes an end of line, end of bitmap, or delta. The interpretation of the escape depends on the value of the second byte of the pair, which can be one of the following:
  53. Value Meaning
  54. 0 End of line.
  55. 1 End of bitmap.
  56. 2 Delta. The two bytes following the escape contain unsigned values indicating the horizontal and vertical offsets of the next pixel from the current position.
  57. · In absolute mode, the first byte is zero and the second byte is a value in the range 03H through FFH. The second byte represents the number of bytes that follow, each of which contains the color index of a single pixel. When the second byte is 2 or less, the escape has the same meaning as in encoded mode. In absolute mode, each run must be aligned on a word boundary.
  58. The following example shows the hexadecimal values of an 8-bit compressed bitmap.
  59. 03 04 05 06 00 03 45 56 67 00 02 78 00 02 05 01
  60. 02 78 00 00 09 1E 00 01
  61. This bitmap would expand as follows (two-digit values represent a color index for a single pixel):
  62. 04 04 04
  63. 06 06 06 06 06
  64. 45 56 67
  65. 78 78
  66. move current position 5 right and 1 down
  67. 78 78
  68. end of line
  69. 1E 1E 1E 1E 1E 1E 1E 1E 1E
  70. end of RLE bitmap
  71. When the biCompression member is BI_RLE4, the bitmap is compressed by using a run-length encoding format for a 4-bit bitmap, which also uses encoded and absolute modes:
  72. · In encoded mode, the first byte of the pair contains the number of pixels to be drawn using the color indices in the second byte. The second byte contains two color indices, one in its high-order four bits and one in its low-order four bits. The first of the pixels is drawn using the color specified by the high-order four bits, the second is drawn using the color in the low-order four bits, the third is drawn using the color in the high-order four bits, and so on, until all the pixels specified by the first byte have been drawn.
  73. · In absolute mode, the first byte is zero, the second byte contains the number of color indices that follow, and subsequent bytes contain color indices in their high- and low-order four bits, one color index for each pixel. In absolute mode, each run must be aligned on a word boundary. The end-of-line, end-of-bitmap, and delta escapes described for BI_RLE8 also apply to BI_RLE4 compression.
  74. The following example shows the hexadecimal values of a 4-bit compressed bitmap.
  75. 03 04 05 06 00 06 45 56 67 00 04 78 00 02 05 01
  76. 04 78 00 00 09 1E 00 01
  77. This bitmap would expand as follows (single-digit values represent a color index for a single pixel):
  78. 0 4 0
  79. 0 6 0 6 0
  80. 4 5 5 6 6 7
  81. 7 8 7 8
  82. move current position 5 right and 1 down
  83. 7 8 7 8
  84. end of line
  85. 1 E 1 E 1 E 1 E 1
  86. end of RLE bitmap
  87. If biHeight is negative, indicating a top-down DIB, biCompression must be either BI_RGB or BI_BITFIELDS. Top-down DIBs cannot be compressed.


 

Code :
  1. typedef struct tagRGBQUAD { // rgbq  
  2.     BYTE    rgbBlue;
  3.     BYTE    rgbGreen;
  4.     BYTE    rgbRed;
  5.     BYTE    rgbReserved;
  6. } RGBQUAD;
  7. The RGBQUAD structure describes a color consisting of relative intensities of red, green, and blue.
  8. Members
  9. rgbBlue
  10. Specifies the intensity of blue in the color.
  11. rgbGreen
  12. Specifies the intensity of green in the color.
  13. rgbRed
  14. Specifies the intensity of red in the color.
  15. rgbReserved
  16. Reserved; must be zero.
  17. Remarks
  18. The bmiColors member of the BITMAPINFO structure consists of an array of RGBQUAD structures.


 
Bonne lecture! ;)

Reply

Marsh Posté le 10-07-2001 à 10:48:29    

ce sera peut être plus facile en français...  
http://progzone.free.fr/graphisme/formats/bmp/bmp.html

Reply

Marsh Posté le 10-07-2001 à 10:56:12    

coucou Toucouch et Pitounet ca va ?
Moi ca va. Il me reste plus que 9 jours de stages. Vous aussi je suppose !!!

Reply

Marsh Posté le 10-07-2001 à 12:28:36    

Hé ben, ça glande sec les stagiaires! :D
Pitounet > C'est vrai que c'est mieux en français! :)

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed