]> git.imager.perl.org - imager.git/blobdiff - Imager.xs
- added experimental EXIF decoding when reading JPEG files.
[imager.git] / Imager.xs
index 9884e6849d077a132e27b39ef07f646d3dbf26d0..da982a9b2c62b8fca4160e200fafac5b1c3f10e9 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -9,26 +9,22 @@ extern "C" {
 
 #endif
 
+#define i_int_hlines_testing() 1
+
 #include "image.h"
 #include "feat.h"
 #include "dynaload.h"
 #include "regmach.h"
 
+#if i_int_hlines_testing()
+#include "imagei.h"
+#endif
+
 typedef io_glue* Imager__IO;
 typedef i_color* Imager__Color;
 typedef i_fcolor* Imager__Color__Float;
 typedef i_img*   Imager__ImgRaw;
-
-/* later perls define this macro to prevent warning when converting
-from IV to pointer types */
-
-#ifndef INT2PTR
-#define INT2PTR(type,value) (type)(value)
-#endif
-
-#ifndef PTR2IV
-#define PTR2IV(p) INT2PTR(IV,p)
-#endif
+typedef int undef_neg_int;
 
 #ifdef HAVE_LIBTT
 typedef TT_Fonthandle* Imager__Font__TT;
@@ -423,7 +419,6 @@ static ssize_t io_reader(void *p, void *data, size_t size) {
   struct cbdata *cbd = p;
   ssize_t total;
   char *out = data; /* so we can do pointer arithmetic */
-  int i;
 
   if (cbd->writing) {
     if (write_flush(cbd) <= 0)
@@ -505,6 +500,7 @@ static void io_destroyer(void *p) {
   SvREFCNT_dec(cbd->readcb);
   SvREFCNT_dec(cbd->seekcb);
   SvREFCNT_dec(cbd->closecb);
+  myfree(cbd);
 }
 
 struct value_name {
@@ -569,30 +565,6 @@ static struct value_name orddith_names[] =
   { "custom", od_custom, },
 };
 
-static int
-hv_fetch_bool(HV *hv, char *name, int def) {
-  SV **sv;
-
-  sv = hv_fetch(hv, name, strlen(name), 0);
-  if (sv && *sv) {
-    return SvTRUE(*sv);
-  }
-  else
-    return def;
-}
-
-static int
-hv_fetch_int(HV *hv, char *name, int def) {
-  SV **sv;
-
-  sv = hv_fetch(hv, name, strlen(name), 0);
-  if (sv && *sv) {
-    return SvIV(*sv);
-  }
-  else
-    return def;
-}
-
 /* look through the hash for quantization options */
 static void handle_quant_opts(i_quantize *quant, HV *hv)
 {
@@ -731,90 +703,6 @@ static void cleanup_quant_opts(i_quantize *quant) {
     myfree(quant->ed_map);
 }
 
-#if 0
-/* look through the hash for options to add to opts */
-static void handle_gif_opts(i_gif_opts *opts, HV *hv)
-{
-  SV **sv;
-  int i;
-  /**((char *)0) = '\0';*/
-  opts->each_palette = hv_fetch_bool(hv, "gif_each_palette", 0);
-  opts->interlace = hv_fetch_bool(hv, "interlace", 0);
-
-  sv = hv_fetch(hv, "gif_delays", 10, 0);
-  if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
-    AV *av = (AV*)SvRV(*sv);
-    opts->delay_count = av_len(av)+1;
-    opts->delays = mymalloc(sizeof(int) * opts->delay_count);
-    for (i = 0; i < opts->delay_count; ++i) {
-      SV *sv1 = *av_fetch(av, i, 0);
-      opts->delays[i] = SvIV(sv1);
-    }
-  }
-  sv = hv_fetch(hv, "gif_user_input", 14, 0);
-  if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
-    AV *av = (AV*)SvRV(*sv);
-    opts->user_input_count = av_len(av)+1;
-    opts->user_input_flags = mymalloc(opts->user_input_count);
-    for (i = 0; i < opts->user_input_count; ++i) {
-      SV *sv1 = *av_fetch(av, i, 0);
-      opts->user_input_flags[i] = SvIV(sv1) != 0;
-    }
-  }
-  sv = hv_fetch(hv, "gif_disposal", 12, 0);
-  if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
-    AV *av = (AV*)SvRV(*sv);
-    opts->disposal_count = av_len(av)+1;
-    opts->disposal = mymalloc(opts->disposal_count);
-    for (i = 0; i < opts->disposal_count; ++i) {
-      SV *sv1 = *av_fetch(av, i, 0);
-      opts->disposal[i] = SvIV(sv1);
-    }
-  }
-  sv = hv_fetch(hv, "gif_tran_color", 14, 0);
-  if (sv && *sv && SvROK(*sv) && sv_derived_from(*sv, "Imager::Color")) {
-    i_color *col = INT2PTR(i_color *, SvIV((SV *)SvRV(*sv)));
-    opts->tran_color = *col;
-  }
-  sv = hv_fetch(hv, "gif_positions", 13, 0);
-  if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
-    AV *av = (AV *)SvRV(*sv);
-    opts->position_count = av_len(av) + 1;
-    opts->positions = mymalloc(sizeof(i_gif_pos) * opts->position_count);
-    for (i = 0; i < opts->position_count; ++i) {
-      SV **sv2 = av_fetch(av, i, 0);
-      opts->positions[i].x = opts->positions[i].y = 0;
-      if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
-       AV *av2 = (AV*)SvRV(*sv2);
-       SV **sv3;
-       sv3 = av_fetch(av2, 0, 0);
-       if (sv3 && *sv3)
-         opts->positions[i].x = SvIV(*sv3);
-       sv3 = av_fetch(av2, 1, 0);
-       if (sv3 && *sv3)
-         opts->positions[i].y = SvIV(*sv3);
-      }
-    }
-  }
-  /* Netscape2.0 loop count extension */
-  opts->loop_count = hv_fetch_int(hv, "gif_loop_count", 0);
-
-  opts->eliminate_unused = hv_fetch_bool(hv, "gif_eliminate_unused", 1);
-}
-
-static void cleanup_gif_opts(i_gif_opts *opts) {
-  if (opts->delays)
-    myfree(opts->delays);
-  if (opts->user_input_flags)
-    myfree(opts->user_input_flags);
-  if (opts->disposal)
-    myfree(opts->disposal);
-  if (opts->positions) 
-    myfree(opts->positions);
-}
-
-#endif
-
 /* copies the color map from the hv into the colors member of the HV */
 static void copy_colors_back(HV *hv, i_quantize *quant) {
   SV **sv;
@@ -856,7 +744,6 @@ load_fount_segs(AV *asegs, int *count) {
   */
   int i, j;
   AV *aseg;
-  SV *sv;
   i_fountain_seg *segs;
   double work[3];
   int worki[2];
@@ -940,6 +827,73 @@ typedef i_fill_t* Imager__FillHandle;
     potential naming conflicts */
 #define init_log m_init_log
 
+#if i_int_hlines_testing()
+
+typedef i_int_hlines *Imager__Internal__Hlines;
+
+static i_int_hlines *
+i_int_hlines_new(int start_y, int count_y, int start_x, int count_x) {
+  i_int_hlines *result = mymalloc(sizeof(i_int_hlines));
+  i_int_init_hlines(result, start_y, count_y, start_x, count_x);
+
+  return result;
+}
+
+static i_int_hlines *
+i_int_hlines_new_img(i_img *im) {
+  i_int_hlines *result = mymalloc(sizeof(i_int_hlines));
+  i_int_init_hlines_img(result, im);
+
+  return result;
+}
+
+static void
+i_int_hlines_DESTROY(i_int_hlines *hlines) {
+  i_int_hlines_destroy(hlines);
+  myfree(hlines);
+}
+
+static int seg_compare(const void *vleft, const void *vright) {
+  const i_int_hline_seg *left = vleft;
+  const i_int_hline_seg *right = vright;
+
+  return left->minx - right->minx;
+}
+
+static SV *
+i_int_hlines_dump(i_int_hlines *hlines) {
+  SV *dump = newSVpvf("start_y: %d limit_y: %d start_x: %d limit_x: %d\n",
+       hlines->start_y, hlines->limit_y, hlines->start_x, hlines->limit_x);
+  int y;
+  
+  for (y = hlines->start_y; y < hlines->limit_y; ++y) {
+    i_int_hline_entry *entry = hlines->entries[y-hlines->start_y];
+    if (entry) {
+      int i;
+      /* sort the segments, if any */
+      if (entry->count)
+        qsort(entry->segs, entry->count, sizeof(i_int_hline_seg), seg_compare);
+
+      sv_catpvf(dump, " %d (%d):", y, entry->count);
+      for (i = 0; i < entry->count; ++i) {
+        sv_catpvf(dump, " [%d, %d)", entry->segs[i].minx, 
+                  entry->segs[i].x_limit);
+      }
+      sv_catpv(dump, "\n");
+    }
+  }
+
+  return dump;
+}
+
+#endif
+
+#ifdef IMEXIF_ENABLE
+#define i_exif_enabled() 1
+#else
+#define i_exif_enabled() 0
+#endif
+
 MODULE = Imager                PACKAGE = Imager::Color PREFIX = ICL_
 
 Imager::Color
@@ -1062,7 +1016,6 @@ i_rgb_to_hsv(c)
         i_rgb_to_hsvf(RETVAL);
       OUTPUT:
         RETVAL
-        
 
 MODULE = Imager                PACKAGE = Imager::ImgRaw        PREFIX = IIM_
 
@@ -1096,7 +1049,6 @@ io_new_buffer(data)
          char   *data
        PREINIT:
          size_t length;
-         SV* sv;
        CODE:
          SvPV(ST(0), length);
           SvREFCNT_inc(ST(0));
@@ -1146,6 +1098,24 @@ io_slurp(ig)
               myfree(data);
 
 
+undef_int
+i_set_image_file_limits(width, height, bytes)
+       int width
+       int height
+       int bytes
+
+void
+i_get_image_file_limits()
+      PREINIT:
+        int width, height, bytes;
+      PPCODE:
+        if (i_get_image_file_limits(&width, &height, &bytes)) {
+         EXTEND(SP, 3);
+          PUSHs(sv_2mortal(newSViv(width)));
+          PUSHs(sv_2mortal(newSViv(height)));
+          PUSHs(sv_2mortal(newSViv(bytes)));
+        }
+
 MODULE = Imager                PACKAGE = Imager::IO    PREFIX = io_glue_
 
 void
@@ -1191,6 +1161,19 @@ i_img_empty_ch(im,x,y,ch)
               int     y
               int     ch
 
+Imager::ImgRaw
+i_sametype(im, x, y)
+    Imager::ImgRaw im
+               int x
+               int y
+
+Imager::ImgRaw
+i_sametype_chans(im, x, y, channels)
+    Imager::ImgRaw im
+               int x
+               int y
+               int channels
+
 void
 m_init_log(name,level)
              char*    name
@@ -1306,6 +1289,16 @@ i_arc(im,x,y,rad,d1,d2,val)
              float     d2
           Imager::Color    val
 
+void
+i_arc_aa(im,x,y,rad,d1,d2,val)
+    Imager::ImgRaw     im
+           double     x
+           double     y
+            double     rad
+            double     d1
+            double     d2
+          Imager::Color    val
+
 void
 i_arc_cfill(im,x,y,rad,d1,d2,fill)
     Imager::ImgRaw     im
@@ -1316,6 +1309,15 @@ i_arc_cfill(im,x,y,rad,d1,d2,fill)
              float     d2
           Imager::FillHandle    fill
 
+void
+i_arc_aa_cfill(im,x,y,rad,d1,d2,fill)
+    Imager::ImgRaw     im
+           double     x
+           double     y
+            double     rad
+            double     d1
+            double     d2
+          Imager::FillHandle   fill
 
 
 void
@@ -1500,12 +1502,34 @@ i_rotate90(im, degrees)
                int      degrees
 
 Imager::ImgRaw
-i_rotate_exact(im, amount)
+i_rotate_exact(im, amount, ...)
     Imager::ImgRaw      im
             double      amount
+      PREINIT:
+       i_color *backp = NULL;
+       i_fcolor *fbackp = NULL;
+       int i;
+       SV * sv1;
+      CODE:
+       /* extract the bg colors if any */
+       /* yes, this is kind of strange */
+       for (i = 2; i < items; ++i) {
+          sv1 = ST(i);
+          if (sv_derived_from(sv1, "Imager::Color")) {
+           IV tmp = SvIV((SV*)SvRV(sv1));
+           backp = INT2PTR(i_color *, tmp);
+         }
+         else if (sv_derived_from(sv1, "Imager::Color::Float")) {
+           IV tmp = SvIV((SV*)SvRV(sv1));
+           fbackp = INT2PTR(i_fcolor *, tmp);
+         }
+       }
+       RETVAL = i_rotate_exact_bg(im, amount, backp, fbackp);
+      OUTPUT:
+       RETVAL
 
 Imager::ImgRaw
-i_matrix_transform(im, xsize, ysize, matrix)
+i_matrix_transform(im, xsize, ysize, matrix, ...)
     Imager::ImgRaw      im
                int      xsize
                int      ysize
@@ -1515,6 +1539,8 @@ i_matrix_transform(im, xsize, ysize, matrix)
         IV len;
         SV *sv1;
         int i;
+       i_color *backp = NULL;
+       i_fcolor *fbackp = NULL;
       CODE:
         if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVAV)
           croak("i_matrix_transform: parameter 4 must be an array ref\n");
@@ -1528,7 +1554,20 @@ i_matrix_transform(im, xsize, ysize, matrix)
         }
         for (; i < 9; ++i)
           matrix[i] = 0;
-        RETVAL = i_matrix_transform(im, xsize, ysize, matrix);        
+       /* extract the bg colors if any */
+       /* yes, this is kind of strange */
+       for (i = 4; i < items; ++i) {
+          sv1 = ST(i);
+          if (sv_derived_from(sv1, "Imager::Color")) {
+           IV tmp = SvIV((SV*)SvRV(sv1));
+           backp = INT2PTR(i_color *, tmp);
+         }
+         else if (sv_derived_from(sv1, "Imager::Color::Float")) {
+           IV tmp = SvIV((SV*)SvRV(sv1));
+           fbackp = INT2PTR(i_fcolor *, tmp);
+         }
+       }
+        RETVAL = i_matrix_transform_bg(im, xsize, ysize, matrix, backp, fbackp);
       OUTPUT:
         RETVAL
 
@@ -1575,7 +1614,6 @@ i_convert(im, src, coeff)
          int inchan;
          AV *avmain;
           SV **temp;
-         SV *svsub;
           AV *avsub;
          int len;
          int i, j;
@@ -1827,7 +1865,7 @@ i_t1_glyph_name(handle, text_sv, utf8 = 0)
         text = SvPV(text_sv, work_len);
         len = work_len;
         while (len) {
-          unsigned char ch;
+          unsigned long ch;
           if (utf8) {
             ch = i_utf8_advance(&text, &len);
             if (ch == ~0UL) {
@@ -1871,7 +1909,7 @@ MODULE = Imager         PACKAGE = Imager
 
 
 undef_int
-i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8)
+i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8,align=1)
   Imager::Font::TT     handle
     Imager::ImgRaw     im
               int     xb
@@ -1882,6 +1920,7 @@ i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8)
               int     len_ignored
               int     smooth
                int     utf8
+               int     align
              PREINIT:
                char *str;
                STRLEN len;
@@ -1892,13 +1931,13 @@ i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8)
 #endif
                str = SvPV(str_sv, len);
                RETVAL = i_tt_text(handle, im, xb, yb, cl, points, str, 
-                                  len, smooth, utf8);
+                                  len, smooth, utf8, align);
              OUTPUT:
                RETVAL                
 
 
 undef_int
-i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8)
+i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8,align=1)
   Imager::Font::TT     handle
     Imager::ImgRaw     im
               int     xb
@@ -1909,6 +1948,7 @@ i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8)
               int     len_ignored
               int     smooth
                int     utf8
+               int     align
              PREINIT:
                char *str;
                STRLEN len;
@@ -1919,12 +1959,12 @@ i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8)
 #endif
                str = SvPV(str_sv, len);
                RETVAL = i_tt_cp(handle, im, xb, yb, channel, points, str, len,
-                                smooth, utf8);
+                                smooth, utf8, align);
              OUTPUT:
                 RETVAL
 
 
-undef_int
+void
 i_tt_bbox(handle,point,str_sv,len_ignored, utf8)
   Imager::Font::TT     handle
             float     point
@@ -2016,7 +2056,7 @@ i_tt_glyph_name(handle, text_sv, utf8 = 0)
         text = SvPV(text_sv, work_len);
         len = work_len;
         while (len) {
-          unsigned char ch;
+          unsigned long ch;
           if (utf8) {
             ch = i_utf8_advance(&text, &len);
             if (ch == ~0UL) {
@@ -2073,6 +2113,8 @@ i_readjpeg_wiol(ig)
                     myfree(iptc_itext);
              }
 
+int
+i_exif_enabled()
 
 #endif
 
@@ -2087,9 +2129,10 @@ i_test_format_probe(ig, length)
 #ifdef HAVE_LIBTIFF
 
 Imager::ImgRaw
-i_readtiff_wiol(ig, length)
+i_readtiff_wiol(ig, length, page=0)
         Imager::IO     ig
               int     length
+               int     page
 
 void
 i_readtiff_multi_wiol(ig, length)
@@ -2258,7 +2301,7 @@ i_writegif(im,fd,colors,pixdev,fixed)
               sv1=(*(av_fetch(av,i,0)));
                if (sv_derived_from(sv1, "Imager::Color")) {
                  Itmp = SvIV((SV*)SvRV(sv1));
-                 tmp = (i_color*) Itmp;
+                 tmp = INT2PTR(i_color*, Itmp);
                } else croak("Imager: one of the elements of array ref is not of Imager::Color type\n");
                fixed[i]=*tmp;
             }
@@ -2529,6 +2572,11 @@ i_readgif_wiol(ig)
             PUSHs(newRV_noinc((SV*)ct));
         }
 
+Imager::ImgRaw
+i_readgif_single_wiol(ig, page=0)
+       Imager::IO      ig
+        int            page
+
 void
 i_readgif_scalar(...)
           PROTOTYPE: $
@@ -2582,8 +2630,6 @@ void
 i_readgif_callback(...)
           PROTOTYPE: &
             PREINIT:
-               char*    data;
-               int     length;
                int*    colour_table;
                int     colours, q, w;
              i_img*    rimg;
@@ -2757,8 +2803,6 @@ i_writetga_wiol(im,ig, wierdpack, compress, idstring)
                int     compress
               char*    idstring
             PREINIT:
-                SV* sv1;
-                int rc;
                 int idlen;
               CODE:
                 idlen  = SvCUR(ST(4));
@@ -2781,8 +2825,6 @@ i_writergb_wiol(im,ig, wierdpack, compress, idstring)
                int     compress
               char*    idstring
             PREINIT:
-                SV* sv1;
-                int rc;
                 int idlen;
               CODE:
                 idlen  = SvCUR(ST(4));
@@ -2881,7 +2923,6 @@ i_transform2(sv_width,sv_height,channels,sv_ops,av_n_regs,av_c_regs,av_in_imgs)
             PREINIT:
              int width;
              int height;
-            double* parm;
             struct rm_op *ops;
             STRLEN ops_len;
             int ops_count;
