genericopenlibs/liboil/tsrc/examples/jpeg/src/test.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <sys/stat.h>
       
    19 #include <fcntl.h>
       
    20 #include <unistd.h>
       
    21 #include <stdlib.h>
       
    22 #include <stdio.h>
       
    23 #include "jpeg.h"
       
    24 
       
    25 #define LOG_FILE "c:\\logs\\examples_jpeg_log1.txt"
       
    26 #include "std_log_result.h"
       
    27 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    28 
       
    29 void create_xml(int result)
       
    30 {
       
    31     if(result)
       
    32         assert_failed = 1;
       
    33     
       
    34     testResultXml("examples_jpeg");
       
    35     close_log_file();
       
    36 }
       
    37 
       
    38 /* getfile */
       
    39 
       
    40 void *getfile (char *path, int *n_bytes);
       
    41 static void dump_pgm (unsigned char *ptr, int rowstride, int width, int height);
       
    42 
       
    43 
       
    44 int
       
    45 main (int argc, char *argv[])
       
    46 {
       
    47   unsigned char *data;
       
    48   int len;
       
    49   JpegDecoder *dec;
       
    50   char *fn = "c:\\data\\liboil\\test.jpg";
       
    51   unsigned char *ptr;
       
    52   int rowstride;
       
    53   int width;
       
    54   int height;
       
    55 
       
    56   /*if (argc < 2) {
       
    57         printf("jpeg_test <file.jpg>\n");
       
    58         exit(1);
       
    59   }*/
       
    60   if (argc > 1)
       
    61         fn = argv[1];
       
    62   
       
    63   std_log(LOG_FILENAME_LINE, "Test Started examples_jpeg");
       
    64   dec = jpeg_decoder_new ();    //to create decoder instance
       
    65   
       
    66   data = getfile (fn, &len);
       
    67 
       
    68   jpeg_decoder_addbits (dec, data, len);
       
    69   jpeg_decoder_decode (dec);
       
    70 
       
    71   jpeg_decoder_get_component_ptr (dec, 1, &ptr, &rowstride);
       
    72   jpeg_decoder_get_component_size (dec, 1, &width, &height);
       
    73 
       
    74   dump_pgm (ptr, rowstride, width, height);
       
    75   
       
    76   std_log(LOG_FILENAME_LINE, "Test Successful");
       
    77   create_xml(0);
       
    78   return 0;
       
    79 }
       
    80 
       
    81 
       
    82 /* getfile */
       
    83 
       
    84 void *
       
    85 getfile (char *path, int *n_bytes)
       
    86 {
       
    87   int fd;
       
    88   struct stat st;
       
    89   void *ptr = NULL;
       
    90   int ret;
       
    91 
       
    92   fd = open (path, O_RDONLY);
       
    93   if (!fd)
       
    94     return NULL;
       
    95 
       
    96   ret = fstat (fd, &st);
       
    97   if (ret < 0) {
       
    98     close (fd);
       
    99     return NULL;
       
   100   }
       
   101 
       
   102   ptr = malloc (st.st_size);
       
   103   if (!ptr) {
       
   104     close (fd);
       
   105     return NULL;
       
   106   }
       
   107 
       
   108   ret = read (fd, ptr, st.st_size);
       
   109   if (ret != st.st_size) {
       
   110     free (ptr);
       
   111     close (fd);
       
   112     return NULL;
       
   113   }
       
   114 
       
   115   if (n_bytes)
       
   116     *n_bytes = st.st_size;
       
   117 
       
   118   close (fd);
       
   119   return ptr;
       
   120 }
       
   121 
       
   122 static void
       
   123 dump_pgm (unsigned char *ptr, int rowstride, int width, int height)
       
   124 {
       
   125   int x, y;
       
   126 
       
   127   printf ("P2\n");
       
   128   printf ("%d %d\n", width, height);
       
   129   printf ("255\n");
       
   130 
       
   131   for (y = 0; y < height; y++) {
       
   132     for (x = 0; x < width; x++) {
       
   133       printf ("%d ", ptr[x]);
       
   134       if ((x & 15) == 15) {
       
   135         printf ("\n");
       
   136       }
       
   137     }
       
   138     printf ("\n");
       
   139     ptr += rowstride;
       
   140   }
       
   141 }