For this file nColors = 0
The original program is only writing one byte where it should be writing 1024. That's why the original program gave an output file that was 1023 bytes shorter than the original file. In fact, with this file, there is a 1024 byte color table (gray ramp) between the end of the header(s) and the beginning of the pixel bytes.
I'm not talking about how it is supposed to be (nColors = 256, I'm thinking), I am talking about the specific file that was attached. It is apparently defective.
This is the answer to the original question about why some files weren't converted properly: it's a bad file. But with a little detective work it can be salvaged. Some viewers work one way, some work another. For example, Firefox could open and display the original file OK, but not Microsoft's whatever.
With swoopy's fix and a proper file, I'm thinking that the program should be OK (after the superfluous pixel array is deleted, of course).
Edit
(As a matter of fact, I just replaced the nColors value with the proper value in the original file and now all is just swell--- with the loop fix swoopy posted.)
Bottom line: the output file size should be exactly the same size as the input file. All file contents other than pixel data should match exactly.
D