Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[07d29c]: jpgwrite.cc Maximize Restore History

Download this file

jpgwrite.cc    222 lines (191 with data), 5.9 kB

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// This is a hack into octave
// based on jpgwrite.c by jpgread by Drea Thomas, The Mathworks, and the
// examples in the IJG distribution.
//
// (C) 1998 Andy Adler. This code is in the public domain
// USE THIS CODE AT YOUR OWN RISK
//
// $Id$
//
#include <octave/oct.h>
#include <iostream>
#ifdef __cplusplus
extern "C" {
#endif
#include "jpeglib.h"
#ifdef __cplusplus
} //extern "C"
#endif
#define GRAYIMAGES
/*
* Simple jpeg writing MEX-file.
*
* Synopsis:
* jpgwrite(filename,r,g,b,quality)
*
* Compilation:
* First, try
* mkoctfile jpgwrite.cc -ljpeg
*
* If this doesn't work, then do
*
* Calls the jpeg library which is part of
* "The Independent JPEG Group's JPEG software" collection.
*
* The jpeg library came from,
*
* ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6.tar.gz
*/
DEFUN_DLD (jpgwrite, args, ,
"JPGWRITE Write a JPEG file to disk.\n\
jpgwrite('filename',R,G,B,quality) writes the specified file\n\
using the Red, Green, and Blue intensity matrices, at the given quality.\n\
\n\
jpgwrite('filename',M,quality) writes a grey-scale image.\n\
\n\
Data must be [0 255] or the high bytes will be lost\n\
\n\
If specified, quality should be in the range 1-100 and will default to \n\
75 if not specified. 100 is best quality, 1 is best compression.\n\
\n\
See also JPGREAD")
{
octave_value_list retval;
int nargin = args.length();
FILE * outfile;
JSAMPARRAY buffer;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
int quality=75; //default value
//
// We bail out if the input parameters are bad
//
if (nargin < 2 || !args(0).is_string() ) {
print_usage ("jpgwrite");
return retval;
}
//
// Open jpg file
//
std::string filename = args(0).string_value();
if ((outfile = fopen(filename.c_str(), "wb")) == NULL) {
error("Couldn't open file");
return retval;
}
//
// Set Jpeg parameters
//
if (nargin == 3) {
quality= (int) args(2).double_value();
} else if (nargin == 5) {
quality= (int) args(4).double_value();
}
//
// Initialize the jpeg library
// Read the jpg header to get info about size and color depth
//
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
//
// set parameters for compression
//
if ( nargin <= 3 ) {
//
// we're here because only one matrix of grey scale values was provided
//
Matrix avg= args(1).matrix_value();
long image_width = args(1).columns();
long image_height = args(1).rows();
cinfo.image_width = image_width; /* image width and height, in pixels */
cinfo.image_height = image_height;
#ifdef GRAYIMAGES
cinfo.input_components = 1;
cinfo.input_components = JCS_GRAYSCALE;
#else
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
#endif
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
//
// start compressor
//
jpeg_start_compress(&cinfo, TRUE);
//
// Allocate buffer for one scan line
//
long row_stride = image_width * cinfo.input_components ;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
//
// Now, loop thru each of the scanlines. For each, copy the image
// data from the buffer, data must be [0 255]
//
for( long j=0; cinfo.next_scanline < cinfo.image_height; j++) {
for(unsigned long i=0; i<cinfo.image_width; i++) {
#ifdef GRAYIMAGES
buffer[0][i] = (unsigned char) avg(j,i);
#else
buffer[0][i*3+0] = (unsigned char) avg(j,i);
buffer[0][i*3+1] = (unsigned char) avg(j,i);
buffer[0][i*3+2] = (unsigned char) avg(j,i);
#endif
}
jpeg_write_scanlines(&cinfo, buffer,1);
}
} // if nargin <= 3
else {
//
// we're here because red green and blue matrices were provided
// we assume that they're the same size
//
Matrix red = args(1).matrix_value();
Matrix green= args(2).matrix_value();
Matrix blue = args(3).matrix_value();
long image_width = args(1).columns();
long image_height = args(1).rows();
if ( args(2).columns() != image_width ||
args(3).columns() != image_width ||
args(2).rows() != image_height ||
args(3).rows() != image_height ) {
error("R,G,B matrix sizes aren't the same");
return retval;
}
cinfo.image_width = image_width; /* image width and height, in pixels */
cinfo.image_height = image_height;
cinfo.input_components = 3; /* # of color components per pixel */
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE );
//
// start compressor
//
jpeg_start_compress(&cinfo, TRUE);
//
// Allocate buffer for one scan line
//
long row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
//
// Now, loop thru each of the scanlines. For each, copy the image
// data from the buffer, data must be [0 255]
//
for( long j=0; cinfo.next_scanline < cinfo.image_height; j++) {
for(unsigned long i=0; i<cinfo.image_width; i++) {
buffer[0][i*3+0] = (unsigned char) red(j,i);
buffer[0][i*3+1] = (unsigned char) green(j,i);
buffer[0][i*3+2] = (unsigned char) blue(j,i);
}
jpeg_write_scanlines(&cinfo, buffer,1);
}
} // else nargin > 3
//
// Clean up
//
jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
return retval;
}