From b52413ad8380544c59fda6661af357c2e8018554 Mon Sep 17 00:00:00 2001
From: Orestis <orestis.malaspinas@pm.me>
Date: Mon, 28 Mar 2022 09:23:29 +0200
Subject: [PATCH] added lossless compression

---
 slides/cours_20.md                       | 149 ++++++++++++++++++++-
 slides/figs/quad_img_simple_comp.svg     | 161 +++++++++++++++++++++++
 slides/graphviz/quad_img_simple_comp.dot |  18 +++
 3 files changed, 323 insertions(+), 5 deletions(-)
 create mode 100644 slides/figs/quad_img_simple_comp.svg
 create mode 100644 slides/graphviz/quad_img_simple_comp.dot

diff --git a/slides/cours_20.md b/slides/cours_20.md
index 7ecd130..51aea18 100644
--- a/slides/cours_20.md
+++ b/slides/cours_20.md
@@ -642,12 +642,151 @@ arbre symétrie(arbre)
 . . .
 
 ```C
-arbre symétrie(arbre)
+rien rotation_gauche(arbre) 
+    si !est_feuille(arbre))
+        échange_cyclique_gauche(arbre.enfant)
+        pour i de 0 à 3
+            rotation_gauche(arbre.enfant[i])
+```
+
+# Rotation d'un quart de cercle
+
+\footnotesize
+
+## Comment faire sur un arbre?
+
+```
+   SG=0  |  SD=1        SG=0  |  SD=1   
+ 21 | 12 | 4 |  4      4 |  4 | 31 | 27
+  9 |  7 | 4 |  4      4 |  4 |  0 |  3
+-----------------  => ----------------- 
+  1 |  1 | 0 | 31     12 |  7 |  1 |  1
+  1 |  1 | 3 | 27     21 |  9 |  1 |  1
+   IG=2  |  ID=3        IG=2  |  ID=3
+```
+
+* Écrire le vrai (5min, matrix)
+
+. . .
+
+```C
+void rotate(node *qt) {
+    if (!is_leaf(qt)) {
+        node *tmp = qt->child[2];
+        qt->child[2] = qt->child[0];
+        qt->child[0] = qt->child[1];
+        qt->child[1] = qt->child[3];
+        qt->child[3] = tmp;
+        for (int i=0;i < 4; i++) { 
+            rotate(qt->child[i]);
+        }
+    }
+}
+```
+
+
+
+# Compression sans perte (1/N)
+
+## Idée générale
+
+* Regrouper les pixels par valeur
+
+```
+   SG=0  |  SD=1        SG=0   |  SD=1   
+ 21 | 12 | 4 |  4      21 | 12 |   4
+  9 |  7 | 4 |  4       9 |  7 |  
+-----------------  => ----------------- 
+  1 |  1 | 0 | 31         1    |  0 | 31
+  1 |  1 | 3 | 27              |  3 | 27
+   IG=2  |  ID=3        IG=2   |  ID=3
+```
+
+* Comment faire?
+
+# Compression sans perte (2/N)
+
+## Que devient l'arbre suivant?
+
+![](figs/quad_img_simple.svg)
+
+. . . 
+
+## Arbre compressé
+
+![](figs/quad_img_simple_comp.svg)
+
+# Compression sans perte (3/N)
+
+* Si un noeud a tous ses enfants égaux:
+    * Donner la valeur au noeud,
+    * Supprimer les enfants.
+* Remonter jusqu'à la racine.
+
+## Écrire le pseudo-code (5min, matrix)
+
+```C
+rien compression_sans_perte(arbre)
     si !est_feuille(arbre)
-        échanger(arbre.enfant[0], arbre.enfant[1])
-        échanger(arbre.enfant[2], arbre.enfant[3])
         pour i de 0 à 3
-            symétrie(arbre.enfant[i])
-    retourne arbre
+            compression_sans_perte(arbre.enfant[i])
+        si derniere_branche(arbre)
+            valeur, toutes_égales = valeur_enfants(arbre)
+            si toutes_egales
+                arbre.info = valeur
+                detruire_enfants(arbre)
+```
+
+# Compression sans perte (4/N)
+
+\footnotesize
+
+## Écrire le code C (5min, matrix)
+
+. . .
+
+```C
+void lossless_compression(node *qt) {
+    if (!is_leaf(qt)) {
+        for (int i = 0; i < CHILDREN; i++) {
+            lossless_compression(qt->child[i]);
+        }
+    }
+    if (is_last_branch(qt)) {
+        int val = -1;
+        if (last_value(qt, &val)) {
+            qt->info = val;
+            for (int i = 0; i < 4; ++i) {  
+                free(qt->child[i]);
+                qt->child[i] = NULL;
+            }
+        }
+    }
+}
 ```
 
