Skip to content
This repository
tree: 898ddf70dc
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 327 lines (249 sloc) 6.716 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
#include "ruby_vips.h"

/* :nodoc: */

static VALUE
writer_initialize(int argc, VALUE *argv, VALUE obj)
{
    VALUE image, opts;
rb_scan_args(argc, argv, "11", &image, &opts);
    GetImg(image, data, im);
    GetImg(obj, data_new, im_new);

    img_add_dep(data_new, image);
    if (im_copy(im, im_new))
        vips_lib_error();

    return obj;
}

/*
* call-seq:
* writer.image -> image
*
* Returns the image associated with *self*.
*/

static VALUE
writer_image(VALUE obj)
{
    GetImg(obj, data, im);

    if(data->deps)
        return data->deps[0];

    return Qnil;
}

static VALUE
writer_meta_set(VALUE obj, const char* name, VALUE str)
{
    GetImg(obj, data, im);

size_t len = RSTRING_LEN(str);
    void *buf = malloc(len);
    memcpy(buf, RSTRING_PTR(str), len);

    if (im_meta_set_blob(im, name, (im_callback_fn)xfree, buf, len)) {
        xfree(buf);
        vips_lib_error();
    }

    return str;
}

static VALUE
writer_meta_remove(VALUE obj, const char* name)
{
    GetImg(obj, data, im);
#if IM_MAJOR_VERSION > 7 || IM_MINOR_VERSION >= 22
if (im_meta_remove(im, name))
return Qfalse;
#else
    rb_raise(eVIPSError, "This method is not implemented in your version of VIPS");
#endif
    return Qtrue;
}

/*
* call-seq:
* writer.exif = exif_data
*
* Sets the exif header of the writer to <i>exif_data</i>. This will be
* written only if the file format supports embedded exif data.
*
*/

static VALUE
writer_exif_set(VALUE obj, VALUE str)
{
    return writer_meta_set(obj, IM_META_EXIF_NAME, str);
}

/*
* call-seq:
* writer.remove_exif -> true or false
*
* Removes exif data associated with *self*.
*/

static VALUE
writer_remove_exif(VALUE obj)
{
    return writer_meta_remove(obj, IM_META_EXIF_NAME);
}

/*
* call-seq:
* writer.icc = icc_data
*
* Sets the icc header of the writer to <i>icc_data</i>. This will be written
* only if the file format supports embedded icc data.
*
*/

static VALUE
writer_icc_set(VALUE obj, VALUE str)
{
    return writer_meta_set(obj, IM_META_ICC_NAME, str);
}

/*
* call-seq:
* writer.remove_icc -> true or false
*
* Removes icc data associated with *self*.
*/

static VALUE
writer_remove_icc(VALUE obj)
{
    return writer_meta_remove(obj, IM_META_ICC_NAME);
}

/* :nodoc: */

static VALUE
writer_write_internal(VALUE obj, VALUE path)
{
    VipsImage *out;
    GetImg(obj, data, im);

    if (!(out = im_open(StringValuePtr(path), "w")) || im_copy(im, out))
        vips_lib_error();

    im_close(out);

    return obj;
}

/* :nodoc: */

static VALUE
jpeg_buf_internal(VALUE obj, VALUE quality)
{
    char *buf = NULL;
    int length;

    GetImg(obj, data, im);

    if (im_vips2bufjpeg(im, NULL, NUM2INT(quality), &buf, &length))
        vips_lib_error();

    return rb_tainted_str_new(buf, length);
}

/* :nodoc: */

static VALUE
jpeg_write_internal(VALUE obj, VALUE path)
{
    GetImg(obj, data, im);

    if (im_vips2jpeg(im, RSTRING_PTR(path)))
        vips_lib_error();

    return obj;
}

/* :nodoc: */

static VALUE
tiff_write_internal(VALUE obj, VALUE path)
{
    GetImg(obj, data, im);

    if (im_vips2tiff(im, RSTRING_PTR(path)))
        vips_lib_error();

    return obj;
}

/* :nodoc: */

static VALUE
ppm_write_internal(VALUE obj, VALUE path)
{
    GetImg(obj, data, im);

    if (im_vips2ppm(im, RSTRING_PTR(path)))
        vips_lib_error();

    return obj;
}

/* :nodoc: */

static VALUE
png_buf_internal(VALUE obj, VALUE compression, VALUE interlace)
{
#if IM_MAJOR_VERSION > 7 || IM_MINOR_VERSION >= 23
    char *buf;
    size_t length;
    GetImg(obj, data, im);

    if (im_vips2bufpng(im, NULL, NUM2INT(compression), NUM2INT(interlace),
        &buf, &length))
        vips_lib_error();

    return rb_tainted_str_new(buf, length);
#else
    rb_raise(eVIPSError, "This method is not implemented in your version of VIPS");
#endif
}

/* :nodoc: */

static VALUE
png_write_internal(VALUE obj, VALUE path)
{
    GetImg(obj, data, im);

    if (im_vips2png(im, RSTRING_PTR(path)))
        vips_lib_error();

    return obj;
}

/* :nodoc: */

static VALUE
csv_write_internal(VALUE obj, VALUE path)
{
    GetImg(obj, data, im);

    if (im_vips2csv(im, RSTRING_PTR(path)))
        vips_lib_error();

    return obj;
}

/* :nodoc: */

static VALUE
vips_write_internal(VALUE obj, VALUE path)
{
    VipsImage *im_new;
    GetImg(obj, data, im);

    if (!(im_new = (VipsImage *)im_openout(RSTRING_PTR(path))))
        vips_lib_error();

    if (im_copy(im, im_new))
        vips_lib_error();

    im_close(im_new);

    return obj;
}

/*
* Base class for image format readers.
*/

void
init_Writer(void)
{
    VALUE writer = rb_define_class_under(mVIPS, "Writer", rb_cObject);

    rb_include_module(writer, mVIPSHeader);
    rb_define_alloc_func(writer, img_init_partial_anyclass);
    rb_define_method(writer, "initialize", writer_initialize, -1);
    rb_define_method(writer, "image", writer_image, 0);
    rb_define_method(writer, "exif=", writer_exif_set, 1);
    rb_define_method(writer, "remove_exif", writer_remove_exif, 0);
    rb_define_method(writer, "icc=", writer_icc_set, 1);
    rb_define_method(writer, "remove_icc", writer_remove_icc, 0);
    rb_define_private_method(writer, "write_internal", writer_write_internal, 1);

    /*
* Write JPEG images.
*/

    VALUE jpeg_writer = rb_define_class_under(mVIPS, "JPEGWriter", writer);
    rb_define_private_method(jpeg_writer, "buf_internal", jpeg_buf_internal, 1);
    rb_define_private_method(jpeg_writer, "write_internal", jpeg_write_internal, 1);

    /*
* Write TIFF images.
*/

    VALUE tiff_writer = rb_define_class_under(mVIPS, "TIFFWriter", writer);
    rb_define_private_method(tiff_writer, "write_internal", tiff_write_internal, 1);

    /*
* Write PPM images.
*/

    VALUE ppm_writer = rb_define_class_under(mVIPS, "PPMWriter", writer);
    rb_define_private_method(ppm_writer, "write_internal", ppm_write_internal, 1);

    /*
* Write PNG images.
*/

    VALUE png_writer = rb_define_class_under(mVIPS, "PNGWriter", writer);
    rb_define_private_method(png_writer, "write_internal", png_write_internal, 1);
    rb_define_private_method(png_writer, "buf_internal", png_buf_internal, 2);

    /*
* Write CSV images.
*/

    VALUE csv_writer = rb_define_class_under(mVIPS, "CSVWriter", writer);
    rb_define_private_method(csv_writer, "write_internal", csv_write_internal, 1);

    /*
* Write native VIPS images.
*/

    VALUE vips_writer = rb_define_class_under(mVIPS, "VIPSWriter", writer);
    rb_define_private_method(vips_writer, "write_internal", vips_write_internal, 1);

#if 0
    VALUE mVIPS = rb_define_module("VIPS");
#endif
}
Something went wrong with that request. Please try again.