os/ossrv/genericopenlibs/liboil/tsrc/examples/jpeg/src/test_rgb.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 
    24 #include <liboil/liboil.h>
    25 #include "liboil/liboilcolorspace.h"
    26 #include "jpeg.h"
    27 
    28 #define LOG_FILE "c:\\logs\\examples_jpeg_rgb_log1.txt"
    29 #include "std_log_result.h"
    30 #define LOG_FILENAME_LINE __FILE__, __LINE__
    31 
    32 void create_xml(int result)
    33 {
    34     if(result)
    35         assert_failed = 1;
    36     
    37     testResultXml("examples_jpeg_rgb");
    38     close_log_file();
    39 }
    40 
    41 /* getfile */
    42 
    43 void *getfile (char *path, int *n_bytes);
    44 static void dump_pnm (uint32_t *ptr, int rowstride, int width, int height);
    45 
    46 int
    47 main (int argc, char *argv[])
    48 {
    49   unsigned char *data;
    50   int len;
    51   char *fn = "c:\\data\\liboil\\test.jpg";
    52   uint32_t *image = NULL;
    53   int width;
    54   int height;
    55   int ret;
    56   std_log(LOG_FILENAME_LINE, "Test Started examples_jpeg_rgb");
    57 
    58   /*if (argc < 2) {
    59     printf("jpeg_rgb_test <file.jpg>\n");
    60     std_log(LOG_FILENAME_LINE,"jpeg_rgb_test <file.jpg>");
    61     exit(1);
    62   }*/
    63   if(argc > 1){
    64       fn = argv[1];
    65   }
    66   std_log(LOG_FILENAME_LINE,"fn = %s",fn);
    67   data = getfile (fn, &len);
    68 
    69   if (data == NULL) {
    70     printf("cannot read file %s\n", fn);
    71     exit(1);
    72   }
    73 
    74   ret = jpeg_decode_argb (data, len, &image, &width, &height);
    75   if (ret) {
    76     dump_pnm (image, width*4, width, height);
    77   }
    78 
    79   if (image) free (image);
    80 
    81   free (data);
    82 
    83   std_log(LOG_FILENAME_LINE, "Test Successful");
    84   create_xml(0);
    85   return 0;
    86 }
    87 
    88 
    89 
    90 /* getfile */
    91 
    92 void *
    93 getfile (char *path, int *n_bytes)
    94 {
    95   int fd;
    96   struct stat st;
    97   void *ptr = NULL;
    98   int ret;
    99   std_log(LOG_FILENAME_LINE, "getfile ENTER");
   100   fd = open (path, O_RDONLY);
   101   if (!fd)
   102     return NULL;
   103 
   104   ret = fstat (fd, &st);
   105   if (ret < 0) {
   106     close (fd);
   107     return NULL;
   108   }
   109 
   110   ptr = malloc (st.st_size);
   111   if (!ptr) {
   112     close (fd);
   113     return NULL;
   114   }
   115 
   116   ret = read (fd, ptr, st.st_size);
   117   if (ret != st.st_size) {
   118     free (ptr);
   119     close (fd);
   120     return NULL;
   121   }
   122 
   123   if (n_bytes)
   124     *n_bytes = st.st_size;
   125 
   126   close (fd);
   127   std_log(LOG_FILENAME_LINE, "getfile EXIT");
   128   return ptr;
   129 }
   130 
   131 static void
   132 dump_pnm (uint32_t *ptr, int rowstride, int width, int height)
   133 {
   134   int x, y;
   135 
   136   printf ("P3\n");
   137   printf ("%d %d\n", width, height);
   138   printf ("255\n");
   139   std_log(LOG_FILENAME_LINE, "P3");
   140   std_log(LOG_FILENAME_LINE, "%d %d",width, height);
   141   std_log(LOG_FILENAME_LINE, "255");
   142 
   143   for (y = 0; y < height; y++) {
   144     for (x = 0; x < width; x++) {
   145       printf ("%d ", oil_argb_R(ptr[x]));
   146       printf ("%d ", oil_argb_G(ptr[x]));   //Extracts the green component from color.Evaluates to the green component
   147       printf ("%d ", oil_argb_B(ptr[x]));
   148       std_log(LOG_FILENAME_LINE, "%d ",oil_argb_R(ptr[x]));
   149       std_log(LOG_FILENAME_LINE, "%d ",oil_argb_G(ptr[x]));
   150       std_log(LOG_FILENAME_LINE, "%d ",oil_argb_B(ptr[x]));
   151       if ((x & 15) == 15) {
   152         printf ("\n");
   153       }
   154     }
   155     printf ("\n");
   156     ptr += rowstride/4;
   157   }
   158 }