Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read.bmp() mismatch between predicted and actual number of bytes in the file

Tags:

r

I'm trying to read a BMP file in R using the read.bmp function in the bmp package. However, I get the following error:

Error in read.bmp("tiger.bmp") :mismatch between predicted and actual number of bytes in image

I am not sure how can I get past this error. Any and all help is highly appreciated.

like image 617
WitchKingofAngmar Avatar asked Jul 05 '16 18:07

WitchKingofAngmar


People also ask

How do I read a BMP file?

You can open BMP files on either PC or Mac with external software, such as Adobe Creative Cloud. If you use a PC or Mac, start by opening the folder with the BMP file you want to use. Right-click on the file name and then hover over the Open With option.

Can Fopen open BMP files?

No. fopen() is a library call provided by the standard library for the C language (libc).

How do BMP files work?

The BMP format stores color data for each pixel in the image without any compression. For example, a 10x10 pixel BMP image will include color data for 100 pixels. This method of storing image information allows for crisp, high-quality graphics, but also produces large file sizes.


1 Answers

That message means that read.bmp is expecting a certain byte depth, 4 bytes per integer in the binary, with each integer composing a part of the pixel, which you can see in this section of code. The script is defining and testing the dimensions of the file based on the color bit depth and header definitions.

If you look at this code which is under the hood of the function:

  bytes_pixel=h$depth / 8
  row_width_bytes=h$width * bytes_pixel
  # bmp rows are written to lenth of nearest 4 bytes
  rounded_row_width_bytes = ceiling(row_width_bytes/4)*4
  bytes_to_trim = row_width_bytes %% 4
  bytes_to_read=rounded_row_width_bytes * h$height
  if(h$bmp_bytesz==0) {
    if(Verbose) warning("invalid byte size information for image")
  }
  else if(h$bmp_bytesz != bytes_to_read)
    stop("mismatch between predicted and actual number of bytes in image")

h$bmp_bytesz=4

The h$ fields are all defined header settings which set the byte depth of 1L to 4 for various dimensions of the image. This section is checking to see that the file is coming in as expected. It is either 8, 24 or 32 bit because it made it past the first warning above this section. It stopped because there is a problem with the encoded dimensions of the file.

If the file is correctly formatted and not damaged, with 3x8 channels, it should come in properly. (or black and white with 1x8 channels)

Try running:

is.bmp('tiger.bmp') to see if it returns that it is a viable Windows BMP file.

like image 61
sconfluentus Avatar answered Oct 10 '22 01:10

sconfluentus