File: gr3_jpeg.c

package info (click to toggle)
gr-framework 0.73.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 11,600 kB
  • sloc: ansic: 87,413; cpp: 45,348; objc: 3,057; javascript: 2,647; makefile: 1,129; python: 1,000; sh: 991; yacc: 855; pascal: 554; fortran: 228
file content (60 lines) | stat: -rw-r--r-- 1,403 bytes parent folder | download
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
#include "gr3.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <jpeglib.h>
#include "gr3_internals.h"

int gr3_export_jpeg_(const char *filename, int width, int height)
{
  FILE *jpegfp;
  char *pixels;
  int err;

  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;

  jpegfp = fopen(filename, "wb");
  if (!jpegfp)
    {
      RETURN_ERROR(GR3_ERROR_CANNOT_OPEN_FILE);
    }

  pixels = (char *)malloc(width * height * 3);
  if (!pixels)
    {
      RETURN_ERROR(GR3_ERROR_OUT_OF_MEM);
    }

  err = gr3_getimage(width, height, FALSE, pixels);
  if (err != GR3_ERROR_NONE)
    {
      fclose(jpegfp);
      free(pixels);
      return err;
    }

  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);
  jpeg_stdio_dest(&cinfo, jpegfp);

  cinfo.image_width = width;
  cinfo.image_height = height;
  cinfo.input_components = 3;
  cinfo.in_color_space = JCS_RGB;
  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, 100, TRUE);
  jpeg_start_compress(&cinfo, TRUE);
  while (cinfo.next_scanline < cinfo.image_height)
    {
      JSAMPROW row_pointer[1];
      row_pointer[0] = (JSAMPLE *)(pixels + 3 * (height - cinfo.next_scanline - 1) * width);
      jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }
  jpeg_finish_compress(&cinfo);
  jpeg_destroy_compress(&cinfo);
  fclose(jpegfp);
  free(pixels);
  return GR3_ERROR_NONE;
}