+# Compression sans perte (5/N)
+
+\footnotesize
+
+```C
+bool is_last_branch(node *qt) {
+    for (int i = 0; i < 4; ++i) {
+        if (!is_leaf(qt)) {
+            return false;
+        }
+    }
+    return true;
+}
+bool last_value(node *qt, int *val) {
+    int info = qt->child[0];
+    for (int i = 1; i < 4; ++i) {
+        if (info != qt->child[i]) {
+            return false;
+        }
+    }
+    *val = info;
+    return true;
+}
+
+```
diff --git a/slides/figs/quad_img_simple_comp.svg b/slides/figs/quad_img_simple_comp.svg
new file mode 100644
index 0000000..8246f9e
--- /dev/null
+++ b/slides/figs/quad_img_simple_comp.svg
@@ -0,0 +1,161 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.40.1 (20161225.0304)
+ -->
+<!-- Title: G Pages: 1 -->
+<svg width="566pt" height="188pt" viewBox="0.00 0.00 566.00 188.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 184)">
+<title>G</title>
+<polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-184 562,-184 562,4 -4,4"/>
+<!-- 0 -->
+<g id="node1" class="node">
+<title>0</title>
+<ellipse fill="none" stroke="#000000" cx="279" cy="-162" rx="27" ry="18"/>
+<text text-anchor="middle" x="279" y="-157.8" font-family="Times,serif" font-size="14.00" fill="#000000"> </text>
+</g>
+<!-- sg1 -->
+<g id="node2" class="node">
+<title>sg1</title>
+<ellipse fill="none" stroke="#000000" cx="171" cy="-90" rx="27" ry="18"/>
+<text text-anchor="middle" x="171" y="-85.8" font-family="Times,serif" font-size="14.00" fill="#000000">SG</text>
+</g>
+<!-- 0&#45;&gt;sg1 -->
+<g id="edge1" class="edge">
+<title>0-&gt;sg1</title>
+<path fill="none" stroke="#000000" d="M259.6918,-149.1278C242.6445,-137.763 217.5981,-121.0654 198.4656,-108.3104"/>
+<polygon fill="#000000" stroke="#000000" points="200.4031,-105.3956 190.1411,-102.7607 196.5201,-111.2199 200.4031,-105.3956"/>
+</g>
+<!-- sd1 -->
+<g id="node3" class="node">
+<title>sd1</title>
+<ellipse fill="none" stroke="#000000" cx="243" cy="-90" rx="27" ry="18"/>
+<text text-anchor="middle" x="243" y="-85.8" font-family="Times,serif" font-size="14.00" fill="#000000">4</text>
+</g>
+<!-- 0&#45;&gt;sd1 -->
+<g id="edge2" class="edge">
+<title>0-&gt;sd1</title>
+<path fill="none" stroke="#000000" d="M270.2854,-144.5708C266.0403,-136.0807 260.8464,-125.6929 256.1337,-116.2674"/>
+<polygon fill="#000000" stroke="#000000" points="259.237,-114.6477 251.6343,-107.2687 252.976,-117.7782 259.237,-114.6477"/>
+</g>
+<!-- ig1 -->
+<g id="node4" class="node">
+<title>ig1</title>
+<ellipse fill="none" stroke="#000000" cx="315" cy="-90" rx="27" ry="18"/>
+<text text-anchor="middle" x="315" y="-85.8" font-family="Times,serif" font-size="14.00" fill="#000000">1</text>
+</g>
+<!-- 0&#45;&gt;ig1 -->
+<g id="edge3" class="edge">
+<title>0-&gt;ig1</title>
+<path fill="none" stroke="#000000" d="M287.7146,-144.5708C291.9597,-136.0807 297.1536,-125.6929 301.8663,-116.2674"/>
+<polygon fill="#000000" stroke="#000000" points="305.024,-117.7782 306.3657,-107.2687 298.763,-114.6477 305.024,-117.7782"/>
+</g>
+<!-- id1 -->
+<g id="node5" class="node">
+<title>id1</title>
+<ellipse fill="none" stroke="#000000" cx="387" cy="-90" rx="27" ry="18"/>
+<text text-anchor="middle" x="387" y="-85.8" font-family="Times,serif" font-size="14.00" fill="#000000">ID</text>
+</g>
+<!-- 0&#45;&gt;id1 -->
+<g id="edge4" class="edge">
+<title>0-&gt;id1</title>
+<path fill="none" stroke="#000000" d="M298.3082,-149.1278C315.3555,-137.763 340.4019,-121.0654 359.5344,-108.3104"/>
+<polygon fill="#000000" stroke="#000000" points="361.4799,-111.2199 367.8589,-102.7607 357.5969,-105.3956 361.4799,-111.2199"/>
+</g>
+<!-- sg2 -->
+<g id="node6" class="node">
+<title>sg2</title>
+<ellipse fill="none" stroke="#000000" cx="27" cy="-18" rx="27" ry="18"/>
+<text text-anchor="middle" x="27" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">21</text>
+</g>
+<!-- sg1&#45;&gt;sg2 -->
+<g id="edge5" class="edge">
+<title>sg1-&gt;sg2</title>
+<path fill="none" stroke="#000000" d="M149.1295,-79.0647C124.7778,-66.8889 85.238,-47.119 57.7715,-33.3858"/>
+<polygon fill="#000000" stroke="#000000" points="59.114,-30.1439 48.6045,-28.8022 55.9835,-36.4049 59.114,-30.1439"/>
+</g>
+<!-- sd2 -->
+<g id="node7" class="node">
+<title>sd2</title>
+<ellipse fill="none" stroke="#000000" cx="99" cy="-18" rx="27" ry="18"/>
+<text text-anchor="middle" x="99" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">12</text>
+</g>
+<!-- sg1&#45;&gt;sd2 -->
+<g id="edge6" class="edge">
+<title>sg1-&gt;sd2</title>
+<path fill="none" stroke="#000000" d="M155.7307,-74.7307C145.803,-64.803 132.6847,-51.6847 121.5637,-40.5637"/>
+<polygon fill="#000000" stroke="#000000" points="123.7933,-37.8436 114.2473,-33.2473 118.8436,-42.7933 123.7933,-37.8436"/>
+</g>
+<!-- ig2 -->
+<g id="node8" class="node">
+<title>ig2</title>
+<ellipse fill="none" stroke="#000000" cx="171" cy="-18" rx="27" ry="18"/>
+<text text-anchor="middle" x="171" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">9</text>
+</g>
+<!-- sg1&#45;&gt;ig2 -->
+<g id="edge7" class="edge">
+<title>sg1-&gt;ig2</title>
+<path fill="none" stroke="#000000" d="M171,-71.8314C171,-64.131 171,-54.9743 171,-46.4166"/>
+<polygon fill="#000000" stroke="#000000" points="174.5001,-46.4132 171,-36.4133 167.5001,-46.4133 174.5001,-46.4132"/>
+</g>
+<!-- id2 -->
+<g id="node9" class="node">
+<title>id2</title>
+<ellipse fill="none" stroke="#000000" cx="243" cy="-18" rx="27" ry="18"/>
+<text text-anchor="middle" x="243" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">7</text>
+</g>
+<!-- sg1&#45;&gt;id2 -->
+<g id="edge8" class="edge">
+<title>sg1-&gt;id2</title>
+<path fill="none" stroke="#000000" d="M186.2693,-74.7307C196.197,-64.803 209.3153,-51.6847 220.4363,-40.5637"/>
+<polygon fill="#000000" stroke="#000000" points="223.1564,-42.7933 227.7527,-33.2473 218.2067,-37.8436 223.1564,-42.7933"/>
+</g>
+<!-- sg5 -->
+<g id="node10" class="node">
+<title>sg5</title>
+<ellipse fill="none" stroke="#000000" cx="315" cy="-18" rx="27" ry="18"/>
+<text text-anchor="middle" x="315" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">0</text>
+</g>
+<!-- id1&#45;&gt;sg5 -->
+<g id="edge9" class="edge">
+<title>id1-&gt;sg5</title>
+<path fill="none" stroke="#000000" d="M371.7307,-74.7307C361.803,-64.803 348.6847,-51.6847 337.5637,-40.5637"/>
+<polygon fill="#000000" stroke="#000000" points="339.7933,-37.8436 330.2473,-33.2473 334.8436,-42.7933 339.7933,-37.8436"/>
+</g>
+<!-- sd5 -->
+<g id="node11" class="node">
+<title>sd5</title>
+<ellipse fill="none" stroke="#000000" cx="387" cy="-18" rx="27" ry="18"/>
+<text text-anchor="middle" x="387" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">31</text>
+</g>
+<!-- id1&#45;&gt;sd5 -->
+<g id="edge10" class="edge">
+<title>id1-&gt;sd5</title>
+<path fill="none" stroke="#000000" d="M387,-71.8314C387,-64.131 387,-54.9743 387,-46.4166"/>
+<polygon fill="#000000" stroke="#000000" points="390.5001,-46.4132 387,-36.4133 383.5001,-46.4133 390.5001,-46.4132"/>
+</g>
+<!-- ig5 -->
+<g id="node12" class="node">
+<title>ig5</title>
+<ellipse fill="none" stroke="#000000" cx="459" cy="-18" rx="27" ry="18"/>
+<text text-anchor="middle" x="459" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">3</text>
+</g>
+<!-- id1&#45;&gt;ig5 -->
+<g id="edge11" class="edge">
+<title>id1-&gt;ig5</title>
+<path fill="none" stroke="#000000" d="M402.2693,-74.7307C412.197,-64.803 425.3153,-51.6847 436.4363,-40.5637"/>
+<polygon fill="#000000" stroke="#000000" points="439.1564,-42.7933 443.7527,-33.2473 434.2067,-37.8436 439.1564,-42.7933"/>
+</g>
+<!-- id5 -->
+<g id="node13" class="node">
+<title>id5</title>
+<ellipse fill="none" stroke="#000000" cx="531" cy="-18" rx="27" ry="18"/>
+<text text-anchor="middle" x="531" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">27</text>
+</g>
+<!-- id1&#45;&gt;id5 -->
+<g id="edge12" class="edge">
+<title>id1-&gt;id5</title>
+<path fill="none" stroke="#000000" d="M408.8705,-79.0647C433.2222,-66.8889 472.762,-47.119 500.2285,-33.3858"/>
+<polygon fill="#000000" stroke="#000000" points="502.0165,-36.4049 509.3955,-28.8022 498.886,-30.1439 502.0165,-36.4049"/>
+</g>
+</g>
+</svg>
\ No newline at end of file
diff --git a/slides/graphviz/quad_img_simple_comp.dot b/slides/graphviz/quad_img_simple_comp.dot
new file mode 100644
index 0000000..9e8f467
--- /dev/null
+++ b/slides/graphviz/quad_img_simple_comp.dot
@@ -0,0 +1,18 @@
+digraph G {
+    0 [label=" "]
+    sg1 [label="SG"]
+    sd1 [label="4"]
+    ig1 [label="1"]
+    id1 [label="ID"]
+    sg2 [label="21"]
+    sd2 [label="12"]
+    ig2 [label="9"]
+    id2 [label="7"]
+    sg5 [label="0"]
+    sd5 [label="31"]
+    ig5 [label="3"]
+    id5 [label="27"]
+  0->{sg1, sd1, ig1, id1}  [quadtree = "normal"]
+  sg1->{sg2, sd2, ig2, id2}  [quadtree = "normal"]
+  id1->{sg5, sd5, ig5, id5}  [quadtree = "normal"]
+}
-- 
GitLab