Add a --debug option to ply-image.

The new option is meant to make it easy to confirm placement of
images and to confirm the frame rate of animations.  It's primarily
meant to be used for debugging any scripts that display PNG assets,
although it could be useful in debugging ply-image as well.

BUG=None
TEST=run manually from shell prompt

Change-Id: I596aaeb6ab7082f1408a98e1f6fa3b48b2b8b58c
Reviewed-on: https://gerrit.chromium.org/gerrit/19008
Commit-Ready: Richard Barnette <jrbarnette@chromium.org>
Tested-by: Richard Barnette <jrbarnette@chromium.org>
Reviewed-by: Luigi Semenzato <semenzato@chromium.org>
diff --git a/src/ply-image.c b/src/ply-image.c
index f98901c..8bffdfa 100644
--- a/src/ply-image.c
+++ b/src/ply-image.c
@@ -247,6 +247,7 @@
   return image;
 }
 
+static int debug = 0;
 static bool center_image = true;
 static ply_frame_buffer_area_t image_location, image_offset;
 static uint32_t clear_color;
@@ -275,6 +276,10 @@
 
   area.x += image_offset.x;
   area.y += image_offset.y;
+  if (debug) {
+    fprintf(stderr, "%lux%lu image at (%ld, %ld) is %s\n",
+                    area.width, area.height, area.x, area.y, path);
+  }
   fill_succeeded = ply_frame_buffer_fill(buffer, &area, data);
 
   if (!fill_succeeded)
@@ -371,6 +376,11 @@
 
   clock_gettime(CLOCK_MONOTONIC, &last_time);
   while (argc > 0) {
+    if (debug) {
+      fprintf(stderr, "time now %ld.%06ld",
+                      last_time.tv_sec,
+                      (last_time.tv_nsec + 500) / 1000);
+    }
     if (sleep_ns > 0) {
       struct timespec sleep_time;
       sleep_time.tv_sec = sleep_ns / NS_PER_SEC;
@@ -380,6 +390,11 @@
       sleep_ns = 0;
     }
 
+    if (debug) {
+      fprintf(stderr, "; slept for %ld.%06ld\n",
+                      sleep_ns / NS_PER_SEC,
+                      (sleep_ns + 500) / 1000);
+    }
     ply_frame_buffer_show_file(buffer, argv[0]);
 
     clock_gettime(CLOCK_MONOTONIC, &curr_time);
@@ -395,6 +410,7 @@
 
 
 #define FLAG_CLEAR      'c'
+#define FLAG_DEBUG      'D'
 #define FLAG_FRAME      'f'
 #define FLAG_GAMMA      'g'
 #define FLAG_HELP       'h'
@@ -403,6 +419,7 @@
 
 static struct option command_options[] = {
   { "clear", required_argument, NULL, FLAG_CLEAR },
+  { "debug", no_argument, NULL, FLAG_DEBUG },
   { "frame-interval", required_argument, NULL, FLAG_FRAME },
   { "gamma", required_argument, NULL, FLAG_GAMMA },
   { "help", no_argument, NULL, FLAG_HELP },
@@ -436,6 +453,10 @@
       usage();
     }
 
+    if (c == FLAG_DEBUG) {
+      debug = 1;
+    }
+
     if (c == FLAG_CLEAR) {
       clear = 1;
       if (!parse_color(optarg)) {
@@ -487,6 +508,13 @@
     return errno;
   }
 
+  if (debug) {
+    ply_frame_buffer_area_t area;
+    ply_frame_buffer_get_size(buffer, &area);
+    fprintf(stderr, "screen dimensions:  %lux%lu\n",
+                    area.width, area.height);
+  }
+
   if (clear) {
     ply_frame_buffer_clear(buffer, clear_color);
   }