Refactor jpegtran.c to provide test interface

jpegtran.c contains a standalone program to transcode JPEG images. One
use of jpegtran is to generate input for libjpeg-turbo unit tests.

This commit refactors jpegtran.c to provide an interface to run the
jpegtran code programmatically. A gtest wrapper containing JPEG
transcoding unit tests will be introduced in a subsequent commit.

Bug: 993876
Change-Id: Iefede222d433e0914b3eb6dc1067684c0232f889
diff --git a/jpegtran.c b/jpegtran.c
index 058e844..55bdb4a 100644
--- a/jpegtran.c
+++ b/jpegtran.c
@@ -380,7 +380,11 @@
  */
 
 int
+#ifdef GTEST
+jpegtran(int argc, char **argv)
+#else
 main(int argc, char **argv)
+#endif
 {
   struct jpeg_decompress_struct srcinfo;
   struct jpeg_compress_struct dstinfo;
@@ -456,7 +460,7 @@
     if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {
       fprintf(stderr, "%s: can't open %s for reading\n", progname,
               argv[file_index]);
-      exit(EXIT_FAILURE);
+      return EXIT_FAILURE;
     }
   } else {
     /* default input file is stdin */
@@ -466,26 +470,26 @@
   if (icc_filename != NULL) {
     if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) {
       fprintf(stderr, "%s: can't open %s\n", progname, icc_filename);
-      exit(EXIT_FAILURE);
+      return EXIT_FAILURE;
     }
     if (fseek(icc_file, 0, SEEK_END) < 0 ||
         (icc_len = ftell(icc_file)) < 1 ||
         fseek(icc_file, 0, SEEK_SET) < 0) {
       fprintf(stderr, "%s: can't determine size of %s\n", progname,
               icc_filename);
-      exit(EXIT_FAILURE);
+      return EXIT_FAILURE;
     }
     if ((icc_profile = (JOCTET *)malloc(icc_len)) == NULL) {
       fprintf(stderr, "%s: can't allocate memory for ICC profile\n", progname);
       fclose(icc_file);
-      exit(EXIT_FAILURE);
+      return EXIT_FAILURE;
     }
     if (fread(icc_profile, icc_len, 1, icc_file) < 1) {
       fprintf(stderr, "%s: can't read ICC profile from %s\n", progname,
               icc_filename);
       free(icc_profile);
       fclose(icc_file);
-      exit(EXIT_FAILURE);
+      return EXIT_FAILURE;
     }
     fclose(icc_file);
     if (copyoption == JCOPYOPT_ALL)
@@ -513,7 +517,7 @@
    */
   if (!jtransform_request_workspace(&srcinfo, &transformoption)) {
     fprintf(stderr, "%s: transformation is not perfect\n", progname);
-    exit(EXIT_FAILURE);
+    return EXIT_FAILURE;
   }
 #endif
 
@@ -549,7 +553,7 @@
     if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {
       fprintf(stderr, "%s: can't open %s for writing\n", progname,
               outfilename);
-      exit(EXIT_FAILURE);
+      return EXIT_FAILURE;
     }
   } else {
     /* default output file is stdout */
@@ -595,7 +599,6 @@
     free(icc_profile);
 
   /* All done. */
-  exit(jsrcerr.num_warnings + jdsterr.num_warnings ?
-       EXIT_WARNING : EXIT_SUCCESS);
-  return 0;                     /* suppress no-return-value warnings */
+  return (jsrcerr.num_warnings + jdsterr.num_warnings ?
+          EXIT_WARNING : EXIT_SUCCESS);
 }