Frankenstein Int problems Neon and Helium?

I don't even claim to be MOTU, but some things I understand quite well. I was working on generation of amino acids using nano technology and the equation that defines the universe and I stopped first at Wikipedia amino acid, ( snarfed a couple SVGs to play with ) and then I decided to incorporate spheres in my simulation. I have a procedure for that, but I wanted to refresh my knowledge and use a different algorithm that was better. I had an idea and decided to play with spheres until the idea was solidified. So I went to NeHe and got lesson 18. and put it in a directory then did "make" and ran the executable generated. The moment I saw the error message I should have clicked with what was going on. Also you will note some very odd numbers coming up there. The BMP is correct in structure, so that is not the problem.

Tue Dec 15 05:06 AM$ ./lesson18 XF86VidModeExtension-Version 2.2 Got Doublebuffered Visual! glX-Version 1.2 Depth 24 Congrats, you have Direct Rendering! Data at Offset: 171798691894 Width of Bitmap: 64 Height of Bitmap: 1572865 Error: number of Planes not 1! Could not initialize OpenGL. Aborting...

The real problem has nothing to do with bit maps or planes. I has to do with understanding that "int" is not always the same size. It is the same size for each machine ( kind of, sort of, sometimes, okay, that is another whole subject. ) and it is the same size in the spec for BMP, but they are not always a match. So here is the code from NeHe, and my fixed code, which I always use. The code from NeHe is SO WRONG! , the logic is simply incorrect and lacks an appreciation for the variability of ints length in C. I could go on and on about this, but either people get it, or they don't. This is what I meant in an earlier post about some iffy code that floats about the internet. Hey "NeHe", give your code some TLC, it reflects poorly on you to publish code as if it is gospel. People look to sites like this to provide useful examples and this is FrankenInt scary!!

/* CODE THAT WORKS */ fseek(file, 18, SEEK_CUR); if ((i = fread(&image->sizeX, 4, 1, file)) != 1) { printf("Error reading width from %s.\n", filename);return 0;} /* READ 4* / if ((i = fread(&image->sizeY, 4, 1, file)) != 1) { printf("Error reading height from %s.\n", filename);return 0;} /*correct for 64 bit passed data if compiled for 64bit CPU*/ image->sizeX=image->sizeX&0xffff; image->sizeY=image->sizeY&0xffff; size = image->sizeX * image->sizeY * 3; /* calculate the size (assuming 24 bits or 3 bytes per pixel).*/ if ((fread(&planes, 2, 1, file)) != 1) { printf("Error reading planes from %s.\n", filename);return 0;} if (planes != 1) { printf("Planes from %s is not 1: %u\n", filename, planes);return 0;} if ((i = fread(&bpp, 2, 1, file)) != 1) { printf("Error reading bpp from %s.\n", filename);return 0;} if (bpp != 24) { printf("Bpp from %s is not 24: %u\n", filename, bpp);return 0;}

Oh the pain, that FrankenInt has imposed upon us all. The key in my code is that I read a specific number of bytes that matches the int size in the file, and compensate for the fact that it is short of the space. Now don't start talking about endedness, as I haven't had coffee yet and I could have a seizure. It isn't my real code as I modified it to highlight what is going on.

/* FUNKY FRANKENINT CODE */ /* get the file size */ /* skip file size and reserved fields of bitmap file header */ fseek(file, 8, SEEK_CUR); /* get the position of the actual bitmap data */ if (!fread(&bfOffBits, sizeof(long int), 1, file)) { printf("Error reading file!\n"); return 0; } printf("Data at Offset: %ld\n", bfOffBits); /* skip size of bitmap info header */ fseek(file, 4, SEEK_CUR); /* get the width of the bitmap */ fread(&texture->width, sizeof(int), 1, file); printf("Width of Bitmap: %d\n", texture->width); /* get the height of the bitmap */ fread(&texture->height, sizeof(int), 1, file); printf("Height of Bitmap: %d\n", texture->height); /* get the number of planes (must be set to 1) */ fread(&biPlanes, sizeof(short int), 1, file); if (biPlanes != 1) { printf("Error: number of Planes not 1!\n"); return 0; }

The error should read

printf("Error: knowledge incomplete, install C spec in my brain!\n");

I am bad today, and I will have breakfast and coffee, before I post next time. My code could be done a better way, and someday I will make it better. Later today I may get into the Franken(Ein)stein - Rosen - Podolsky problem.

0 comments:

Contributors

Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen