KVM WORKING! ! !
diff --git a/src/getvideo_main.cpp b/src/getvideo_main.cpp
index 50889c9..4f375ca 100644
--- a/src/getvideo_main.cpp
+++ b/src/getvideo_main.cpp
@@ -9,120 +9,68 @@
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <stdio.h>
+#include <stdlib.h>
+#define BUILD_CIMG
+#ifdef BUILD_CIMG
+#define cimg_display 0
+#include <CImg.h>
+#endif
 
-namespace AstVideo {
-class VideoPuller {
- public:
-  VideoPuller() {}
-
-  void initialize() {
-    std::cout << "Opening /dev/video\n";
-    video_fd = open("/dev/video", O_RDWR);
-    if (!video_fd) {
-      std::cout << "Failed to open /dev/video\n";
-    } else {
-      std::cout << "Opened successfully\n";
-    }
-
-    std::vector<unsigned char> buffer(1024 * 1024, 0);
-
-    IMAGE_INFO image_info{};
-    image_info.do_image_refresh = 1;  // full frame refresh
-    image_info.qc_valid = 0;          // quick cursor disabled
-    image_info.parameter.features.w = 800;
-    image_info.parameter.features.h = 600;
-    image_info.parameter.features.chrom_tbl = 0;  // level
-    image_info.parameter.features.lumin_tbl = 0;
-    image_info.parameter.features.jpg_fmt = 1;
-    image_info.parameter.features.buf = buffer.data();
-    image_info.crypttype = -1;
-    std::cout << "Writing\n";
-    
-    int status;
-    
-    status = write(video_fd, reinterpret_cast<char*>(&image_info),
-                        sizeof(image_info));
-    if (status != 0) {
-      std::cout << "Write failed.  Return: " << status <<"\n";
-      perror("perror output:");
-    }
-    
-    std::cout << "Write done\n";
-    //std::this_thread::sleep_for(std::chrono::milliseconds(2000));
-    
-    std::cout << "Reading\n";
-    status = read(video_fd, reinterpret_cast<char*>(&image_info), sizeof(image_info));
-    std::cout << "Reading\n";
-
-    if (status != 0) {
-      std::cout << "Read failed with status " << status << "\n";
-    }
-
-    auto pt = reinterpret_cast<char*>(&image_info);
-
-    for (int i = 0; i < sizeof(image_info); i++) {
-      std::cout << std::hex << std::setfill('0') << std::setw(2)
-                << int(*(pt + i)) << " ";
-    }
-
-    std::cout << "\nprinting buffer\n";
-    
-    for(int i = 0; i < 512; i++){
-        if (i % 16 == 0){
-          std::cout << "\n";
-        }
-        std::cout << std::hex << std::setfill('0') << std::setw(2)
-            << int(buffer[i]) << " ";
-    }
-    
-    buffer.resize(image_info.len);
-    
-    std::ofstream f("/tmp/screen.jpg",std::ios::out | std::ios::binary); 
-
-    f.write(reinterpret_cast<char*>(buffer.data()), buffer.size());
-
-    std::cout << "\n";
-
-    std::cout << "typedef struct _video_features {\n";
-    std::cout << "short jpg_fmt: " << image_info.parameter.features.jpg_fmt
-              << "\n";
-    std::cout << "short lumin_tbl;" << image_info.parameter.features.lumin_tbl
-              << "\n";
-    std::cout << "short chrom_tbl;" << image_info.parameter.features.chrom_tbl
-              << "\n";
-    std::cout << "short tolerance_noise;"
-              << image_info.parameter.features.tolerance_noise << "\n";
-    std::cout << "int w; 0X" << image_info.parameter.features.w << "\n";
-    std::cout << "int h; 0X" << image_info.parameter.features.h << "\n";
-
-    std::cout << "void* buf; 0X" << static_cast<void*>(image_info.parameter.features.buf) << "\n";
-    // std::cout << "unsigned char *buf;" << image_info.parameter.features.buf
-    // << "\n";
-    std::cout << "} FEATURES_TAG;\n";
-
-    std::cout << "typedef struct _image_info {";
-    std::cout << "short do_image_refresh;" << image_info.do_image_refresh
-              << "\n";
-    std::cout << "char qc_valid;" << image_info.qc_valid << "\n";
-    std::cout << "unsigned int len;" << image_info.len << "\n";
-    std::cout << "int crypttype;" << image_info.crypttype << "\n";
-    std::cout << "char cryptkey[16];" << image_info.cryptkey << "\n";
-    std::cout << "union {\n";
-    std::cout << "    FEATURES_TAG features;\n";
-    std::cout << "    AST_CURSOR_TAG cursor_info;\n";
-    std::cout << "} parameter;\n";
-    std::cout << "} IMAGE_INFO;\n";
-    std::cout << std::endl;
-
-    close(video_fd);
-  }
-  int video_fd;
-};
-}
+#include <ast_jpeg_decoder.hpp>
+#include <ast_video_puller.hpp>
 
 int main() {
-  AstVideo::VideoPuller p;
-  p.initialize();
+  std::cout << "Started\n";
+  AstVideo::RawVideoBuffer out;
+  bool have_hardware = false;
+  if( access( "/dev/video", F_OK ) != -1 ) {
+    AstVideo::VideoPuller p;
+    p.initialize();
+    out = p.read_video();
+  } else {
+    FILE *fp = fopen("/home/ed/screendata.bin", "rb");
+    if (fp) {
+      size_t newLen = fread(out.buffer.data(), sizeof(char),
+                            out.buffer.size() * sizeof(long), fp);
+      if (ferror(fp) != 0) {
+        fputs("Error reading file", stderr);
+      }
+      fclose(fp);
+      out.buffer.resize(newLen);
+      out.mode = AstVideo::YuvMode::YUV444;
+      out.width = 800;
+      out.height = 600;
+      out.y_selector = 0;
+      out.uv_selector = 0;
+
+    }
+  }
+
+  FILE *fp = fopen("/tmp/screendata.bin", "wb");
+  fwrite(out.buffer.data(), sizeof(char),
+                            out.buffer.size(), fp);
+
+  AstVideo::AstJpegDecoder d;
+  std::cout << "MODE " << static_cast<int>(out.mode);
+  d.decode(out.buffer, out.width, out.height, out.mode, out.y_selector,
+           out.uv_selector);
+  #ifdef BUILD_CIMG
+  cimg_library::CImg<unsigned char> image(out.width, out.height, 1,
+                                          3 /*numchannels*/);
+  for (int y = 0; y < out.height; y++) {
+    for (int x = 0; x < out.width; x++) {
+      auto pixel = d.OutBuffer[x + (y * out.width)];
+      image(x, y, 0) = pixel.R;
+      image(x, y, 1) = pixel.G;
+      image(x, y, 2) = pixel.B;
+    }
+  }
+  image.save("/tmp/file2.bmp");
+  #endif
+  
+  std::cout << "Done!\n";
+
 
   return 1;
-}
\ No newline at end of file
+}