I have an an application where I would like to perform lossless swapping of tiles in a JPEG, resulting in a file such as this: To do so, I have been able to make a proof-of-concept python script that repeatedly calls the patched jpegtran with crop and drop support like so:
# Export tiles
for i, swap in enumerate(swaps):
if i > 0:
continue
commanda = [
jtp,
"-crop", "{}x{}+{}+{}".format(dividerWidth, dividerHeight, swap["sx"], swap["sy"]),
"-outfile", "tmp/s{}s.jpg".format(i),
"original.jpg"
]
commandb = [
jtp,
"-crop", "{}x{}+{}+{}".format(dividerWidth, dividerHeight, swap["dx"], swap["dy"]),
"-outfile", "tmp/s{}d.jpg".format(i),
"original.jpg"
]
print(list2cmdline(commanda))
call(commanda)
print(list2cmdline(commandb))
call(commandb)
# Then drop them on the original
for i, swap in enumerate(swaps):
command = [jtp]
if i == len(swaps) - 1:
command.append("-optimize")
source = "mixed.jpg"
if i == 0:
source = "original.jpg"
commanda = command.copy()
commanda.extend([
"-drop", "+{}+{}".format(swap["dx"], swap["dy"]),
"tmp/s{}s.jpg".format(i),
"-outfile", "mixed.jpg",
source
])
commandb = command.copy()
commandb.extend([
"-drop", "+{}+{}".format(swap["sx"], swap["sy"]),
"tmp/s{}d.jpg".format(i),
"-outfile", "mixed.jpg",
"mixed.jpg"
])
print(list2cmdline(commanda))
call(commanda)
print(list2cmdline(commandb))
call(commandb)
This is, however, very inefficient and slow. I started trying to make a version inspired by the source code of jpegtrans (patched vers.) to no avail. I do not understand how I take the cropped image (in a jpeg_compress_struct) after executing (jtransform_execute_transformation) and then dropping it on the image. I seems like I could do this with just one copy of the entire image in memory, and crop out parts and drop them in other places, but I have no idea how to go about this.