17 typedef io_glue* Imager__IO;
18 typedef i_color* Imager__Color;
19 typedef i_img* Imager__ImgRaw;
23 typedef TT_Fonthandle* Imager__TTHandle;
26 typedef struct i_reader_data_tag
28 /* presumably a CODE ref or name of a sub */
32 /* used by functions that want callbacks */
33 static int read_callback(char *userdata, char *buffer, int need, int want) {
34 i_reader_data *rd = (i_reader_data *)userdata;
38 dSP; dTARG = sv_newmortal();
39 /* thanks to Simon Cozens for help with the dTARG above */
49 count = perl_call_sv(rd->sv, G_SCALAR);
54 croak("Result of perl_call_sv(..., G_SCALAR) != 1");
60 char *ptr = SvPV(data, len);
62 croak("Too much data returned in reader callback");
64 memcpy(buffer, ptr, len);
80 SV *sv; /* a coderef or sub name */
83 /* used by functions that want callbacks */
84 static int write_callback(char *userdata, char const *data, int size) {
85 i_writer_data *wd = (i_writer_data *)userdata;
95 XPUSHs(sv_2mortal(newSVpv((char *)data, size)));
98 count = perl_call_sv(wd->sv, G_SCALAR);
103 croak("Result of perl_call_sv(..., G_SCALAR) != 1");
106 success = SvTRUE(sv);
120 static int lookup_name(struct value_name *names, int count, char *name, int def_value)
123 for (i = 0; i < count; ++i)
124 if (strEQ(names[i].name, name))
125 return names[i].value;
129 static struct value_name transp_names[] =
132 { "threshold", tr_threshold },
133 { "errdiff", tr_errdiff },
134 { "ordered", tr_ordered, },
137 static struct value_name make_color_names[] =
139 { "none", mc_none, },
140 { "webmap", mc_web_map, },
141 { "addi", mc_addi, },
144 static struct value_name translate_names[] =
147 { "giflib", pt_giflib, },
149 { "closest", pt_closest, },
150 { "perturb", pt_perturb, },
151 { "errdiff", pt_errdiff, },
154 static struct value_name errdiff_names[] =
156 { "floyd", ed_floyd, },
157 { "jarvis", ed_jarvis, },
158 { "stucki", ed_stucki, },
159 { "custom", ed_custom, },
162 static struct value_name orddith_names[] =
164 { "random", od_random, },
165 { "dot8", od_dot8, },
166 { "dot4", od_dot4, },
167 { "hline", od_hline, },
168 { "vline", od_vline, },
169 { "/line", od_slashline, },
170 { "slashline", od_slashline, },
171 { "\\line", od_backline, },
172 { "backline", od_backline, },
173 { "tiny", od_tiny, },
174 { "custom", od_custom, },
177 /* look through the hash for quantization options */
178 static void handle_quant_opts(i_quantize *quant, HV *hv)
180 /*** POSSIBLY BROKEN: do I need to unref the SV from hv_fetch ***/
186 sv = hv_fetch(hv, "transp", 6, 0);
187 if (sv && *sv && (str = SvPV(*sv, len))) {
189 lookup_name(transp_names, sizeof(transp_names)/sizeof(*transp_names),
191 if (quant->transp != tr_none) {
192 quant->tr_threshold = 127;
193 sv = hv_fetch(hv, "tr_threshold", 12, 0);
195 quant->tr_threshold = SvIV(*sv);
197 if (quant->transp == tr_errdiff) {
198 sv = hv_fetch(hv, "tr_errdiff", 10, 0);
199 if (sv && *sv && (str = SvPV(*sv, len)))
200 quant->tr_errdiff = lookup_name(errdiff_names, sizeof(errdiff_names)/sizeof(*errdiff_names), str, ed_floyd);
202 if (quant->transp == tr_ordered) {
203 quant->tr_orddith = od_tiny;
204 sv = hv_fetch(hv, "tr_orddith", 10, 0);
205 if (sv && *sv && (str = SvPV(*sv, len)))
206 quant->tr_orddith = lookup_name(orddith_names, sizeof(orddith_names)/sizeof(*orddith_names), str, od_random);
208 if (quant->tr_orddith == od_custom) {
209 sv = hv_fetch(hv, "tr_map", 6, 0);
210 if (sv && *sv && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
211 AV *av = (AV*)SvRV(*sv);
212 len = av_len(av) + 1;
213 if (len > sizeof(quant->tr_custom))
214 len = sizeof(quant->tr_custom);
215 for (i = 0; i < len; ++i) {
216 SV **sv2 = av_fetch(av, i, 0);
218 quant->tr_custom[i] = SvIV(*sv2);
221 while (i < sizeof(quant->tr_custom))
222 quant->tr_custom[i++] = 0;
227 quant->make_colors = mc_addi;
228 sv = hv_fetch(hv, "make_colors", 11, 0);
229 if (sv && *sv && (str = SvPV(*sv, len))) {
231 lookup_name(make_color_names, sizeof(make_color_names)/sizeof(*make_color_names), str, mc_addi);
233 sv = hv_fetch(hv, "colors", 6, 0);
234 if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
235 /* needs to be an array of Imager::Color
236 note that the caller allocates the mc_color array and sets mc_size
238 AV *av = (AV *)SvRV(*sv);
239 quant->mc_count = av_len(av)+1;
240 if (quant->mc_count > quant->mc_size)
241 quant->mc_count = quant->mc_size;
242 for (i = 0; i < quant->mc_count; ++i) {
243 SV **sv1 = av_fetch(av, i, 0);
244 if (sv1 && *sv1 && SvROK(*sv1) && sv_derived_from(*sv1, "Imager::Color")) {
245 i_color *col = (i_color *)SvIV((SV*)SvRV(*sv1));
246 quant->mc_colors[i] = *col;
250 sv = hv_fetch(hv, "max_colors", 10, 0);
253 if (i <= quant->mc_size && i >= quant->mc_count)
257 quant->translate = pt_closest;
258 sv = hv_fetch(hv, "translate", 9, 0);
259 if (sv && *sv && (str = SvPV(*sv, len))) {
260 quant->translate = lookup_name(translate_names, sizeof(translate_names)/sizeof(*translate_names), str, pt_closest);
262 sv = hv_fetch(hv, "errdiff", 7, 0);
263 if (sv && *sv && (str = SvPV(*sv, len))) {
264 quant->errdiff = lookup_name(errdiff_names, sizeof(errdiff_names)/sizeof(*errdiff_names), str, ed_floyd);
266 if (quant->translate == pt_errdiff && quant->errdiff == ed_custom) {
267 /* get the error diffusion map */
268 sv = hv_fetch(hv, "errdiff_width", 13, 0);
270 quant->ed_width = SvIV(*sv);
271 sv = hv_fetch(hv, "errdiff_height", 14, 0);
273 quant->ed_height = SvIV(*sv);
274 sv = hv_fetch(hv, "errdiff_orig", 12, 0);
276 quant->ed_orig = SvIV(*sv);
277 if (quant->ed_width > 0 && quant->ed_height > 0) {
279 quant->ed_map = mymalloc(sizeof(int)*quant->ed_width*quant->ed_height);
280 sv = hv_fetch(hv, "errdiff_map", 11, 0);
281 if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
282 AV *av = (AV*)SvRV(*sv);
283 len = av_len(av) + 1;
284 if (len > quant->ed_width * quant->ed_height)
285 len = quant->ed_width * quant->ed_height;
286 for (i = 0; i < len; ++i) {
287 SV **sv2 = av_fetch(av, i, 0);
289 quant->ed_map[i] = SvIV(*sv2);
290 sum += quant->ed_map[i];
296 myfree(quant->ed_map);
298 quant->errdiff = ed_floyd;
302 sv = hv_fetch(hv, "perturb", 7, 0);
304 quant->perturb = SvIV(*sv);
307 /* look through the hash for options to add to opts */
308 static void handle_gif_opts(i_gif_opts *opts, HV *hv)
310 /*** FIXME: POSSIBLY BROKEN: do I need to unref the SV from hv_fetch? ***/
313 /**((char *)0) = '\0';*/
314 sv = hv_fetch(hv, "gif_each_palette", 16, 0);
316 opts->each_palette = SvIV(*sv);
317 sv = hv_fetch(hv, "interlace", 9, 0);
319 opts->interlace = SvIV(*sv);
320 sv = hv_fetch(hv, "gif_delays", 10, 0);
321 if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
322 AV *av = (AV*)SvRV(*sv);
323 opts->delay_count = av_len(av)+1;
324 opts->delays = mymalloc(sizeof(int) * opts->delay_count);
325 for (i = 0; i < opts->delay_count; ++i) {
326 SV *sv1 = *av_fetch(av, i, 0);
327 opts->delays[i] = SvIV(sv1);
330 sv = hv_fetch(hv, "gif_user_input", 14, 0);
331 if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
332 AV *av = (AV*)SvRV(*sv);
333 opts->user_input_count = av_len(av)+1;
334 opts->user_input_flags = mymalloc(opts->user_input_count);
335 for (i = 0; i < opts->user_input_count; ++i) {
336 SV *sv1 = *av_fetch(av, i, 0);
337 opts->user_input_flags[i] = SvIV(sv1) != 0;
340 sv = hv_fetch(hv, "gif_disposal", 12, 0);
341 if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
342 AV *av = (AV*)SvRV(*sv);
343 opts->disposal_count = av_len(av)+1;
344 opts->disposal = mymalloc(opts->disposal_count);
345 for (i = 0; i < opts->disposal_count; ++i) {
346 SV *sv1 = *av_fetch(av, i, 0);
347 opts->disposal[i] = SvIV(sv1);
350 sv = hv_fetch(hv, "gif_tran_color", 14, 0);
351 if (sv && *sv && SvROK(*sv) && sv_derived_from(*sv, "Imager::Color")) {
352 i_color *col = (i_color *)SvIV((SV *)SvRV(*sv));
353 opts->tran_color = *col;
355 sv = hv_fetch(hv, "gif_positions", 13, 0);
356 if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
357 AV *av = (AV *)SvRV(*sv);
358 opts->position_count = av_len(av) + 1;
359 opts->positions = mymalloc(sizeof(i_gif_pos) * opts->position_count);
360 for (i = 0; i < opts->position_count; ++i) {
361 SV **sv2 = av_fetch(av, i, 0);
362 opts->positions[i].x = opts->positions[i].y = 0;
363 if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
364 AV *av2 = (AV*)SvRV(*sv2);
366 sv3 = av_fetch(av2, 0, 0);
368 opts->positions[i].x = SvIV(*sv3);
369 sv3 = av_fetch(av2, 1, 0);
371 opts->positions[i].y = SvIV(*sv3);
375 /* Netscape2.0 loop count extension */
376 sv = hv_fetch(hv, "gif_loop_count", 14, 0);
378 opts->loop_count = SvIV(*sv);
381 /* copies the color map from the hv into the colors member of the HV */
382 static void copy_colors_back(HV *hv, i_quantize *quant) {
388 sv = hv_fetch(hv, "colors", 6, 0);
389 if (!sv || !*sv || !SvROK(*sv) || SvTYPE(SvRV(*sv)) != SVt_PVAV) {
392 ref = newRV_inc((SV*) av);
393 sv = hv_store(hv, "colors", 6, ref, 0);
396 av = (AV *)SvRV(*sv);
398 av_extend(av, quant->mc_count+1);
399 for (i = 0; i < quant->mc_count; ++i) {
400 i_color *in = quant->mc_colors+i;
401 Imager__Color c = ICL_new_internal(in->rgb.r, in->rgb.g, in->rgb.b, 255);
402 work = sv_newmortal();
403 sv_setref_pv(work, "Imager::Color", (void *)c);
405 if (!av_store(av, i, work)) {
411 MODULE = Imager PACKAGE = Imager::Color PREFIX = ICL_
414 ICL_new_internal(r,g,b,a)
426 ICL_set_internal(cl,r,g,b,a)
433 ICL_set_internal(cl, r, g, b, a);
447 PUSHs(sv_2mortal(newSVnv(cl->rgba.r)));
448 PUSHs(sv_2mortal(newSVnv(cl->rgba.g)));
449 PUSHs(sv_2mortal(newSVnv(cl->rgba.b)));
450 PUSHs(sv_2mortal(newSVnv(cl->rgba.a)));
458 MODULE = Imager PACKAGE = Imager::ImgRaw PREFIX = IIM_
472 MODULE = Imager PACKAGE = Imager
494 tlength = io_slurp(ig, &data);
497 PUSHs(sv_2mortal(newSVpv(data,tlength)));
509 while( (item=i_format_list[i++]) != NULL ) {
511 PUSHs(sv_2mortal(newSVpv(item,0)));
528 i_img_empty_ch(im,x,y,ch)
555 PUSHs(sv_2mortal(newSViv(info[0])));
556 PUSHs(sv_2mortal(newSViv(info[1])));
557 PUSHs(sv_2mortal(newSViv(info[2])));
558 PUSHs(sv_2mortal(newSViv(info[3])));
564 i_img_setmask(im,ch_mask)
573 i_img_getchannels(im)
581 PUSHs(sv_2mortal(newSVpv(im->data, im->bytes)));
585 i_draw(im,x1,y1,x2,y2,val)
594 i_line_aa(im,x1,y1,x2,y2,val)
603 i_box(im,x1,y1,x2,y2,val)
612 i_box_filled(im,x1,y1,x2,y2,val)
621 i_arc(im,x,y,rad,d1,d2,val)
633 i_bezier_multi(im,xc,yc,val)
646 if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_bezier_multi must be a reference to an array\n");
647 if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_bezier_multi must be a reference to an array\n");
648 if (!SvROK(ST(2))) croak("Imager: Parameter 2 to i_bezier_multi must be a reference to an array\n");
649 if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 2 to i_bezier_multi must be a reference to an array\n");
650 av1=(AV*)SvRV(ST(1));
651 av2=(AV*)SvRV(ST(2));
652 if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_bezier_multi must be equal length\n");
654 x=mymalloc( len*sizeof(double) );
655 y=mymalloc( len*sizeof(double) );
657 sv1=(*(av_fetch(av1,i,0)));
658 sv2=(*(av_fetch(av2,i,0)));
659 x[i]=(double)SvNV(sv1);
660 y[i]=(double)SvNV(sv2);
662 i_bezier_multi(im,len,x,y,val);
668 i_poly_aa(im,xc,yc,val)
681 if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
682 if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
683 if (!SvROK(ST(2))) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
684 if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
685 av1=(AV*)SvRV(ST(1));
686 av2=(AV*)SvRV(ST(2));
687 if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_poly_aa must be equal length\n");
689 x=mymalloc( len*sizeof(double) );
690 y=mymalloc( len*sizeof(double) );
692 sv1=(*(av_fetch(av1,i,0)));
693 sv2=(*(av_fetch(av2,i,0)));
694 x[i]=(double)SvNV(sv1);
695 y[i]=(double)SvNV(sv2);
697 i_poly_aa(im,len,x,y,val);
704 i_flood_fill(im,seedx,seedy,dcol)
712 i_copyto(im,src,x1,y1,x2,y2,tx,ty)
724 i_copyto_trans(im,src,x1,y1,x2,y2,tx,ty,trans)
742 i_rubthru(im,src,tx,ty)
749 i_flipxy(im, direction)
769 if (!SvROK(ST(1))) croak("Imager: Parameter 1 must be a reference to an array\n");
770 if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 must be a reference to an array\n");
773 coeff=mymalloc( len*sizeof(float) );
775 sv1=(*(av_fetch(av,i,0)));
776 coeff[i]=(float)SvNV(sv1);
778 i_conv(im,coeff,len);
799 i_t1_new(pfb,afm=NULL)
804 i_t1_destroy(font_id)
809 i_t1_cp(im,xb,yb,channel,fontnum,points,str,len,align)
821 i_t1_bbox(fontnum,point,str,len)
829 i_t1_bbox(fontnum,point,str,len,cords);
831 PUSHs(sv_2mortal(newSViv(cords[0])));
832 PUSHs(sv_2mortal(newSViv(cords[1])));
833 PUSHs(sv_2mortal(newSViv(cords[2])));
834 PUSHs(sv_2mortal(newSViv(cords[3])));
835 PUSHs(sv_2mortal(newSViv(cords[4])));
836 PUSHs(sv_2mortal(newSViv(cords[5])));
841 i_t1_text(im,xb,yb,cl,fontnum,points,str,len,align)
863 Imager::TTHandle handle
868 i_tt_text(handle,im,xb,yb,cl,points,str,len,smooth)
869 Imager::TTHandle handle
881 i_tt_cp(handle,im,xb,yb,channel,points,str,len,smooth)
882 Imager::TTHandle handle
895 i_tt_bbox(handle,point,str,len)
896 Imager::TTHandle handle
903 if ((rc=i_tt_bbox(handle,point,str,len,cords))) {
905 PUSHs(sv_2mortal(newSViv(cords[0])));
906 PUSHs(sv_2mortal(newSViv(cords[1])));
907 PUSHs(sv_2mortal(newSViv(cords[2])));
908 PUSHs(sv_2mortal(newSViv(cords[3])));
909 PUSHs(sv_2mortal(newSViv(cords[4])));
910 PUSHs(sv_2mortal(newSViv(cords[5])));
921 i_writejpeg(im,fd,qfactor)
936 rimg=i_readjpeg(fd,&iptc_itext,&tlength);
937 if (iptc_itext == NULL) {
940 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
945 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
947 PUSHs(sv_2mortal(newSVpv(iptc_itext,tlength)));
953 i_readjpeg_scalar(...)
964 data = (char *)SvPV(ST(0), length);
965 rimg=i_readjpeg_scalar(data,length,&iptc_itext,&tlength);
966 mm_log((1,"i_readjpeg_scalar: 0x%08X\n",rimg));
967 if (iptc_itext == NULL) {
970 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
975 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
977 PUSHs(sv_2mortal(newSVpv(iptc_itext,tlength)));
992 rimg = i_readjpeg_wiol(ig,-1,&iptc_itext,&tlength);
993 if (iptc_itext == NULL) {
996 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1001 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1003 PUSHs(sv_2mortal(newSVpv(iptc_itext,tlength)));
1016 i_readtiff_wiol(ig, length)
1022 i_writetiff_wiol(im, ig)
1027 #endif /* HAVE_LIBTIFF */
1047 i_readpng_scalar(...)
1051 unsigned int length;
1053 data = (char *)SvPV(ST(0), length);
1054 RETVAL=i_readpng_scalar(data,length);
1068 PUSHs(sv_2mortal(newSVnv(IM_GIFMAJOR+IM_GIFMINOR*0.1)));
1071 i_writegif(im,fd,colors,pixdev,fixed)
1078 Imager__Color fixed;
1085 if (!SvROK(ST(4))) croak("Imager: Parameter 4 must be a reference to an array\n");
1086 if (SvTYPE(SvRV(ST(4))) != SVt_PVAV) croak("Imager: Parameter 4 must be a reference to an array\n");
1087 av=(AV*)SvRV(ST(4));
1088 fixedlen=av_len(av)+1;
1089 fixed=mymalloc( fixedlen*sizeof(i_color) );
1090 for(i=0;i<fixedlen;i++) {
1091 sv1=(*(av_fetch(av,i,0)));
1092 if (sv_derived_from(sv1, "Imager::Color")) {
1093 Itmp = SvIV((SV*)SvRV(sv1));
1094 tmp = (i_color*) Itmp;
1095 } else croak("Imager: one of the elements of array ref is not of Imager::Color type\n");
1098 RETVAL=i_writegif(im,fd,colors,pixdev,fixedlen,fixed);
1100 ST(0) = sv_newmortal();
1101 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1102 else sv_setiv(ST(0), (IV)RETVAL);
1108 i_writegifmc(im,fd,colors)
1119 i_writegif_gen(fd, ...)
1125 i_img **imgs = NULL;
1131 croak("Usage: i_writegif_gen(fd,hashref, images...)");
1132 if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
1133 croak("i_writegif_gen: Second argument must be a hash ref");
1134 hv = (HV *)SvRV(ST(1));
1135 memset(&quant, 0, sizeof(quant));
1136 quant.mc_size = 256;
1137 quant.mc_colors = mymalloc(quant.mc_size * sizeof(i_color));
1138 memset(&opts, 0, sizeof(opts));
1139 handle_quant_opts(&quant, hv);
1140 handle_gif_opts(&opts, hv);
1141 img_count = items - 2;
1143 if (img_count < 1) {
1146 i_push_error(0, "You need to specify images to save");
1149 imgs = mymalloc(sizeof(i_img *) * img_count);
1150 for (i = 0; i < img_count; ++i) {
1153 if (SvROK(sv) && sv_derived_from(sv, "Imager::ImgRaw")) {
1154 imgs[i] = (i_img *)SvIV((SV*)SvRV(sv));
1158 i_push_error(0, "Only images can be saved");
1164 RETVAL = i_writegif_gen(&quant, fd, imgs, img_count, &opts);
1168 copy_colors_back(hv, &quant);
1171 ST(0) = sv_newmortal();
1172 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1173 else sv_setiv(ST(0), (IV)RETVAL);
1176 i_writegif_callback(cb, maxbuffer,...)
1181 i_img **imgs = NULL;
1188 croak("Usage: i_writegif_callback(\\&callback,maxbuffer,hashref, images...)");
1189 if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
1190 croak("i_writegif_callback: Second argument must be a hash ref");
1191 hv = (HV *)SvRV(ST(2));
1192 memset(&quant, 0, sizeof(quant));
1193 quant.mc_size = 256;
1194 quant.mc_colors = mymalloc(quant.mc_size * sizeof(i_color));
1195 memset(&opts, 0, sizeof(opts));
1196 handle_quant_opts(&quant, hv);
1197 handle_gif_opts(&opts, hv);
1198 img_count = items - 3;
1200 if (img_count < 1) {
1204 imgs = mymalloc(sizeof(i_img *) * img_count);
1205 for (i = 0; i < img_count; ++i) {
1208 if (SvROK(sv) && sv_derived_from(sv, "Imager::ImgRaw")) {
1209 imgs[i] = (i_img *)SvIV((SV*)SvRV(sv));
1218 RETVAL = i_writegif_callback(&quant, write_callback, (char *)&wd, maxbuffer, imgs, img_count, &opts);
1222 copy_colors_back(hv, &quant);
1225 ST(0) = sv_newmortal();
1226 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1227 else sv_setiv(ST(0), (IV)RETVAL);
1240 colour_table = NULL;
1243 if(GIMME_V == G_ARRAY) {
1244 rimg = i_readgif(fd,&colour_table,&colours);
1246 /* don't waste time with colours if they aren't wanted */
1247 rimg = i_readgif(fd,NULL,NULL);
1250 if (colour_table == NULL) {
1253 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1256 /* the following creates an [[r,g,b], [r, g, b], [r, g, b]...] */
1257 /* I don't know if I have the reference counts right or not :( */
1258 /* Neither do I :-) */
1259 /* No Idea here either */
1262 av_extend(ct, colours);
1263 for(q=0; q<colours; q++) {
1265 temp[w]=sv_2mortal(newSViv(colour_table[q*3 + w]));
1266 av_store(ct, q, (SV*)newRV_noinc((SV*)av_make(3, temp)));
1268 myfree(colour_table);
1272 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1274 PUSHs(newRV_noinc((SV*)ct));
1282 i_readgif_scalar(...)
1286 unsigned int length;
1294 data = (char *)SvPV(ST(0), length);
1298 if(GIMME_V == G_ARRAY) {
1299 rimg=i_readgif_scalar(data,length,&colour_table,&colours);
1301 /* don't waste time with colours if they aren't wanted */
1302 rimg=i_readgif_scalar(data,length,NULL,NULL);
1305 if (colour_table == NULL) {
1308 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1311 /* the following creates an [[r,g,b], [r, g, b], [r, g, b]...] */
1312 /* I don't know if I have the reference counts right or not :( */
1313 /* Neither do I :-) */
1315 av_extend(ct, colours);
1316 for(q=0; q<colours; q++) {
1318 temp[w]=sv_2mortal(newSViv(colour_table[q*3 + w]));
1319 av_store(ct, q, (SV*)newRV_noinc((SV*)av_make(3, temp)));
1321 myfree(colour_table);
1325 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1327 PUSHs(newRV_noinc((SV*)ct));
1331 i_readgif_callback(...)
1348 if(GIMME_V == G_ARRAY) {
1349 rimg=i_readgif_callback(read_callback, (char *)&rd,&colour_table,&colours);
1351 /* don't waste time with colours if they aren't wanted */
1352 rimg=i_readgif_callback(read_callback, (char *)&rd,NULL,NULL);
1355 if (colour_table == NULL) {
1358 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1361 /* the following creates an [[r,g,b], [r, g, b], [r, g, b]...] */
1362 /* I don't know if I have the reference counts right or not :( */
1363 /* Neither do I :-) */
1364 /* Neither do I - maybe I'll move this somewhere */
1366 av_extend(ct, colours);
1367 for(q=0; q<colours; q++) {
1369 temp[w]=sv_2mortal(newSViv(colour_table[q*3 + w]));
1370 av_store(ct, q, (SV*)newRV_noinc((SV*)av_make(3, temp)));
1372 myfree(colour_table);
1376 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1378 PUSHs(newRV_noinc((SV*)ct));
1391 i_readpnm_wiol(ig, length)
1402 i_readraw(fd,x,y,datachannels,storechannels,intrl)
1417 i_scaleaxis(im,Value,Axis)
1423 i_scale_nn(im,scx,scy)
1433 i_count_colors(im,maxc)
1439 i_transform(im,opx,opy,parm)
1452 if (!SvROK(ST(1))) croak("Imager: Parameter 1 must be a reference to an array\n");
1453 if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to an array\n");
1454 if (!SvROK(ST(3))) croak("Imager: Parameter 3 must be a reference to an array\n");
1455 if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 must be a reference to an array\n");
1456 if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 2 must be a reference to an array\n");
1457 if (SvTYPE(SvRV(ST(3))) != SVt_PVAV) croak("Imager: Parameter 3 must be a reference to an array\n");
1458 av=(AV*)SvRV(ST(1));
1460 opx=mymalloc( opxl*sizeof(int) );
1461 for(i=0;i<opxl;i++) {
1462 sv1=(*(av_fetch(av,i,0)));
1463 opx[i]=(int)SvIV(sv1);
1465 av=(AV*)SvRV(ST(2));
1467 opy=mymalloc( opyl*sizeof(int) );
1468 for(i=0;i<opyl;i++) {
1469 sv1=(*(av_fetch(av,i,0)));
1470 opy[i]=(int)SvIV(sv1);
1472 av=(AV*)SvRV(ST(3));
1473 parmlen=av_len(av)+1;
1474 parm=mymalloc( parmlen*sizeof(double) );
1475 for(i=0;i<parmlen;i++) { /* FIXME: Bug? */
1476 sv1=(*(av_fetch(av,i,0)));
1477 parm[i]=(double)SvNV(sv1);
1479 RETVAL=i_transform(im,opx,opxl,opy,opyl,parm,parmlen);
1483 ST(0) = sv_newmortal();
1484 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1485 else sv_setref_pv(ST(0), "Imager::ImgRaw", (void*)RETVAL);
1488 i_transform2(width,height,ops,n_regs,c_regs,in_imgs)
1494 unsigned int ops_len;
1507 if (!SvROK(ST(3))) croak("Imager: Parameter 4 must be a reference to an array\n");
1508 if (!SvROK(ST(4))) croak("Imager: Parameter 5 must be a reference to an array\n");
1509 if (!SvROK(ST(5))) croak("Imager: Parameter 6 must be a reference to an array of images\n");
1510 if (SvTYPE(SvRV(ST(3))) != SVt_PVAV) croak("Imager: Parameter 4 must be a reference to an array\n");
1511 if (SvTYPE(SvRV(ST(4))) != SVt_PVAV) croak("Imager: Parameter 5 must be a reference to an array\n");
1513 /*if (SvTYPE(SvRV(ST(5))) != SVt_PVAV) croak("Imager: Parameter 6 must be a reference to an array\n");*/
1515 if (SvTYPE(SvRV(ST(5))) == SVt_PVAV) {
1516 av = (AV*)SvRV(ST(5));
1517 in_imgs_count = av_len(av)+1;
1518 for (i = 0; i < in_imgs_count; ++i) {
1519 sv1 = *av_fetch(av, i, 0);
1520 if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
1521 croak("Parameter 5 must contain only images");
1528 if (SvTYPE(SvRV(ST(5))) == SVt_PVAV) {
1529 av = (AV*)SvRV(ST(5));
1530 in_imgs = mymalloc(in_imgs_count*sizeof(i_img*));
1531 for (i = 0; i < in_imgs_count; ++i) {
1532 sv1 = *av_fetch(av,i,0);
1533 if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
1534 croak("Parameter 5 must contain only images");
1536 tmp = SvIV((SV*)SvRV(sv1));
1537 in_imgs[i] = (i_img*)tmp;
1541 /* no input images */
1544 /* default the output size from the first input if possible */
1546 width = SvIV(ST(0));
1547 else if (in_imgs_count)
1548 width = in_imgs[0]->xsize;
1550 croak("No output image width supplied");
1553 height = SvIV(ST(1));
1554 else if (in_imgs_count)
1555 height = in_imgs[0]->ysize;
1557 croak("No output image height supplied");
1559 ops = (struct rm_op *)SvPV(ST(2), ops_len);
1560 if (ops_len % sizeof(struct rm_op))
1561 croak("Imager: Parameter 3 must be a bitmap of regops\n");
1562 ops_count = ops_len / sizeof(struct rm_op);
1563 av = (AV*)SvRV(ST(3));
1564 n_regs_count = av_len(av)+1;
1565 n_regs = mymalloc(n_regs_count * sizeof(double));
1566 for (i = 0; i < n_regs_count; ++i) {
1567 sv1 = *av_fetch(av,i,0);
1569 n_regs[i] = SvNV(sv1);
1571 av = (AV*)SvRV(ST(4));
1572 c_regs_count = av_len(av)+1;
1573 c_regs = mymalloc(c_regs_count * sizeof(i_color));
1574 /* I don't bother initializing the colou?r registers */
1576 RETVAL=i_transform2(width, height, 3, ops, ops_count,
1577 n_regs, n_regs_count,
1578 c_regs, c_regs_count, in_imgs, in_imgs_count);
1583 ST(0) = sv_newmortal();
1584 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1585 else sv_setref_pv(ST(0), "Imager::ImgRaw", (void*)RETVAL);
1589 i_contrast(im,intensity)
1598 i_noise(im,amount,type)
1604 i_bumpmap(im,bump,channel,light_x,light_y,strength)
1613 i_postlevels(im,levels)
1623 i_watermark(im,wmark,tx,ty,pixdiff)
1625 Imager::ImgRaw wmark
1632 i_autolevels(im,lsat,usat,skew)
1639 i_radnoise(im,xo,yo,rscale,ascale)
1647 i_turbnoise(im, xo, yo, scale)
1670 croak("Usage: i_gradgen(im, xo, yo, ival, dmeasure)");
1671 if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
1672 croak("i_gradgen: Second argument must be an array ref");
1673 if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
1674 croak("i_gradgen: Third argument must be an array ref");
1675 if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
1676 croak("i_gradgen: Fourth argument must be an array ref");
1677 axx = (AV *)SvRV(ST(1));
1678 ayy = (AV *)SvRV(ST(2));
1679 ac = (AV *)SvRV(ST(3));
1680 dmeasure = (int)SvIV(ST(4));
1682 num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
1683 num = num <= av_len(ac) ? num : av_len(ac);
1685 if (num < 2) croak("Usage: i_gradgen array refs must have more than 1 entry each");
1686 xo = mymalloc( sizeof(int) * num );
1687 yo = mymalloc( sizeof(int) * num );
1688 ival = mymalloc( sizeof(i_color) * num );
1689 for(i = 0; i<num; i++) {
1690 xo[i] = (int)SvIV(* av_fetch(axx, i, 0));
1691 yo[i] = (int)SvIV(* av_fetch(ayy, i, 0));
1692 sv = *av_fetch(ac, i, 0);
1693 if ( !sv_derived_from(sv, "Imager::Color") ) {
1694 free(axx); free(ayy); free(ac);
1695 croak("i_gradgen: Element of fourth argument is not derived from Imager::Color");
1697 ival[i] = *(i_color *)SvIV((SV *)SvRV(sv));
1699 i_gradgen(im, num, xo, yo, ival, dmeasure);
1715 errors = i_errors();
1717 while (errors[i].msg) {
1719 sv = newSVpv(errors[i].msg, strlen(errors[i].msg));
1720 if (!av_store(av, 0, sv)) {
1723 sv = newSViv(errors[i].code);
1724 if (!av_store(av, 1, sv)) {
1727 PUSHs(sv_2mortal(newRV_noinc((SV*)av)));
1732 i_nearest_color(im, ...)
1747 croak("Usage: i_nearest_color(im, xo, yo, ival, dmeasure)");
1748 if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
1749 croak("i_nearest_color: Second argument must be an array ref");
1750 if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
1751 croak("i_nearest_color: Third argument must be an array ref");
1752 if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
1753 croak("i_nearest_color: Fourth argument must be an array ref");
1754 axx = (AV *)SvRV(ST(1));
1755 ayy = (AV *)SvRV(ST(2));
1756 ac = (AV *)SvRV(ST(3));
1757 dmeasure = (int)SvIV(ST(4));
1759 num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
1760 num = num <= av_len(ac) ? num : av_len(ac);
1762 if (num < 2) croak("Usage: i_nearest_color array refs must have more than 1 entry each");
1763 xo = mymalloc( sizeof(int) * num );
1764 yo = mymalloc( sizeof(int) * num );
1765 ival = mymalloc( sizeof(i_color) * num );
1766 for(i = 0; i<num; i++) {
1767 xo[i] = (int)SvIV(* av_fetch(axx, i, 0));
1768 yo[i] = (int)SvIV(* av_fetch(ayy, i, 0));
1769 sv = *av_fetch(ac, i, 0);
1770 if ( !sv_derived_from(sv, "Imager::Color") ) {
1771 free(axx); free(ayy); free(ac);
1772 croak("i_nearest_color: Element of fourth argument is not derived from Imager::Color");
1774 ival[i] = *(i_color *)SvIV((SV *)SvRV(sv));
1776 i_nearest_color(im, num, xo, yo, ival, dmeasure);
1790 if (!SvROK(ST(0))) croak("Imager: Parameter 0 must be a reference to a hash\n");
1791 hv=(HV*)SvRV(ST(0));
1792 if (SvTYPE(hv)!=SVt_PVHV) croak("Imager: Parameter 0 must be a reference to a hash\n");
1793 if (getint(hv,"stuff",&stuff)) printf("ok: %d\n",stuff); else printf("key doesn't exist\n");
1794 if (getint(hv,"stuff2",&stuff)) printf("ok: %d\n",stuff); else printf("key doesn't exist\n");
1803 rc=DSO_open(filename,&evstr);
1807 PUSHs(sv_2mortal(newSViv((IV)rc)));
1808 PUSHs(sv_2mortal(newSVpvn(evstr, strlen(evstr))));
1811 PUSHs(sv_2mortal(newSViv((IV)rc)));
1817 DSO_close(dso_handle)
1821 DSO_funclist(dso_handle_v)
1825 DSO_handle *dso_handle;
1827 dso_handle=(DSO_handle*)dso_handle_v;
1829 while( dso_handle->function_list[i].name != NULL) {
1831 PUSHs(sv_2mortal(newSVpv(dso_handle->function_list[i].name,0)));
1833 PUSHs(sv_2mortal(newSVpv(dso_handle->function_list[i++].pcode,0)));
1838 DSO_call(handle,func_index,hv)
1844 if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to a hash\n");
1845 hv=(HV*)SvRV(ST(2));
1846 if (SvTYPE(hv)!=SVt_PVHV) croak("Imager: Parameter 2 must be a reference to a hash\n");
1847 DSO_call( (DSO_handle *)handle,func_index,hv);