File: test.c

package info (click to toggle)
liboil 0.3.15-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,032 kB
  • ctags: 5,929
  • sloc: ansic: 47,058; xml: 12,229; sh: 8,957; perl: 1,095; asm: 1,072; makefile: 748
file content (108 lines) | stat: -rw-r--r-- 1,713 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "jpeg.h"

/* getfile */

void *getfile (char *path, int *n_bytes);
static void dump_pgm (unsigned char *ptr, int rowstride, int width, int height);


int
main (int argc, char *argv[])
{
  unsigned char *data;
  int len;
  JpegDecoder *dec;
  char *fn = "biglebowski.jpg";
  unsigned char *ptr;
  int rowstride;
  int width;
  int height;

  dec = jpeg_decoder_new ();

  if (argc > 1)
    fn = argv[1];
  data = getfile (fn, &len);

  jpeg_decoder_addbits (dec, data, len);
  jpeg_decoder_decode (dec);

  jpeg_decoder_get_component_ptr (dec, 1, &ptr, &rowstride);
  jpeg_decoder_get_component_size (dec, 1, &width, &height);

  dump_pgm (ptr, rowstride, width, height);

  return 0;
}





/* getfile */

void *
getfile (char *path, int *n_bytes)
{
  int fd;
  struct stat st;
  void *ptr = NULL;
  int ret;

  fd = open (path, O_RDONLY);
  if (!fd)
    return NULL;

  ret = fstat (fd, &st);
  if (ret < 0) {
    close (fd);
    return NULL;
  }

  ptr = malloc (st.st_size);
  if (!ptr) {
    close (fd);
    return NULL;
  }

  ret = read (fd, ptr, st.st_size);
  if (ret != st.st_size) {
    free (ptr);
    close (fd);
    return NULL;
  }

  if (n_bytes)
    *n_bytes = st.st_size;

  close (fd);
  return ptr;
}

static void
dump_pgm (unsigned char *ptr, int rowstride, int width, int height)
{
  int x, y;

  printf ("P2\n");
  printf ("%d %d\n", width, height);
  printf ("255\n");

  for (y = 0; y < height; y++) {
    for (x = 0; x < width; x++) {
      printf ("%d ", ptr[x]);
      if ((x & 15) == 15) {
        printf ("\n");
      }
    }
    printf ("\n");
    ptr += rowstride;
  }
}