PBM files are very easy to create and should suffice for your purpose. No need to use an external library.
A problem in your original code was that you were storing both the PBM metadata and the actual image data in the same dequeA
. Don't do that. Mixing data like this makes your program hard to understand and maintain.
You can create a valid PBM file by slightly adjusting the code writing the file:
ofstream output_file("./test.ppm");
// write file header
output_file << "P1\n" << (l+1) << " " << (h+1) << "\n";
//write image data
ostream_iterator<int> output_iterator(output_file, "\n");
copy(dequeA.begin(), dequeA.end(), output_iterator);
The resulting PPM file works fine with Gimp.
Writing to a BMP or TGA file works basically the same - you first write a header, followed by the actual data. The main difference is that those file formats are binary files, so the file I/O looks a bit different, and the header and image data formats are slightly more complicated. But still, both formats are quite simple and easy to write even without using a library.