os/ossrv/genericopenlibs/liboil/tsrc/examples/jpeg/src/test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/liboil/tsrc/examples/jpeg/src/test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,141 @@
     1.4 +/*
     1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +#include <sys/stat.h>
    1.22 +#include <fcntl.h>
    1.23 +#include <unistd.h>
    1.24 +#include <stdlib.h>
    1.25 +#include <stdio.h>
    1.26 +#include "jpeg.h"
    1.27 +
    1.28 +#define LOG_FILE "c:\\logs\\examples_jpeg_log1.txt"
    1.29 +#include "std_log_result.h"
    1.30 +#define LOG_FILENAME_LINE __FILE__, __LINE__
    1.31 +
    1.32 +void create_xml(int result)
    1.33 +{
    1.34 +    if(result)
    1.35 +        assert_failed = 1;
    1.36 +    
    1.37 +    testResultXml("examples_jpeg");
    1.38 +    close_log_file();
    1.39 +}
    1.40 +
    1.41 +/* getfile */
    1.42 +
    1.43 +void *getfile (char *path, int *n_bytes);
    1.44 +static void dump_pgm (unsigned char *ptr, int rowstride, int width, int height);
    1.45 +
    1.46 +
    1.47 +int
    1.48 +main (int argc, char *argv[])
    1.49 +{
    1.50 +  unsigned char *data;
    1.51 +  int len;
    1.52 +  JpegDecoder *dec;
    1.53 +  char *fn = "c:\\data\\liboil\\test.jpg";
    1.54 +  unsigned char *ptr;
    1.55 +  int rowstride;
    1.56 +  int width;
    1.57 +  int height;
    1.58 +
    1.59 +  /*if (argc < 2) {
    1.60 +        printf("jpeg_test <file.jpg>\n");
    1.61 +        exit(1);
    1.62 +  }*/
    1.63 +  if (argc > 1)
    1.64 +        fn = argv[1];
    1.65 +  
    1.66 +  std_log(LOG_FILENAME_LINE, "Test Started examples_jpeg");
    1.67 +  dec = jpeg_decoder_new ();    //to create decoder instance
    1.68 +  
    1.69 +  data = getfile (fn, &len);
    1.70 +
    1.71 +  jpeg_decoder_addbits (dec, data, len);
    1.72 +  jpeg_decoder_decode (dec);
    1.73 +
    1.74 +  jpeg_decoder_get_component_ptr (dec, 1, &ptr, &rowstride);
    1.75 +  jpeg_decoder_get_component_size (dec, 1, &width, &height);
    1.76 +
    1.77 +  dump_pgm (ptr, rowstride, width, height);
    1.78 +  
    1.79 +  std_log(LOG_FILENAME_LINE, "Test Successful");
    1.80 +  create_xml(0);
    1.81 +  return 0;
    1.82 +}
    1.83 +
    1.84 +
    1.85 +/* getfile */
    1.86 +
    1.87 +void *
    1.88 +getfile (char *path, int *n_bytes)
    1.89 +{
    1.90 +  int fd;
    1.91 +  struct stat st;
    1.92 +  void *ptr = NULL;
    1.93 +  int ret;
    1.94 +
    1.95 +  fd = open (path, O_RDONLY);
    1.96 +  if (!fd)
    1.97 +    return NULL;
    1.98 +
    1.99 +  ret = fstat (fd, &st);
   1.100 +  if (ret < 0) {
   1.101 +    close (fd);
   1.102 +    return NULL;
   1.103 +  }
   1.104 +
   1.105 +  ptr = malloc (st.st_size);
   1.106 +  if (!ptr) {
   1.107 +    close (fd);
   1.108 +    return NULL;
   1.109 +  }
   1.110 +
   1.111 +  ret = read (fd, ptr, st.st_size);
   1.112 +  if (ret != st.st_size) {
   1.113 +    free (ptr);
   1.114 +    close (fd);
   1.115 +    return NULL;
   1.116 +  }
   1.117 +
   1.118 +  if (n_bytes)
   1.119 +    *n_bytes = st.st_size;
   1.120 +
   1.121 +  close (fd);
   1.122 +  return ptr;
   1.123 +}
   1.124 +
   1.125 +static void
   1.126 +dump_pgm (unsigned char *ptr, int rowstride, int width, int height)
   1.127 +{
   1.128 +  int x, y;
   1.129 +
   1.130 +  printf ("P2\n");
   1.131 +  printf ("%d %d\n", width, height);
   1.132 +  printf ("255\n");
   1.133 +
   1.134 +  for (y = 0; y < height; y++) {
   1.135 +    for (x = 0; x < width; x++) {
   1.136 +      printf ("%d ", ptr[x]);
   1.137 +      if ((x & 15) == 15) {
   1.138 +        printf ("\n");
   1.139 +      }
   1.140 +    }
   1.141 +    printf ("\n");
   1.142 +    ptr += rowstride;
   1.143 +  }
   1.144 +}