@@ -2891,8 +2932,7 @@ i_transform2(sv_width,sv_height,channels,sv_ops,av_n_regs,av_c_regs,av_in_imgs)
             int c_regs_count;
              int in_imgs_count;
              i_img **in_imgs;
-            AV* av;
-            SV* sv1;
+             SV *sv1;
              IV tmp;
             int i;
              CODE:
@@ -3163,7 +3203,6 @@ i_errors()
         i_errmsg *errors;
        int i;
        AV *av;
-       SV *ref;
        SV *sv;
       PPCODE:
        errors = i_errors();
@@ -3302,7 +3341,6 @@ DSO_call(handle,func_index,hv)
 
 
 
-# this is mostly for testing...
 SV *
 i_get_pixel(im, x, y)
        Imager::ImgRaw im
@@ -3313,13 +3351,15 @@ i_get_pixel(im, x, y)
       CODE:
        color = (i_color *)mymalloc(sizeof(i_color));
        if (i_gpix(im, x, y, color) == 0) {
-          ST(0) = sv_newmortal();
-          sv_setref_pv(ST(0), "Imager::Color", (void *)color);
+          RETVAL = NEWSV(0, 0);
+          sv_setref_pv(RETVAL, "Imager::Color", (void *)color);
         }
         else {
           myfree(color);
-          ST(0) = &PL_sv_undef;
+          RETVAL = &PL_sv_undef;
         }
+      OUTPUT:
+        RETVAL
         
 
 int
@@ -3400,7 +3440,7 @@ i_ppal(im, l, y, ...)
         int     y
       PREINIT:
         i_palidx *work;
-        int count, i;
+        int i;
       CODE:
         if (items > 3) {
           work = mymalloc(sizeof(i_palidx) * (items-3));
@@ -3435,22 +3475,24 @@ i_addcolors(im, ...)
           }
           else {
             myfree(colors);
-            croak("i_plin: pixels must be Imager::Color objects");
+            croak("i_addcolor: pixels must be Imager::Color objects");
           }
         }
         index = i_addcolors(im, colors, items-1);
         myfree(colors);
         if (index == 0) {
-          ST(0) = sv_2mortal(newSVpv("0 but true", 0));
+          RETVAL = newSVpv("0 but true", 0);
         }
         else if (index == -1) {
-          ST(0) = &PL_sv_undef;
+          RETVAL = &PL_sv_undef;
         }
         else {
-          ST(0) = sv_2mortal(newSViv(index));
+          RETVAL = newSViv(index);
         }
+      OUTPUT:
+        RETVAL
 
-int 
+undef_int 
 i_setcolors(im, index, ...)
         Imager::ImgRaw  im
         int index
@@ -3474,6 +3516,8 @@ i_setcolors(im, index, ...)
         }
         RETVAL = i_setcolors(im, index, colors, items-2);
         myfree(colors);
+      OUTPUT:
+       RETVAL
 
 void
 i_getcolors(im, index, ...)
@@ -3504,33 +3548,13 @@ i_getcolors(im, index, ...)
         myfree(colors);
 
 
-SV *
+undef_neg_int
 i_colorcount(im)
         Imager::ImgRaw im
-      PREINIT:
-        int count;
-      CODE:
-        count = i_colorcount(im);
-        if (count >= 0) {
-          ST(0) = sv_2mortal(newSViv(count));
-        }
-        else {
-          ST(0) = &PL_sv_undef;
-        }
 
-SV *
+undef_neg_int
 i_maxcolors(im)
         Imager::ImgRaw im
-      PREINIT:
-        int count;
-      CODE:
-        count = i_maxcolors(im);
-        if (count >= 0) {
-          ST(0) = sv_2mortal(newSViv(count));
-        }
-        else {
-          ST(0) = &PL_sv_undef;
-        }
 
 SV *
 i_findcolor(im, color)
@@ -3540,11 +3564,13 @@ i_findcolor(im, color)
         i_palidx index;
       CODE:
         if (i_findcolor(im, color, &index)) {
-          ST(0) = sv_2mortal(newSViv(index));
+          RETVAL = newSViv(index);
         }
         else {
-          ST(0) = &PL_sv_undef;
+          RETVAL = &PL_sv_undef;
         }
+      OUTPUT:
+        RETVAL
 
 int
 i_img_bits(im)
