{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f90c8291",
   "metadata": {},
   "source": [
    "# TP-4: Image Compression\n",
    "\n",
    "## 1. Prediction-Based Coding (Lossless)\n",
    "\n",
    "Based on lecture pages 10-13, prediction coding reduces image entropy by coding differences\n",
    "instead of absolute pixel values.\n",
    "\n",
    "### Objective\n",
    "\n",
    "Students implement simple 1-D and 2-D predictors and compute the prediction error image.\n",
    "\n",
    "### Tasks\n",
    "\n",
    "1. Load a grayscale image.\n",
    "\n",
    "2. Predict each pixel using one of the predictors from the slides (page 10):\n",
    "\n",
    "   - Predictor $A$: Left neighbor\n",
    "   - Predictor $C$: Upper neighbor\n",
    "   - Predictor ($A + B - C$), etc.\n",
    "\n",
    "3. Compute the difference/error image:\n",
    "\n",
    "   - $\\text{error} = \\text{original} - \\text{predicted}$\n",
    "\n",
    "4. Display:\n",
    "   - original image\n",
    "   - predicted image\n",
    "   - error image\n",
    "\n",
    "### Python functions to use\n",
    "\n",
    "- cv2.imread()\n",
    "- numpy.roll() or direct indexing\n",
    "- Display using matplotlib.pyplot.imshow(\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a9bf3cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# ==========================\n",
    "# 1. Load grayscale image\n",
    "# ==========================\n",
    "# Change this to your actual path\n",
    "image_path = \"image.jpg\"\n",
    "\n",
    "# Read as grayscale\n",
    "orig = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n",
    "if orig is None:\n",
    "    raise FileNotFoundError(f\"Could not load image at path: {image_path}\")\n",
    "\n",
    "orig = orig.astype(np.int16)  # use int16 to avoid negative overflow in error\n",
    "\n",
    "H, W = orig.shape\n",
    "\n",
    "# ===================================\n",
    "# 2. Build neighbor images A, B, C\n",
    "#    A = left, B = upper-left, C = up\n",
    "# ===================================\n",
    "# roll shifts, we fix borders manually\n",
    "A = np.roll(orig, shift=1, axis=1)  # left neighbor\n",
    "C = np.roll(orig, shift=1, axis=0)  # upper neighbor\n",
    "B = np.roll(C, shift=1, axis=1)  # upper-left (shift left from C)\n",
    "\n",
    "# Fix first column for A and B (no left neighbor)\n",
    "A[:, 0] = orig[:, 0]\n",
    "B[:, 0] = C[:, 0]\n",
    "\n",
    "# Fix first row for C and B (no upper neighbor)\n",
    "C[0, :] = orig[0, :]\n",
    "B[0, :] = orig[0, :]\n",
    "\n",
    "# ===================================\n",
    "# Choose a predictor\n",
    "# Uncomment ONE of these blocks\n",
    "# ===================================\n",
    "\n",
    "# --- Predictor A: left neighbor ---\n",
    "pred_A = A.copy()\n",
    "\n",
    "# --- Predictor C: upper neighbor ---\n",
    "pred_C = C.copy()\n",
    "\n",
    "# --- Predictor A + B - C (2D) ---\n",
    "pred_ABC = A + B - C\n",
    "\n",
    "# Clip to valid pixel range [0, 255] just for visualization\n",
    "pred_A_vis = np.clip(pred_A, 0, 255).astype(np.uint8)\n",
    "pred_C_vis = np.clip(pred_C, 0, 255).astype(np.uint8)\n",
    "pred_ABC_vis = np.clip(pred_ABC, 0, 255).astype(np.uint8)\n",
    "\n",
    "# ===================================\n",
    "# 3. Compute error image\n",
    "#    error = original - predicted\n",
    "# ===================================\n",
    "error_A = orig - pred_A\n",
    "error_C = orig - pred_C\n",
    "error_ABC = orig - pred_ABC\n",
    "\n",
    "# For visualization: shift error to be visible\n",
    "# (e.g., add 128 so that zero-error is mid gray)\n",
    "error_A_vis = np.clip(error_A + 128, 0, 255).astype(np.uint8)\n",
    "error_C_vis = np.clip(error_C + 128, 0, 255).astype(np.uint8)\n",
    "error_ABC_vis = np.clip(error_ABC + 128, 0, 255).astype(np.uint8)\n",
    "\n",
    "# ===================================\n",
    "# 4. Display original, predicted, error\n",
    "# ===================================\n",
    "\n",
    "plt.figure(figsize=(12, 9))\n",
    "\n",
    "plt.subplot(3, 3, 1)\n",
    "plt.title(\"Original\")\n",
    "plt.imshow(orig.astype(np.uint8), cmap=\"gray\")\n",
    "plt.axis(\"off\")\n",
    "\n",
    "plt.subplot(3, 3, 2)\n",
    "plt.title(\"Predicted (A = left)\")\n",
    "plt.imshow(pred_A_vis, cmap=\"gray\")\n",
    "plt.axis(\"off\")\n",
    "\n",
    "plt.subplot(3, 3, 3)\n",
    "plt.title(\"Error (A)\")\n",
    "plt.imshow(error_A_vis, cmap=\"gray\")\n",
    "plt.axis(\"off\")\n",
    "\n",
    "plt.subplot(3, 3, 4)\n",
    "plt.title(\"Predicted (C = up)\")\n",
    "plt.imshow(pred_C_vis, cmap=\"gray\")\n",
    "plt.axis(\"off\")\n",
    "\n",
    "plt.subplot(3, 3, 5)\n",
    "plt.title(\"Error (C)\")\n",
    "plt.imshow(error_C_vis, cmap=\"gray\")\n",
    "plt.axis(\"off\")\n",
    "\n",
    "plt.subplot(3, 3, 6)\n",
    "plt.title(\"Predicted (A+B-C)\")\n",
    "plt.imshow(pred_ABC_vis, cmap=\"gray\")\n",
    "plt.axis(\"off\")\n",
    "\n",
    "plt.subplot(3, 3, 7)\n",
    "plt.title(\"Error (A+B-C)\")\n",
    "plt.imshow(error_ABC_vis, cmap=\"gray\")\n",
    "plt.axis(\"off\")\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
