       
       (update-rgba)
       (overlay-triangles)
-      (cv/update-p-image)))
+      (update-drawn-p-image)))
 
 (defn draw [state]
   (let [{:keys [p-image frame-mat]} state]

                         color
                         glyph-pts]
  (let [poly (MatOfPoint.)
        c (apply #(let [r %1
                        g %2
                        b %3
                        a %4]
                    (Scalar. b g r))
                 color)]
    (.fromList poly (ArrayList. glyph-pts))
    (Core/fillPoly img-mat (ArrayList. [poly]) c)))

(defn draw-mosaic-pair
  [triangle-glyphs frame-mat rgba-mat [pt1 pt2]]
  (let [color-fn (fn [mat-col mat-row]
                   (let [c (.get rgba-mat
                                 (+ mat-row MOSAIC-BIN-SIZE-2)

        [mat-col2 mat-row2] pt2
        c1 (color-fn mat-col1 mat-row1)
        c2 (color-fn mat-col2 mat-row2)]
    (draw-mosaic-glyph drawn-mat c1 (triangle-glyphs pt1))
    (draw-mosaic-glyph drawn-mat c2 (triangle-glyphs pt2))))

(defn overlay-triangles
  [{:keys [drawn-mat rgba-mat triangle-points triangle-glyphs] :as state}]
  (dorun
   (map (partial draw-mosaic-pair
                 triangle-glyphs
                 drawn-mat
                 rgba-mat)
        (partition 2 triangle-points)))
  state)

(defn rgba-mat->p-img
  "Input mat is really BGR, but we'll flip these bits in
   any-mat->p-img. If you don't use that, swap blue and red."
  [in-mat out-mat b-array i-array p-img]
  (cv/any-mat->p-img in-mat out-mat Imgproc/COLOR_RGBA2BGRA b-array i-array p-img))

(defn update-drawn-p-image [state]
  (let [{:keys [drawn-mat output-mat b-array i-array]} state]
    (update-in state [:p-image] #(cv/mat->p-img drawn-mat output-mat b-array i-array %))))

(defn update [state]
  (-> state
