tag:blogger.com,1999:blog-1839870468145408669.post8048733065370652439..comments2024-03-28T04:52:30.605-07:00Comments on Solutions: Code for reading bmp image files directly in C/C++risingsunhttp://www.blogger.com/profile/00560773894139187343noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-1839870468145408669.post-44900764830175378732018-07-08T03:22:18.375-07:002018-07-08T03:22:18.375-07:00this code isn't working ...this code isn't working ... Anonymoushttps://www.blogger.com/profile/12354697617463387914noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-34337477573657625842016-12-31T02:43:28.191-08:002016-12-31T02:43:28.191-08:00Good work and thanks for the code.
There is a func...Good work and thanks for the code.<br />There is a functional bug which took me around 2hours to point out.<br />array[i*width+j+0] = data[j+2]; should be array[i*width*3+j+0]. Notice that you have to multiply "i * width * 3".<br /><br />Here is the working code:<br />static unsigned char *texels;<br />static int width, height;<br /><br />static void readBmp(char *filename)<br />{<br /> FILE *fd;<br /> fd = fopen(filename, "rb");<br /> if (fd == NULL)<br /> {<br /> printf("Error: fopen failed\n");<br /> return; <br /> }<br /><br /> unsigned char header[54];<br /><br /> // Read header<br /> fread(header, sizeof(unsigned char), 54, fd);<br /><br /> // Capture dimensions<br /> width = *(int*)&header[18];<br /> height = *(int*)&header[22];<br /> <br /> int padding = 0;<br /> <br /> // Calculate padding<br /> while ((width * 3 + padding) % 4 != 0)<br /> {<br /> padding++;<br /> }<br /><br /> // Compute new width, which includes padding<br /> int widthnew = width * 3 + padding;<br /><br /> // Allocate memory to store image data (non-padded)<br /> texels = (unsigned char *)malloc(width * height * 3 * sizeof(unsigned char));<br /> if (texels == NULL)<br /> {<br /> printf("Error: Malloc failed\n");<br /> return;<br /> }<br /><br /> // Allocate temporary memory to read widthnew size of data<br /> unsigned char* data = (unsigned char *)malloc(widthnew * sizeof (unsigned int));<br /><br /> // Read row by row of data and remove padded data.<br /> for (int i = 0; i<height; i++)<br /> {<br /> // Read widthnew length of data<br /> fread(data, sizeof(unsigned char), widthnew, fd);<br /><br /> // Retain width length of data, and swizzle RB component.<br /> // BMP stores in BGR format, my usecase needs RGB format<br /> for (int j = 0; j < width * 3; j += 3)<br /> {<br /> int index = (i * width * 3) + (j);<br /> texels[index + 0] = data[j + 2];<br /> texels[index + 1] = data[j + 1];<br /> texels[index + 2] = data[j + 0];<br /> }<br /> }<br /><br /> free(data);<br /> fclose(fd);<br />}Anonymoushttps://www.blogger.com/profile/13019883541095460057noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-28793649811378849872016-10-27T00:30:04.015-07:002016-10-27T00:30:04.015-07:00Plz can u give me soln. How to output that stored ...Plz can u give me soln. How to output that stored data to output console in c++Akshayhttps://www.blogger.com/profile/05280036211411854529noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-22919264141831198422016-10-27T00:28:56.676-07:002016-10-27T00:28:56.676-07:00Plz can u give me soln. How to output that stored ...Plz can u give me soln. How to output that stored data to output console in c++Akshayhttps://www.blogger.com/profile/05280036211411854529noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-2597028588276378272016-08-09T02:02:49.604-07:002016-08-09T02:02:49.604-07:00how to store pixel values into 2D arrayhow to store pixel values into 2D arrayAnonymoushttps://www.blogger.com/profile/03555236666700176335noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-87110298450167200562016-08-09T02:02:04.589-07:002016-08-09T02:02:04.589-07:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/03555236666700176335noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-10110823969894997492015-11-18T05:55:41.002-08:002015-11-18T05:55:41.002-08:00Is there any chance that we can see the correct co...Is there any chance that we can see the correct code?Ratkahttps://www.blogger.com/profile/09218709484908366887noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-48937547006639546692015-10-18T11:03:57.177-07:002015-10-18T11:03:57.177-07:00babes u r right....and love u toobabes u r right....and love u tooAnonymoushttps://www.blogger.com/profile/08601138150079394306noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-67844089948659974482015-10-18T11:03:22.770-07:002015-10-18T11:03:22.770-07:00This comment has been removed by a blog administrator.Anonymoushttps://www.blogger.com/profile/08601138150079394306noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-79237857755755355162015-06-23T00:49:26.161-07:002015-06-23T00:49:26.161-07:00There are a lot of errors. Havent you run it even ...There are a lot of errors. Havent you run it even ones ? I think not. Even 'i' in the first for loop is not defined ..Anonymoushttps://www.blogger.com/profile/14877473425075277868noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-9928738955804196172015-06-23T00:44:58.412-07:002015-06-23T00:44:58.412-07:00data is also undefined
data is also undefined<br />Anonymoushttps://www.blogger.com/profile/14877473425075277868noreply@blogger.comtag:blogger.com,1999:blog-1839870468145408669.post-61503678036239344382015-06-23T00:36:55.899-07:002015-06-23T00:36:55.899-07:00height is undefined ? height is undefined ? Anonymoushttps://www.blogger.com/profile/14877473425075277868noreply@blogger.com