Note that there are some explanatory texts on larger screens.

plurals
  1. PO8Bit Grayscale Bitmap to Array
    text
    copied!<p>I am wondering, from here how would I go about storing the pixel values into a matrix? I would like to print out the intensity (grayscale) value.</p> <p>Example Array[5] = 4 Or Array[3][1] = 17</p> <p>I really have no idea how to go about this. All the examples I see seem way to complicated, is there a simple way to do this?</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; struct BitMap { short Type; long Size; short Reserve1; short Reserve2; long OffBits; long biSize; long biWidth; long biHeight; short biPlanes; short biBitCount; long biCompression; long biSizeImage; long biXPelsPerMeter; long biYPelsPerMeter; long biClrUsed; long biClrImportant; }Header; int main( void ){ FILE *BMPFile = fopen ("MriHotrod.bmp", "rb"); if(BMPFile == NULL){ return; } unsigned char DataBuff[128*128]; int i; int j; memset(&amp;Header, 0, sizeof(Header)); fread(&amp;Header.Type, 2, 1, BMPFile); fread(&amp;Header.Size, 4, 1, BMPFile); fread(&amp;Header.Reserve1, 2, 1, BMPFile); fread(&amp;Header.Reserve2, 2, 1, BMPFile); fread(&amp;Header.OffBits, 4, 1, BMPFile); fread(&amp;Header.biSize, 4, 1, BMPFile); fread(&amp;Header.biWidth, 4, 1, BMPFile); fread(&amp;Header.biHeight, 4, 1, BMPFile); fread(&amp;Header.biPlanes, 2, 1, BMPFile); fread(&amp;Header.biBitCount, 2, 1, BMPFile); fread(&amp;Header.biCompression, 4, 1, BMPFile); fread(&amp;Header.biSizeImage, 4, 1, BMPFile); fread(&amp;Header.biXPelsPerMeter, 4, 1, BMPFile); fread(&amp;Header.biYPelsPerMeter, 4, 1, BMPFile); fread(&amp;Header.biClrUsed, 4, 1, BMPFile); fread(&amp;Header.biClrImportant, 4, 1, BMPFile); printf("\nType:%hd\n", Header.Type); printf("Size:%ld\n", Header.Size); printf("Reserve1:%hd\n", Header.Reserve1); printf("Reserve2:%hd\n", Header.Reserve2); printf("OffBits:%ld\n", Header.OffBits); printf("biSize:%ld\n", Header.biSize); printf("Width:%ld\n", Header.biWidth); printf("Height:%ld\n", Header.biHeight); printf("biPlanes:%hd\n", Header.biPlanes); printf("biBitCount:%hd\n", Header.biBitCount); printf("biCompression:%ld\n", Header.biCompression); printf("biSizeImage:%ld\n", Header.biSizeImage); printf("biXPelsPerMeter:%ld\n", Header.biXPelsPerMeter); printf("biYPelsPerMeter:%ld\n", Header.biYPelsPerMeter); printf("biClrUsed:%ld\n", Header.biClrUsed); printf("biClrImportant:%ld\n\n", Header.biClrImportant); fread(DataBuff, sizeof(DataBuff), 128*128, BMPFile); for(i=0; i&lt;20; i++){ printf("%d\n", DataBuff[i]); } fclose(BMPFile); return 0; } Output Type:19778 Size:17462 Reserve1:0 Reserve2:0 OffBits:1078 biSize:40 Width:128 Height:128 biPlanes:1 biBitCount:8 biCompression:0 biSizeImage:0 biXPelsPerMeter:0 biYPelsPerMeter:0 biClrUsed:256 biClrImportant:0 0 0 0 0 1 1 1 0 2 2 2 0 3 3 3 0 4 4 4 0 </code></pre> <p>I think the header info is correct, and the number of elements is correct (16384). I am just unsure how to print out the pixel values. The above is clearly not right...</p> <p>Does this have to do with padding?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload