Jpgsave

From RoutineWiki

Revision as of 09:36, 28 July 2008; view current revision
←Older revision | Newer revision→
Jump to: navigation, search

Contents

Credits and License details

The code of this routine is inspired on the reading jpg code from http://wiki.octave.org.

Author(s)

Maintainer(s)

The software provided in this page is maintained by:

Disclaimer

No warranty of any kind is implied for the uses of the source code provided in this page. It is provided 'as is'. Use at your own risk.

License

Released under the GNU Free Documentation License.

Other licenses

There are no other licenses.

Instalation

  • Copy the code from the section Code and save it in a file named ~/jpgsave.cc
  • Create a .oct file using the command:
mkoctfile ~/jpgsave.cc -ljpeg

This command will create the file jpgsave.oct in the same folder

  • Move the jpgsave.oct to the octave oct-site folder.

If you don't know where to place the .oct file, use the command

octave-config --oct-site-dir

that will show to you the appropriate place to move the .oct file.

  • Delete the jpgsave.cc

If you dont want to store the jpgsave.cc for future installs, you can now safety remove it.

Code

// FILE: jpgsave.cc

#include <octave/oct.h> 

extern "C" {
    #include "jpeglib.h"
} //extern "C"

DEFUN_DLD (jpgsave, args, nargout ,
   "JPGSAVE Save a JPEG file to disk. \n\
   jpgsave('filename',r,g,b) save the specified file\n")
{
    octave_value_list retval;
    int nargin  = args.length();

    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

    if (nargin != 4 || !args(0).is_string() || !args(1).is_real_matrix()
        || !args(2).is_real_matrix() || !args(3).is_real_matrix()) {
        print_usage ("jpgsave");
        return retval;
    }
    std::string filename = args(0).string_value();
    FILE * outfile = fopen(filename.c_str(), "wb");

    if (outfile == NULL) {
        error("Couldn't open file");
        return retval;
    }
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);
 
    unsigned long rows,cols;
    Matrix r = args(1).matrix_value();
    Matrix g = args(2).matrix_value();
    Matrix b = args(3).matrix_value();

    rows = r.rows();
    cols = r.columns();

    cinfo.image_width = cols;
    cinfo.image_height = rows;
    cinfo.input_components = 3;	/* # of color components per pixel */
    cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
    cinfo.dct_method = JDCT_FLOAT;
 
    jpeg_set_defaults(&cinfo);
    jpeg_set_colorspace(&cinfo, JCS_RGB);
    cinfo.mem->max_memory_to_use = 1000000000;
    jpeg_set_quality(&cinfo, 85, FALSE);
    
    jpeg_start_compress(&cinfo, TRUE);
 
    JSAMPROW row_pointer[1];	/* pointer to a single row */
    int row_stride;			/* physical row width in buffer */

    row_stride = cols * 3;	/* JSAMPLEs per row in image_buffer */

    row_pointer[0] = (JSAMPLE*)(*cinfo.mem->alloc_small)
        ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride*sizeof(JSAMPLE));
    while (cinfo.next_scanline < cinfo.image_height)
    {
        for(unsigned long i=0;i<cols;i++)
        {
            row_pointer[0][3*i]    = r(cinfo.next_scanline,i);
            row_pointer[0][3*i+1]  = g(cinfo.next_scanline,i);
            row_pointer[0][3*i+2]  = b(cinfo.next_scanline,i);
        }
        jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
    fclose(outfile);
    return retval;
};

Usage

Octave needs to be restarted after install the new jpgsave.oct file. After that, you can issue the command :

jpgsave('~/myimage.jpg',r,g,b)

for saving a color image file. Saving grayscale files is unimplemented. But we can use always a command like :

jpgsave('~/myimage.jpg',grayimage,grayimage,grayimage)

see also