Refactoring a TGA image parser
The 128 case can be optimized as:

    if (packetType == 128) 
    { 
        // packet stores number of times following pixel is repeated 
        repeat = (packetHdr &amp; ~(1 &lt;&lt; 7)) + 1; 
        bytes = inFile.ReadBytes(bytesPerPixel);
        for (int j = 0; j &lt; repeat; j++) 
        {
            bytes.CopyTo(pixData, count);
            count += bytes.Length;
        }
    } 


The 0 case can be optimized as:

    if (packetType == 0) 
    { 
        // packet stores number of pixels that follow 
        repeat = ((packetHdr &amp; ~(1 &lt;&lt; 7)) + 1) * bytesPerPixel;
        bytes = inFile.ReadBytes(repeat);
        bytes.CopyTo(pixData, count);
        count += bytes.Length;
    }