In Photoshop, you use layers, and just add the flag image to the layer above the table, then using the TRANSFORM DISTORT tool, you drag each corner of the flag to the corresponding corner of the table, making the flag image match the perspective of the table, without distorting the table image...