Initial import
3 files changed, 80 insertions(+), 0 deletions(-)

A => README.rst
A => crop
A => overlay
A => README.rst +48 -0
@@ 0,0 1,48 @@ 
+============
+jpeg swapper
+============
+
+Swaps blocks of jpeg file. Or more like crop and overlay. Used to "descramble" image files obtained from certain online "book" readers.
+
+``crop``
+--------
+Given a directory with jpeg files (``<name>.jpg``), make a directory named ``<name>-split``, move the file there and create a bunch of files named ``<name>-<row>-<col>.jpg``.
+
+Edit the ``crop`` command to adjust number of rows and columns and its size. Hopefully in multiplication of 8.
+
+``overlay <mapping> <filename>``
+---------------------------------
+Run with ``<filename>`` pointing to previously moved file ``<name>.jpg`` in ``<name>-split`` directory, given a ``<mapping>`` file with content like this:
+
+::
+
+  src_row0-src_col0 dst_row0 dst_col0
+  src_row1-src_col1 dst_row1 dst_col1
+
+It will overlay the cropped files to a copy of source file named ``<name>-ok.jpg``.
+
+Misc
+----
+Clearly needs improvement:
+
+1. Swap in one command. And for one file instead of batch for the ``crop`` command.
+2. The command should accept block size and number or columns and rows.
+3. Create a mapping file generator. Ideally some GUI which accepts ``jpeg`` image, followed by entering number of columns and rows and size of the block. Or maybe automatically guess the blocks. Then allow user to swap around the image either by drag and drop or pair of clicks.
+
+License
+-------
+::
+
+  Copyright (c) 2012-2017 nanaya <me@myconan.net>
+
+  Permission to use, copy, modify, and distribute this software for any
+  purpose with or without fee is hereby granted, provided that the above
+  copyright notice and this permission notice appear in all copies.
+
+  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

          
A => crop +12 -0
@@ 0,0 1,12 @@ 
+#!/bin/sh
+
+for file in *.jpg; do
+  i="${file%%.*}"
+  mkdir -p "${i}-split"
+  mv "${i}.jpg" "${i}-split/"
+  for row in 0 1 2 3 4 5 6 7 8; do
+    for col in 0 1 2 3 4 5 6 7; do
+      jpegtran -crop "96x128+$((col * 96))+$((row * 128))" "${i}-split/${i}.jpg" > "${i}-split/${i}-${row}-${col}.jpg"
+    done
+  done
+done

          
A => overlay +20 -0
@@ 0,0 1,20 @@ 
+#!/usr/local/bin/bash
+
+set -e
+set -u
+
+[ "$#" = 2 ] || exit 1
+
+i="${2%%.*}"
+outfile="${i}-ok.jpg"
+cp "${2}" "${outfile}"
+
+grep -v ' $' "${1}" | while read map; do
+  source="${i}-${map:0:3}.jpg"
+  drop_x="${map:6:1}"
+  drop_y="${map:4:1}"
+  drop="+$((drop_x * 96))+$((drop_y * 128))"
+  echo "map: $map | source: $source | drop: $drop"
+  "${HOME}/apps/jpeg/bin/jpegtran" -drop "${drop}" "${source}" "${outfile}" > "${outfile}-temp"
+  mv "${outfile}-temp" "${outfile}"
+done