If I'm reading the code correctly, it looks like a padding issue. When you are scaling up by any number that requires padding, here's what I see happening. You read in a line of pixels, process it, and write it out, and then go back and repeat this "L" times. However, the code that adds the padding is outside the loop that does this, causing the padding to only be added after the last line of each group. For example, say that you're scaling up by 3. You will read a line of pixels in, process it 3 times, exit the loop and add the padding to the end of the third line before going back to process the next line from the input file.
Restructure so that the padding is added to each line that is sent to the output file and it should work (assuming there are no other bugs).
If this answers your question, please accept this answer to remove your question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)