@@ -3629,24 +3655,36 @@ i_plin(im, l, y, ...)
         int     y
       PREINIT:
         i_color *work;
-        int count, i;
+        int i;
+        STRLEN len;
+        int count;
       CODE:
         if (items > 3) {
-          work = mymalloc(sizeof(i_color) * (items-3));
-          for (i=0; i < items-3; ++i) {
-            if (sv_isobject(ST(i+3)) 
-                && sv_derived_from(ST(i+3), "Imager::Color")) {
-              IV tmp = SvIV((SV *)SvRV(ST(i+3)));
-              work[i] = *INT2PTR(i_color *, tmp);
+          if (items == 4 && SvOK(ST(3)) && !SvROK(ST(3))) {
+           /* supplied as a byte string */
+            work = (i_color *)SvPV(ST(3), len);
+            count = len / sizeof(i_color);
+           if (count * sizeof(i_color) != len) {
+              croak("i_plin: length of scalar argument must be multiple of sizeof i_color");
             }
-            else {
-              myfree(work);
-              croak("i_plin: pixels must be Imager::Color objects");
+            RETVAL = i_plin(im, l, l+count, y, work);
+          }
+         else {
+            work = mymalloc(sizeof(i_color) * (items-3));
+            for (i=0; i < items-3; ++i) {
+              if (sv_isobject(ST(i+3)) 
+                  && sv_derived_from(ST(i+3), "Imager::Color")) {
+                IV tmp = SvIV((SV *)SvRV(ST(i+3)));
+                work[i] = *INT2PTR(i_color *, tmp);
+              }
+              else {
+                myfree(work);
+                croak("i_plin: pixels must be Imager::Color objects");
+              }
             }
+            RETVAL = i_plin(im, l, l+items-3, y, work);
+            myfree(work);
           }
-          /**(char *)0 = 1;*/
-          RETVAL = i_plin(im, l, l+items-3, y, work);
-          myfree(work);
         }
         else {
           RETVAL = 0;
@@ -3706,24 +3744,37 @@ i_plinf(im, l, y, ...)
         int     y
       PREINIT:
         i_fcolor *work;
-        int count, i;
+        int i;
+        STRLEN len;
+        int count;
       CODE:
         if (items > 3) {
-          work = mymalloc(sizeof(i_fcolor) * (items-3));
-          for (i=0; i < items-3; ++i) {
-            if (sv_isobject(ST(i+3)) 
-                && sv_derived_from(ST(i+3), "Imager::Color::Float")) {
-              IV tmp = SvIV((SV *)SvRV(ST(i+3)));
-              work[i] = *INT2PTR(i_fcolor *, tmp);
+          if (items == 4 && SvOK(ST(3)) && !SvROK(ST(3))) {
+           /* supplied as a byte string */
+            work = (i_fcolor *)SvPV(ST(3), len);
+            count = len / sizeof(i_fcolor);
+           if (count * sizeof(i_fcolor) != len) {
+              croak("i_plin: length of scalar argument must be multiple of sizeof i_fcolor");
             }
-            else {
-              myfree(work);
-              croak("i_plin: pixels must be Imager::Color::Float objects");
+            RETVAL = i_plinf(im, l, l+count, y, work);
+          }
+         else {
+            work = mymalloc(sizeof(i_fcolor) * (items-3));
+            for (i=0; i < items-3; ++i) {
+              if (sv_isobject(ST(i+3)) 
+                  && sv_derived_from(ST(i+3), "Imager::Color::Float")) {
+                IV tmp = SvIV((SV *)SvRV(ST(i+3)));
+                work[i] = *INT2PTR(i_fcolor *, tmp);
+              }
+              else {
+                myfree(work);
+                croak("i_plinf: pixels must be Imager::Color::Float objects");
+              }
             }
+            /**(char *)0 = 1;*/
+            RETVAL = i_plinf(im, l, l+items-3, y, work);
+            myfree(work);
           }
-          /**(char *)0 = 1;*/
-          RETVAL = i_plinf(im, l, l+items-3, y, work);
-          myfree(work);
         }
         else {
           RETVAL = 0;
@@ -3741,14 +3792,16 @@ i_gpixf(im, x, y)
       CODE:
        color = (i_fcolor *)mymalloc(sizeof(i_fcolor));
        if (i_gpixf(im, x, y, color) == 0) {
-          ST(0) = sv_newmortal();
-          sv_setref_pv(ST(0), "Imager::Color::Float", (void *)color);
+          RETVAL = NEWSV(0,0);
+          sv_setref_pv(RETVAL, "Imager::Color::Float", (void *)color);
         }
         else {
           myfree(color);
-          ST(0) = &PL_sv_undef;
+          RETVAL = &PL_sv_undef;
         }
-        
+      OUTPUT:
+        RETVAL
+
 void
 i_glin(im, l, r, y)
         Imager::ImgRaw im
@@ -3762,13 +3815,20 @@ i_glin(im, l, r, y)
         if (l < r) {
           vals = mymalloc((r-l) * sizeof(i_color));
           count = i_glin(im, l, r, y, vals);
-          EXTEND(SP, count);
-          for (i = 0; i < count; ++i) {
-            SV *sv;
-            i_color *col = mymalloc(sizeof(i_color));
-            sv = sv_newmortal();
-            sv_setref_pv(sv, "Imager::Color", (void *)col);
-            PUSHs(sv);
+         if (GIMME_V == G_ARRAY) {
+            EXTEND(SP, count);
+            for (i = 0; i < count; ++i) {
+              SV *sv;
+              i_color *col = mymalloc(sizeof(i_color));
+              *col = vals[i];
+              sv = sv_newmortal();
+              sv_setref_pv(sv, "Imager::Color", (void *)col);
+              PUSHs(sv);
+            }
+          }
+          else if (count) {
+           EXTEND(SP, 1);
+           PUSHs(sv_2mortal(newSVpv((void *)vals, count * sizeof(i_color))));
           }
           myfree(vals);
         }
@@ -3786,14 +3846,20 @@ i_glinf(im, l, r, y)
         if (l < r) {
           vals = mymalloc((r-l) * sizeof(i_fcolor));
           count = i_glinf(im, l, r, y, vals);
-          EXTEND(SP, count);
-          for (i = 0; i < count; ++i) {
-            SV *sv;
-            i_fcolor *col = mymalloc(sizeof(i_fcolor));
-            *col = vals[i];
-            sv = sv_newmortal();
-            sv_setref_pv(sv, "Imager::Color::Float", (void *)col);
-            PUSHs(sv);
+          if (GIMME_V == G_ARRAY) {
+            EXTEND(SP, count);
+            for (i = 0; i < count; ++i) {
+              SV *sv;
+              i_fcolor *col = mymalloc(sizeof(i_fcolor));
+              *col = vals[i];
+              sv = sv_newmortal();
+              sv_setref_pv(sv, "Imager::Color::Float", (void *)col);
+              PUSHs(sv);
+            }
+          }
+          else if (count) {
+            EXTEND(SP, 1);
+            PUSHs(sv_2mortal(newSVpv((void *)vals, count * sizeof(i_fcolor))));
           }
           myfree(vals);
         }
@@ -3861,12 +3927,14 @@ i_tags_find(im, name, start)
       CODE:
         if (i_tags_find(&im->tags, name, start, &entry)) {
           if (entry == 0)
-            ST(0) = sv_2mortal(newSVpv("0 but true", 0));
+            RETVAL = newSVpv("0 but true", 0);
           else
-            ST(0) = sv_2mortal(newSViv(entry));
+            RETVAL = newSViv(entry);
         } else {
-          ST(0) = &PL_sv_undef;
+          RETVAL = &PL_sv_undef;
         }
+      OUTPUT:
+        RETVAL
 
 SV *
 i_tags_findn(im, code, start)
@@ -3878,12 +3946,15 @@ i_tags_findn(im, code, start)
       CODE:
         if (i_tags_findn(&im->tags, code, start, &entry)) {
           if (entry == 0)
-            ST(0) = sv_2mortal(newSVpv("0 but true", 0));
+            RETVAL = newSVpv("0 but true", 0);
           else
-            ST(0) = sv_2mortal(newSViv(entry));
+            RETVAL = newSViv(entry);
         }
-        else
-          ST(0) = &PL_sv_undef;
+        else {
+          RETVAL = &PL_sv_undef;
+        }
+      OUTPUT:
+        RETVAL
 
 int
 i_tags_delete(im, entry)
@@ -3935,6 +4006,28 @@ i_tags_get(im, index)
           }
         }
 
+void
+i_tags_get_string(im, what_sv)
+        Imager::ImgRaw  im
+        SV *what_sv
+      PREINIT:
+        char const *name = NULL;
+        int code;
+        char buffer[200];
+      PPCODE:
+        if (SvIOK(what_sv)) {
+          code = SvIV(what_sv);
+          name = NULL;
+        }
+        else {
+          name = SvPV_nolen(what_sv);
+          code = 0;
+        }
+        if (i_tags_get_string(&im->tags, name, code, buffer, sizeof(buffer))) {
+          EXTEND(SP, 1);
+          PUSHs(sv_2mortal(newSVpv(buffer, 0)));
+        }
+
 int
 i_tags_count(im)
         Imager::ImgRaw  im
@@ -3950,9 +4043,9 @@ i_wf_bbox(face, size, text)
        char *face
        int size
        char *text
-        int rc, i;
       PREINIT:
        int cords[BOUNDING_BOX_COUNT];
+        int rc, i;
       PPCODE:
         if (rc = i_wf_bbox(face, size, text, strlen(text), cords)) {
           EXTEND(SP, rc);  
@@ -3994,6 +4087,9 @@ i_wf_cp(face, im, tx, ty, channel, size, text, align, aa)
       OUTPUT:
        RETVAL
 
+undef_int
+i_wf_addfont(font)
+        char *font
 
 #endif
 
@@ -4230,10 +4326,11 @@ undef_int
 i_ft2_can_face_name()
 
 void
-i_ft2_glyph_name(handle, text_sv, utf8 = 0)
+i_ft2_glyph_name(handle, text_sv, utf8 = 0, reliable_only = 1)
         Imager::Font::FT2 handle
         SV *text_sv
         int utf8
+        int reliable_only
       PREINIT:
         char const *text;
         STRLEN work_len;
@@ -4248,7 +4345,7 @@ i_ft2_glyph_name(handle, text_sv, utf8 = 0)
         text = SvPV(text_sv, work_len);
         len = work_len;
         while (len) {
-          unsigned char ch;
+          unsigned long ch;
           if (utf8) {
             ch = i_utf8_advance(&text, &len);
             if (ch == ~0UL) {
@@ -4261,7 +4358,8 @@ i_ft2_glyph_name(handle, text_sv, utf8 = 0)
             --len;
           }
           EXTEND(SP, 1);
-          if (outsize = i_ft2_glyph_name(handle, ch, name, sizeof(name))) {
+          if (outsize = i_ft2_glyph_name(handle, ch, name, sizeof(name), 
+                                         reliable_only)) {
             PUSHs(sv_2mortal(newSVpv(name, 0)));
           }
           else {
@@ -4276,6 +4374,58 @@ int
 i_ft2_face_has_glyph_names(handle)
         Imager::Font::FT2 handle
 
+int
+i_ft2_is_multiple_master(handle)
+        Imager::Font::FT2 handle
+
+void
+i_ft2_get_multiple_masters(handle)
+        Imager::Font::FT2 handle
+      PREINIT:
+        i_font_mm mm;
+        int i;
+      PPCODE:
+        if (i_ft2_get_multiple_masters(handle, &mm)) {
+          EXTEND(SP, 2+mm.num_axis);
+          PUSHs(sv_2mortal(newSViv(mm.num_axis)));
+          PUSHs(sv_2mortal(newSViv(mm.num_designs)));
+          for (i = 0; i < mm.num_axis; ++i) {
+            AV *av = newAV();
+            SV *sv;
+            av_extend(av, 3);
+            sv = newSVpv(mm.axis[i].name, strlen(mm.axis[i].name));
+            SvREFCNT_inc(sv);
+            av_store(av, 0, sv);
+            sv = newSViv(mm.axis[i].minimum);
+            SvREFCNT_inc(sv);
+            av_store(av, 1, sv);
+            sv = newSViv(mm.axis[i].maximum);
+            SvREFCNT_inc(sv);
+            av_store(av, 2, sv);
+            PUSHs(newRV_noinc((SV *)av));
+          }
+        }
+
+undef_int
+i_ft2_set_mm_coords(handle, ...)
+        Imager::Font::FT2 handle
+      PROTOTYPE: DISABLE
+      PREINIT:
+        long *coords;
+        int ix_coords, i;
+      CODE:
+        /* T_ARRAY handling by xsubpp seems to be busted in 5.6.1, so
+           transfer the array manually */
+        ix_coords = items-1;
+        coords = mymalloc(sizeof(long) * ix_coords);
+       for (i = 0; i < ix_coords; ++i) {
+          coords[i] = (long)SvIV(ST(1+i));
+        }
+        RETVAL = i_ft2_set_mm_coords(handle, ix_coords, coords);
+        myfree(coords);
+      OUTPUT:
+        RETVAL
+
 #endif
 
 MODULE = Imager         PACKAGE = Imager::FillHandle PREFIX=IFILL_
@@ -4374,3 +4524,39 @@ i_new_fill_image(src, matrix, xoff, yoff, combine)
       OUTPUT:
         RETVAL
 
+MODULE = Imager  PACKAGE = Imager::Internal::Hlines  PREFIX=i_int_hlines_
+
+# this class is only exposed for testing
+
+int
+i_int_hlines_testing()
+
+#if i_int_hlines_testing()
+
+Imager::Internal::Hlines
+i_int_hlines_new(start_y, count_y, start_x, count_x)
+       int start_y
+       int count_y
+       int start_x
+       int count_x
+
+Imager::Internal::Hlines
+i_int_hlines_new_img(im)
+       Imager::ImgRaw im
+
+void
+i_int_hlines_add(hlines, y, minx, width)
+       Imager::Internal::Hlines hlines
+       int y
+       int minx
+       int width
+
+void
+i_int_hlines_DESTROY(hlines)
+       Imager::Internal::Hlines hlines
+
+SV *
+i_int_hlines_dump(hlines)
+       Imager::Internal::Hlines hlines
+
+#endif