fix segfault when setting gamma table

ply-image assumes that CRTC 1 (hardcoded in kInternalPanel const )is the
internal panel. it is not true when running under QEMU since there is
only one CRTC under QEMU and count_crtcs in resources struct is 1, so
using index 1 uses bad array location. I added a check that will use
index 0 instead of kInternalPanel if kInternalPanel is out of bounds.

TEST=run ply-image --gamma /usr/share/color/bin/internal_display.bin \
--clear 0xfefefe

Change-Id: I600f8cafcc99a25c126398fc9aaa986077101f8b
Signed-off-by: Dominik Behr <dbehr@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/171445
Reviewed-by: Stéphane Marchesin <marcheu@chromium.org>
diff --git a/src/ply-gamma.c b/src/ply-gamma.c
index be47e9a..1f138d0 100644
--- a/src/ply-gamma.c
+++ b/src/ply-gamma.c
@@ -77,6 +77,7 @@
 
 bool ply_gamma_set(const char* file) {
   int r;
+  int crtc;
   drmModeCrtcPtr mode;
   GammaRamp* ramp;
 
@@ -100,7 +101,8 @@
     return false;
   }
 
-  mode = drmModeGetCrtc(fd, resources->crtcs[kInternalPanel]);
+  crtc = (kInternalPanel < resources->count_crtcs) ? kInternalPanel : 0;
+  mode = drmModeGetCrtc(fd, resources->crtcs[crtc]);
   r = drmModeCrtcSetGamma(fd,
                            mode->crtc_id,
                            mode->gamma_size,