mirror of
				https://github.com/ruby-opencv/ruby-opencv
				synced 2023-03-27 23:22:12 -04:00 
			
		
		
		
	change CvMat#save_image to set format-specific save parameters
This commit is contained in:
		
							parent
							
								
									020ba99fc1
								
							
						
					
					
						commit
						b63e197680
					
				
					 3 changed files with 47 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -413,7 +413,7 @@ void define_ruby_class()
 | 
			
		|||
 | 
			
		||||
  rb_define_method(rb_klass, "extract_surf", RUBY_METHOD_FUNC(rb_extract_surf), -1);
 | 
			
		||||
 | 
			
		||||
  rb_define_method(rb_klass, "save_image", RUBY_METHOD_FUNC(rb_save_image), 1);
 | 
			
		||||
  rb_define_method(rb_klass, "save_image", RUBY_METHOD_FUNC(rb_save_image), -1);
 | 
			
		||||
  rb_define_alias(rb_klass, "save", "save_image");
 | 
			
		||||
 | 
			
		||||
  rb_define_method(rb_klass, "encode_image", RUBY_METHOD_FUNC(rb_encode_imageM), -1);
 | 
			
		||||
| 
						 | 
				
			
			@ -1496,7 +1496,7 @@ rb_set_bang(int argc, VALUE *argv, VALUE self)
 | 
			
		|||
 | 
			
		||||
/*
 | 
			
		||||
 * call-seq:
 | 
			
		||||
 *   save_image(<i>filename</i>) -> self
 | 
			
		||||
 *   save_image(<i>filename, params = nil</i>) -> self
 | 
			
		||||
 *
 | 
			
		||||
 * Saves an image to file. The image format is chosen depending on the filename extension.
 | 
			
		||||
 * Only 8bit single-channel or 3-channel(with 'BGR' channel order) image can be saved.
 | 
			
		||||
| 
						 | 
				
			
			@ -1507,15 +1507,31 @@ rb_set_bang(int argc, VALUE *argv, VALUE self)
 | 
			
		|||
 *   image.save_image("image.png") #=> save as PNG format
 | 
			
		||||
 */
 | 
			
		||||
VALUE
 | 
			
		||||
rb_save_image(VALUE self, VALUE filename)
 | 
			
		||||
rb_save_image(int argc, VALUE *argv, VALUE self)
 | 
			
		||||
{
 | 
			
		||||
  Check_Type(filename, T_STRING);
 | 
			
		||||
  VALUE _filename, _params;
 | 
			
		||||
  rb_scan_args(argc, argv, "11", &_filename, &_params);
 | 
			
		||||
  Check_Type(_filename, T_STRING);
 | 
			
		||||
  int *params = NULL;
 | 
			
		||||
  if (!NIL_P(_params)) {
 | 
			
		||||
    params = hash_to_format_specific_param(_params);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  try {
 | 
			
		||||
    cvSaveImage(StringValueCStr(filename), CVARR(self));
 | 
			
		||||
    cvSaveImage(StringValueCStr(_filename), CVARR(self), params);
 | 
			
		||||
  }
 | 
			
		||||
  catch (cv::Exception& e) {
 | 
			
		||||
    if (params != NULL) {
 | 
			
		||||
      free(params);
 | 
			
		||||
      params = NULL;
 | 
			
		||||
    }
 | 
			
		||||
    raise_cverror(e);
 | 
			
		||||
  }
 | 
			
		||||
  if (params != NULL) {
 | 
			
		||||
    free(params);
 | 
			
		||||
    params = NULL;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -240,7 +240,7 @@ VALUE rb_compute_correspond_epilines(VALUE klass, VALUE points, VALUE which_imag
 | 
			
		|||
VALUE rb_extract_surf(int argc, VALUE *argv, VALUE self);
 | 
			
		||||
 | 
			
		||||
// HighGUI function
 | 
			
		||||
VALUE rb_save_image(VALUE self, VALUE filename);
 | 
			
		||||
VALUE rb_save_image(int argc, VALUE *argv, VALUE self);
 | 
			
		||||
 | 
			
		||||
VALUE new_object(int rows, int cols, int type);
 | 
			
		||||
VALUE new_object(CvSize size, int type);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -88,19 +88,36 @@ class TestCvMat < OpenCVTestCase
 | 
			
		|||
  end
 | 
			
		||||
 | 
			
		||||
  def test_save_image
 | 
			
		||||
    filename = 'save_image_test.jpg'
 | 
			
		||||
    filename_jpg = 'save_image_test.jpg'
 | 
			
		||||
    filename_png = 'save_image_test.png'
 | 
			
		||||
    m = CvMat.new(20, 20, :cv8u, 1)
 | 
			
		||||
    
 | 
			
		||||
    File.delete filename if File.exists? filename
 | 
			
		||||
    m.save_image filename
 | 
			
		||||
    assert(File.exists? filename)
 | 
			
		||||
    File.delete filename_jpg if File.exists? filename_jpg
 | 
			
		||||
    m.save_image filename_jpg
 | 
			
		||||
    assert(File.exists? filename_jpg)
 | 
			
		||||
 | 
			
		||||
    File.delete filename_jpg if File.exists? filename_jpg
 | 
			
		||||
    m.save_image(filename_jpg, CV_IMWRITE_JPEG_QUALITY => 10)
 | 
			
		||||
    assert(File.exists? filename_jpg)
 | 
			
		||||
 | 
			
		||||
    File.delete filename_png if File.exists? filename_png
 | 
			
		||||
    m.save_image(filename_png, CV_IMWRITE_PNG_COMPRESSION => 9)
 | 
			
		||||
    assert(File.exists? filename_png)
 | 
			
		||||
 | 
			
		||||
    # Alias
 | 
			
		||||
    File.delete filename if File.exists? filename
 | 
			
		||||
    m.save filename
 | 
			
		||||
    assert(File.exists? filename)
 | 
			
		||||
    File.delete filename_jpg if File.exists? filename_jpg
 | 
			
		||||
    m.save filename_jpg
 | 
			
		||||
    assert(File.exists? filename_jpg)
 | 
			
		||||
    
 | 
			
		||||
    File.delete filename
 | 
			
		||||
    assert_raise(TypeError) {
 | 
			
		||||
      m.save_image(DUMMY_OBJ)
 | 
			
		||||
    }
 | 
			
		||||
    assert_raise(TypeError) {
 | 
			
		||||
      m.save_image(filename_jpg, DUMMY_OBJ)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    File.delete filename_jpg if File.exists? filename_jpg
 | 
			
		||||
    File.delete filename_png if File.exists? filename_png
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_encode
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue