Add drawing grid
[yuv-player.git] / yuvPlayer.cpp
index fbd4536..fe7f45e 100644 (file)
@@ -26,6 +26,7 @@ class YuvPlayer
 public:
     YuvPlayer(int width, int height, const string& title = "YUV Player");
     void play(string fname, int fps = -1);
+    void toggleGrid();
     ~YuvPlayer();
 
 private:
@@ -37,8 +38,23 @@ private:
     SDL_Window *window;
     SDL_Renderer *renderer;
     SDL_Texture *texture;
-    long frameCnt = 0;
+    unsigned int frameCnt = 0;
     string title;
+    ifstream::pos_type last_pos;
+    bool isShowingGrid = false;
+    uint8_t *raw;
+};
+
+
+class ImageOperation {
+public:
+    ImageOperation(uint8_t *r, int width, int height);
+
+
+private:
+    int width;
+    int height;
+    uint8_t *raw;
 };
 
 YuvPlayer::YuvPlayer(int w, int h, const string& t)
@@ -66,12 +82,12 @@ YuvPlayer::~YuvPlayer()
 void YuvPlayer::play(string file, int fps)
 {
     size_t frame_size = width * height * 3 / 2;
-    uint8_t *raw = (uint8_t *) malloc(sizeof(uint8_t) * frame_size);
+    raw = (uint8_t *) malloc(sizeof(uint8_t) * frame_size);
     ifstream in (file, ios::binary);
     if (!in.is_open()) {
         cout << "failed to open " << file << endl;
     }
-    cout << "fps " << fps << endl;
+
 
     SDL_Event event;
     bool quit = false;
@@ -96,14 +112,24 @@ void YuvPlayer::play(string file, int fps)
                         quit = true;
                         break;
                     case SDLK_RIGHT:
-                        cout << "Right click" << endl;
                         if(readFrame(in, raw, frame_size)){
                             updateRenderer(raw);
                         }
                         break;
+                    case SDLK_LEFT:
+                        frameCnt = frameCnt >= 2 ? frameCnt - 2 : 0;
+                        in.seekg(frameCnt * frame_size);
+                        if(readFrame(in, raw, frame_size)){
+                            updateRenderer(raw);
+                        }
+
+                        break;
                     case SDLK_p:
                         isPlaying = !isPlaying;
                         break;
+                    case SDLK_g:
+                        toggleGrid();
+                        break;
                     default:
                         break;
                 }
@@ -114,15 +140,24 @@ void YuvPlayer::play(string file, int fps)
     free(raw);
 }
 
+void YuvPlayer::toggleGrid()
+{
+    isShowingGrid = !isShowingGrid;
+    updateRenderer(raw);
+}
+
+
 bool YuvPlayer::readFrame(ifstream& in, uint8_t *raw, size_t size)
 {
+    last_pos = in.tellg();
     bool ret = (bool) in.read(reinterpret_cast<char *>(raw), size);
     if (ret){
         frameCnt++;
         stringstream ss;
         ss << this->title << " [" << setw(4) << to_string(frameCnt) << "]";
-        cout << "read frame " << frameCnt << endl;
         SDL_SetWindowTitle(window, ss.str().c_str());
+    }else {
+        cout << "read error" << endl;
     }
     return ret;
 }
@@ -132,6 +167,18 @@ void YuvPlayer::updateRenderer(uint8_t *raw)
     SDL_UpdateTexture(texture, nullptr, raw, width * SDL_BYTESPERPIXEL(SDL_PIXELFORMAT_IYUV));
     SDL_RenderClear(renderer);
     SDL_RenderCopy(renderer, texture, nullptr, nullptr);
+
+    if(isShowingGrid){
+        SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0xFF, 0xFF );            
+        //SDL_RenderDrawLine( renderer, 0, height / 2, width, height / 2 );
+        SDL_RenderDrawLine( renderer, 0, height / 3, width, height / 3 );
+        SDL_RenderDrawLine( renderer, 0, height * 2 / 3, width, height * 2 / 3 );
+
+        SDL_RenderDrawLine( renderer, width / 3, 0, width / 3, height );
+        SDL_RenderDrawLine( renderer, width * 2 / 3, 0, width * 2 / 3, height );
+
+    }
+
     SDL_RenderPresent(renderer);
 }
 
@@ -140,4 +187,5 @@ int main(int argc, char* argv[])
     string fname = "out.yuv";
     YuvPlayer player(1280, 720);
     player.play(fname, 25);
+    //player.play(fname);
 }