summaryrefslogtreecommitdiff
path: root/lib/modules/swfbits.c
blob: 05b21fb671222d62264fa4985f40fd84aac2824d (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* swfbits.c

   Bitmap functions (needs libjpeg) 

   Extension module for the rfxswf library.
   Part of the swftools package.

   Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
 
   This file is distributed under the GPL, see file COPYING for details 

*/

#ifdef _JPEGLIB_INCLUDED_
#define OUTBUFFER_SIZE 32768

typedef struct _JPEGDESTMGR
{ struct jpeg_destination_mgr mgr;
  LPTAG  t;
  JOCTET * buffer;
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
} JPEGDESTMGR, * LPJPEGDESTMGR;

// Destination manager callbacks

void swf_init_destination(j_compress_ptr cinfo) 
{ LPJPEGDESTMGR dmgr = (LPJPEGDESTMGR)cinfo->dest;
  dmgr->buffer = (JOCTET*)malloc(OUTBUFFER_SIZE);
  dmgr->mgr.next_output_byte = dmgr->buffer;
  dmgr->mgr.free_in_buffer = OUTBUFFER_SIZE;
}

boolean swf_empty_output_buffer(j_compress_ptr cinfo)
{ LPJPEGDESTMGR dmgr = (LPJPEGDESTMGR)cinfo->dest;
  SetBlock(dmgr->t,
           (U8*)dmgr->buffer,
           OUTBUFFER_SIZE-dmgr->mgr.free_in_buffer);
  dmgr->mgr.next_output_byte = dmgr->buffer;
  dmgr->mgr.free_in_buffer = OUTBUFFER_SIZE;
}

void swf_term_destination(j_compress_ptr cinfo) 
{ LPJPEGDESTMGR dmgr = (LPJPEGDESTMGR)cinfo->dest;
  swf_empty_output_buffer(cinfo);
  free(dmgr->buffer);
  dmgr->mgr.free_in_buffer = 0;
}

LPJPEGBITS SetJPEGBitsStart(LPTAG t,int width,int height,int quality)
{
  LPJPEGDESTMGR jpeg;
        
  // redirect compression lib output to local SWF Tag structure
  
  jpeg = (LPJPEGDESTMGR)malloc(sizeof(JPEGDESTMGR));
  if (!jpeg) return NULL;
  
  memset(jpeg,0x00,sizeof(JPEGDESTMGR));
  jpeg->cinfo.err = jpeg_std_error(&jpeg->jerr);

  jpeg_create_compress(&jpeg->cinfo);

  jpeg->mgr.init_destination =  swf_init_destination;
  jpeg->mgr.empty_output_buffer =       swf_empty_output_buffer;
  jpeg->mgr.term_destination =  swf_term_destination;
      
  jpeg->t = t;

  jpeg->cinfo.dest = (struct jpeg_destination_mgr *)jpeg;

  // init compression
  
  jpeg->cinfo.image_width  = width;
  jpeg->cinfo.image_height = height;
  jpeg->cinfo.input_components = 3;
  jpeg->cinfo.in_color_space = JCS_RGB;

  jpeg_set_defaults(&jpeg->cinfo);
  jpeg_set_quality(&jpeg->cinfo,quality,TRUE);

  // write tables to SWF
  
  jpeg_write_tables(&jpeg->cinfo);

  // compess image to SWF
   
  jpeg_suppress_tables(&jpeg->cinfo, TRUE);
  jpeg_start_compress(&jpeg->cinfo, FALSE);

  return (LPJPEGBITS)jpeg;
}

int SetJPEGBitsLines(LPJPEGBITS jpegbits,U8 ** data,int n)
{ LPJPEGDESTMGR jpeg = (LPJPEGDESTMGR)jpegbits;
  if (!jpeg) return -1;
  jpeg_write_scanlines(&jpeg->cinfo,data,n);
  return 0;
}

int SetJPEGBitsLine(LPJPEGBITS jpegbits,U8 * data)
{ return SetJPEGBitsLines(jpegbits,&data,1);
}

int SetJPEGBitsFinish(LPJPEGBITS jpegbits)
{ LPJPEGDESTMGR jpeg = (LPJPEGDESTMGR)jpegbits;
  if (!jpeg) return -1;
  jpeg_finish_compress(&jpeg->cinfo);
  free(jpeg);
  return 0;
}

int SetJPEGBits(LPTAG t,char * fname,int quality)
{ struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;
  LPJPEGBITS out;
  FILE * f;
  U8 * scanline;
  
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_decompress(&cinfo); 

  if ((f=fopen(fname,"rb"))==NULL) return -1;
  

  jpeg_stdio_src(&cinfo,f);
  jpeg_read_header(&cinfo, TRUE);
  jpeg_start_decompress(&cinfo);

  out = SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
  scanline = (U8*)malloc(4*cinfo.output_width);
  
  if (scanline)
  { int y;
    U8 * js = scanline;
    for (y=0;y<cinfo.output_height;y++)
    { jpeg_read_scanlines(&cinfo,&js,1);
      SetJPEGBitsLines(out,(U8**)&js,1);
    }
  }

  SetJPEGBitsFinish(out);
  jpeg_finish_decompress(&cinfo);
  fclose(f);
  
  return 0;
}

#undef OUTBUFFER_SIZE
#endif

// insert zlib/PNG functions here