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)
764 if (!SvROK(ST(1))) croak("Imager: Parameter 1 must be a reference to an array\n");
765 if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 must be a reference to an array\n");
768 coeff=mymalloc( len*sizeof(float) );
770 sv1=(*(av_fetch(av,i,0)));
771 coeff[i]=(float)SvNV(sv1);
773 i_conv(im,coeff,len);
794 i_t1_new(pfb,afm=NULL)
799 i_t1_destroy(font_id)
804 i_t1_cp(im,xb,yb,channel,fontnum,points,str,len,align)
816 i_t1_bbox(fontnum,point,str,len)
824 i_t1_bbox(fontnum,point,str,len,cords);
826 PUSHs(sv_2mortal(newSViv(cords[0])));
827 PUSHs(sv_2mortal(newSViv(cords[1])));
828 PUSHs(sv_2mortal(newSViv(cords[2])));
829 PUSHs(sv_2mortal(newSViv(cords[3])));
830 PUSHs(sv_2mortal(newSViv(cords[4])));
831 PUSHs(sv_2mortal(newSViv(cords[5])));
836 i_t1_text(im,xb,yb,cl,fontnum,points,str,len,align)
858 Imager::TTHandle handle
863 i_tt_text(handle,im,xb,yb,cl,points,str,len,smooth)
864 Imager::TTHandle handle
876 i_tt_cp(handle,im,xb,yb,channel,points,str,len,smooth)
877 Imager::TTHandle handle
890 i_tt_bbox(handle,point,str,len)
891 Imager::TTHandle handle
898 if ((rc=i_tt_bbox(handle,point,str,len,cords))) {
900 PUSHs(sv_2mortal(newSViv(cords[0])));
901 PUSHs(sv_2mortal(newSViv(cords[1])));
902 PUSHs(sv_2mortal(newSViv(cords[2])));
903 PUSHs(sv_2mortal(newSViv(cords[3])));
904 PUSHs(sv_2mortal(newSViv(cords[4])));
905 PUSHs(sv_2mortal(newSViv(cords[5])));
916 i_writejpeg(im,fd,qfactor)
931 rimg=i_readjpeg(fd,&iptc_itext,&tlength);
932 if (iptc_itext == NULL) {
935 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
940 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
942 PUSHs(sv_2mortal(newSVpv(iptc_itext,tlength)));
948 i_readjpeg_scalar(...)
959 data = (char *)SvPV(ST(0), length);
960 rimg=i_readjpeg_scalar(data,length,&iptc_itext,&tlength);
961 mm_log((1,"i_readjpeg_scalar: 0x%08X\n",rimg));
962 if (iptc_itext == NULL) {
965 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
970 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
972 PUSHs(sv_2mortal(newSVpv(iptc_itext,tlength)));
987 rimg = i_readjpeg_wiol(ig,-1,&iptc_itext,&tlength);
988 if (iptc_itext == NULL) {
991 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
996 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
998 PUSHs(sv_2mortal(newSVpv(iptc_itext,tlength)));
1011 i_readtiff_wiol(ig, length)
1017 i_writetiff_wiol(im, ig)
1022 #endif /* HAVE_LIBTIFF */
1042 i_readpng_scalar(...)
1046 unsigned int length;
1048 data = (char *)SvPV(ST(0), length);
1049 RETVAL=i_readpng_scalar(data,length);
1063 PUSHs(sv_2mortal(newSVnv(IM_GIFMAJOR+IM_GIFMINOR*0.1)));
1066 i_writegif(im,fd,colors,pixdev,fixed)
1073 Imager__Color fixed;
1080 if (!SvROK(ST(4))) croak("Imager: Parameter 4 must be a reference to an array\n");
1081 if (SvTYPE(SvRV(ST(4))) != SVt_PVAV) croak("Imager: Parameter 4 must be a reference to an array\n");
1082 av=(AV*)SvRV(ST(4));
1083 fixedlen=av_len(av)+1;
1084 fixed=mymalloc( fixedlen*sizeof(i_color) );
1085 for(i=0;i<fixedlen;i++) {
1086 sv1=(*(av_fetch(av,i,0)));
1087 if (sv_derived_from(sv1, "Imager::Color")) {
1088 Itmp = SvIV((SV*)SvRV(sv1));
1089 tmp = (i_color*) Itmp;
1090 } else croak("Imager: one of the elements of array ref is not of Imager::Color type\n");
1093 RETVAL=i_writegif(im,fd,colors,pixdev,fixedlen,fixed);
1095 ST(0) = sv_newmortal();
1096 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1097 else sv_setiv(ST(0), (IV)RETVAL);
1103 i_writegifmc(im,fd,colors)
1114 i_writegif_gen(fd, ...)
1120 i_img **imgs = NULL;
1126 croak("Usage: i_writegif_gen(fd,hashref, images...)");
1127 if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
1128 croak("i_writegif_gen: Second argument must be a hash ref");
1129 hv = (HV *)SvRV(ST(1));
1130 memset(&quant, 0, sizeof(quant));
1131 quant.mc_size = 256;
1132 quant.mc_colors = mymalloc(quant.mc_size * sizeof(i_color));
1133 memset(&opts, 0, sizeof(opts));
1134 handle_quant_opts(&quant, hv);
1135 handle_gif_opts(&opts, hv);
1136 img_count = items - 2;
1138 if (img_count < 1) {
1141 i_push_error(0, "You need to specify images to save");
1144 imgs = mymalloc(sizeof(i_img *) * img_count);
1145 for (i = 0; i < img_count; ++i) {
1148 if (SvROK(sv) && sv_derived_from(sv, "Imager::ImgRaw")) {
1149 imgs[i] = (i_img *)SvIV((SV*)SvRV(sv));
1153 i_push_error(0, "Only images can be saved");
1159 RETVAL = i_writegif_gen(&quant, fd, imgs, img_count, &opts);
1163 copy_colors_back(hv, &quant);
1166 ST(0) = sv_newmortal();
1167 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1168 else sv_setiv(ST(0), (IV)RETVAL);
1171 i_writegif_callback(cb, maxbuffer,...)
1176 i_img **imgs = NULL;
1183 croak("Usage: i_writegif_callback(\\&callback,maxbuffer,hashref, images...)");
1184 if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
1185 croak("i_writegif_callback: Second argument must be a hash ref");
1186 hv = (HV *)SvRV(ST(2));
1187 memset(&quant, 0, sizeof(quant));
1188 quant.mc_size = 256;
1189 quant.mc_colors = mymalloc(quant.mc_size * sizeof(i_color));
1190 memset(&opts, 0, sizeof(opts));
1191 handle_quant_opts(&quant, hv);
1192 handle_gif_opts(&opts, hv);
1193 img_count = items - 3;
1195 if (img_count < 1) {
1199 imgs = mymalloc(sizeof(i_img *) * img_count);
1200 for (i = 0; i < img_count; ++i) {
1203 if (SvROK(sv) && sv_derived_from(sv, "Imager::ImgRaw")) {
1204 imgs[i] = (i_img *)SvIV((SV*)SvRV(sv));
1213 RETVAL = i_writegif_callback(&quant, write_callback, (char *)&wd, maxbuffer, imgs, img_count, &opts);
1217 copy_colors_back(hv, &quant);
1220 ST(0) = sv_newmortal();
1221 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1222 else sv_setiv(ST(0), (IV)RETVAL);
1235 colour_table = NULL;
1238 if(GIMME_V == G_ARRAY) {
1239 rimg = i_readgif(fd,&colour_table,&colours);
1241 /* don't waste time with colours if they aren't wanted */
1242 rimg = i_readgif(fd,NULL,NULL);
1245 if (colour_table == NULL) {
1248 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1251 /* the following creates an [[r,g,b], [r, g, b], [r, g, b]...] */
1252 /* I don't know if I have the reference counts right or not :( */
1253 /* Neither do I :-) */
1254 /* No Idea here either */
1257 av_extend(ct, colours);
1258 for(q=0; q<colours; q++) {
1260 temp[w]=sv_2mortal(newSViv(colour_table[q*3 + w]));
1261 av_store(ct, q, (SV*)newRV_noinc((SV*)av_make(3, temp)));
1263 myfree(colour_table);
1267 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1269 PUSHs(newRV_noinc((SV*)ct));
1277 i_readgif_scalar(...)
1281 unsigned int length;
1289 data = (char *)SvPV(ST(0), length);
1293 if(GIMME_V == G_ARRAY) {
1294 rimg=i_readgif_scalar(data,length,&colour_table,&colours);
1296 /* don't waste time with colours if they aren't wanted */
1297 rimg=i_readgif_scalar(data,length,NULL,NULL);
1300 if (colour_table == NULL) {
1303 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1306 /* the following creates an [[r,g,b], [r, g, b], [r, g, b]...] */
1307 /* I don't know if I have the reference counts right or not :( */
1308 /* Neither do I :-) */
1310 av_extend(ct, colours);
1311 for(q=0; q<colours; q++) {
1313 temp[w]=sv_2mortal(newSViv(colour_table[q*3 + w]));
1314 av_store(ct, q, (SV*)newRV_noinc((SV*)av_make(3, temp)));
1316 myfree(colour_table);
1320 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1322 PUSHs(newRV_noinc((SV*)ct));
1326 i_readgif_callback(...)
1343 if(GIMME_V == G_ARRAY) {
1344 rimg=i_readgif_callback(read_callback, (char *)&rd,&colour_table,&colours);
1346 /* don't waste time with colours if they aren't wanted */
1347 rimg=i_readgif_callback(read_callback, (char *)&rd,NULL,NULL);
1350 if (colour_table == NULL) {
1353 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1356 /* the following creates an [[r,g,b], [r, g, b], [r, g, b]...] */
1357 /* I don't know if I have the reference counts right or not :( */
1358 /* Neither do I :-) */
1359 /* Neither do I - maybe I'll move this somewhere */
1361 av_extend(ct, colours);
1362 for(q=0; q<colours; q++) {
1364 temp[w]=sv_2mortal(newSViv(colour_table[q*3 + w]));
1365 av_store(ct, q, (SV*)newRV_noinc((SV*)av_make(3, temp)));
1367 myfree(colour_table);
1371 sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg);
1373 PUSHs(newRV_noinc((SV*)ct));
1386 i_readpnm_wiol(ig, length)
1397 i_readraw(fd,x,y,datachannels,storechannels,intrl)
1412 i_scaleaxis(im,Value,Axis)
1418 i_scale_nn(im,scx,scy)
1428 i_count_colors(im,maxc)
1434 i_transform(im,opx,opy,parm)
1447 if (!SvROK(ST(1))) croak("Imager: Parameter 1 must be a reference to an array\n");
1448 if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to an array\n");
1449 if (!SvROK(ST(3))) croak("Imager: Parameter 3 must be a reference to an array\n");
1450 if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 must be a reference to an array\n");
1451 if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 2 must be a reference to an array\n");
1452 if (SvTYPE(SvRV(ST(3))) != SVt_PVAV) croak("Imager: Parameter 3 must be a reference to an array\n");
1453 av=(AV*)SvRV(ST(1));
1455 opx=mymalloc( opxl*sizeof(int) );
1456 for(i=0;i<opxl;i++) {
1457 sv1=(*(av_fetch(av,i,0)));
1458 opx[i]=(int)SvIV(sv1);
1460 av=(AV*)SvRV(ST(2));
1462 opy=mymalloc( opyl*sizeof(int) );
1463 for(i=0;i<opyl;i++) {
1464 sv1=(*(av_fetch(av,i,0)));
1465 opy[i]=(int)SvIV(sv1);
1467 av=(AV*)SvRV(ST(3));
1468 parmlen=av_len(av)+1;
1469 parm=mymalloc( parmlen*sizeof(double) );
1470 for(i=0;i<parmlen;i++) { /* FIXME: Bug? */
1471 sv1=(*(av_fetch(av,i,0)));
1472 parm[i]=(double)SvNV(sv1);
1474 RETVAL=i_transform(im,opx,opxl,opy,opyl,parm,parmlen);
1478 ST(0) = sv_newmortal();
1479 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1480 else sv_setref_pv(ST(0), "Imager::ImgRaw", (void*)RETVAL);
1483 i_transform2(width,height,ops,n_regs,c_regs,in_imgs)
1489 unsigned int ops_len;
1502 if (!SvROK(ST(3))) croak("Imager: Parameter 4 must be a reference to an array\n");
1503 if (!SvROK(ST(4))) croak("Imager: Parameter 5 must be a reference to an array\n");
1504 if (!SvROK(ST(5))) croak("Imager: Parameter 6 must be a reference to an array of images\n");
1505 if (SvTYPE(SvRV(ST(3))) != SVt_PVAV) croak("Imager: Parameter 4 must be a reference to an array\n");
1506 if (SvTYPE(SvRV(ST(4))) != SVt_PVAV) croak("Imager: Parameter 5 must be a reference to an array\n");
1508 /*if (SvTYPE(SvRV(ST(5))) != SVt_PVAV) croak("Imager: Parameter 6 must be a reference to an array\n");*/
1510 if (SvTYPE(SvRV(ST(5))) == SVt_PVAV) {
1511 av = (AV*)SvRV(ST(5));
1512 in_imgs_count = av_len(av)+1;
1513 for (i = 0; i < in_imgs_count; ++i) {
1514 sv1 = *av_fetch(av, i, 0);
1515 if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
1516 croak("Parameter 5 must contain only images");
1523 if (SvTYPE(SvRV(ST(5))) == SVt_PVAV) {
1524 av = (AV*)SvRV(ST(5));
1525 in_imgs = mymalloc(in_imgs_count*sizeof(i_img*));
1526 for (i = 0; i < in_imgs_count; ++i) {
1527 sv1 = *av_fetch(av,i,0);
1528 if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
1529 croak("Parameter 5 must contain only images");
1531 tmp = SvIV((SV*)SvRV(sv1));
1532 in_imgs[i] = (i_img*)tmp;
1536 /* no input images */
1539 /* default the output size from the first input if possible */
1541 width = SvIV(ST(0));
1542 else if (in_imgs_count)
1543 width = in_imgs[0]->xsize;
1545 croak("No output image width supplied");
1548 height = SvIV(ST(1));
1549 else if (in_imgs_count)
1550 height = in_imgs[0]->ysize;
1552 croak("No output image height supplied");
1554 ops = (struct rm_op *)SvPV(ST(2), ops_len);
1555 if (ops_len % sizeof(struct rm_op))
1556 croak("Imager: Parameter 3 must be a bitmap of regops\n");
1557 ops_count = ops_len / sizeof(struct rm_op);
1558 av = (AV*)SvRV(ST(3));
1559 n_regs_count = av_len(av)+1;
1560 n_regs = mymalloc(n_regs_count * sizeof(double));
1561 for (i = 0; i < n_regs_count; ++i) {
1562 sv1 = *av_fetch(av,i,0);
1564 n_regs[i] = SvNV(sv1);
1566 av = (AV*)SvRV(ST(4));
1567 c_regs_count = av_len(av)+1;
1568 c_regs = mymalloc(c_regs_count * sizeof(i_color));
1569 /* I don't bother initializing the colou?r registers */
1571 RETVAL=i_transform2(width, height, 3, ops, ops_count,
1572 n_regs, n_regs_count,
1573 c_regs, c_regs_count, in_imgs, in_imgs_count);
1578 ST(0) = sv_newmortal();
1579 if (RETVAL == 0) ST(0)=&PL_sv_undef;
1580 else sv_setref_pv(ST(0), "Imager::ImgRaw", (void*)RETVAL);
1584 i_contrast(im,intensity)
1593 i_noise(im,amount,type)
1599 i_bumpmap(im,bump,channel,light_x,light_y,strength)
1608 i_postlevels(im,levels)
1618 i_watermark(im,wmark,tx,ty,pixdiff)
1620 Imager::ImgRaw wmark
1627 i_autolevels(im,lsat,usat,skew)
1634 i_radnoise(im,xo,yo,rscale,ascale)
1642 i_turbnoise(im, xo, yo, scale)
1665 croak("Usage: i_gradgen(im, xo, yo, ival, dmeasure)");
1666 if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
1667 croak("i_gradgen: Second argument must be an array ref");
1668 if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
1669 croak("i_gradgen: Third argument must be an array ref");
1670 if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
1671 croak("i_gradgen: Fourth argument must be an array ref");
1672 axx = (AV *)SvRV(ST(1));
1673 ayy = (AV *)SvRV(ST(2));
1674 ac = (AV *)SvRV(ST(3));
1675 dmeasure = (int)SvIV(ST(4));
1677 num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
1678 num = num <= av_len(ac) ? num : av_len(ac);
1680 if (num < 2) croak("Usage: i_gradgen array refs must have more than 1 entry each");
1681 xo = mymalloc( sizeof(int) * num );
1682 yo = mymalloc( sizeof(int) * num );
1683 ival = mymalloc( sizeof(i_color) * num );
1684 for(i = 0; i<num; i++) {
1685 xo[i] = (int)SvIV(* av_fetch(axx, i, 0));
1686 yo[i] = (int)SvIV(* av_fetch(ayy, i, 0));
1687 sv = *av_fetch(ac, i, 0);
1688 if ( !sv_derived_from(sv, "Imager::Color") ) {
1689 free(axx); free(ayy); free(ac);
1690 croak("i_gradgen: Element of fourth argument is not derived from Imager::Color");
1692 ival[i] = *(i_color *)SvIV((SV *)SvRV(sv));
1694 i_gradgen(im, num, xo, yo, ival, dmeasure);
1710 errors = i_errors();
1712 while (errors[i].msg) {
1714 sv = newSVpv(errors[i].msg, strlen(errors[i].msg));
1715 if (!av_store(av, 0, sv)) {
1718 sv = newSViv(errors[i].code);
1719 if (!av_store(av, 1, sv)) {
1722 PUSHs(sv_2mortal(newRV_noinc((SV*)av)));
1727 i_nearest_color(im, ...)
1742 croak("Usage: i_nearest_color(im, xo, yo, ival, dmeasure)");
1743 if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
1744 croak("i_nearest_color: Second argument must be an array ref");
1745 if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
1746 croak("i_nearest_color: Third argument must be an array ref");
1747 if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
1748 croak("i_nearest_color: Fourth argument must be an array ref");
1749 axx = (AV *)SvRV(ST(1));
1750 ayy = (AV *)SvRV(ST(2));
1751 ac = (AV *)SvRV(ST(3));
1752 dmeasure = (int)SvIV(ST(4));
1754 num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
1755 num = num <= av_len(ac) ? num : av_len(ac);
1757 if (num < 2) croak("Usage: i_nearest_color array refs must have more than 1 entry each");
1758 xo = mymalloc( sizeof(int) * num );
1759 yo = mymalloc( sizeof(int) * num );
1760 ival = mymalloc( sizeof(i_color) * num );
1761 for(i = 0; i<num; i++) {
1762 xo[i] = (int)SvIV(* av_fetch(axx, i, 0));
1763 yo[i] = (int)SvIV(* av_fetch(ayy, i, 0));
1764 sv = *av_fetch(ac, i, 0);
1765 if ( !sv_derived_from(sv, "Imager::Color") ) {
1766 free(axx); free(ayy); free(ac);
1767 croak("i_nearest_color: Element of fourth argument is not derived from Imager::Color");
1769 ival[i] = *(i_color *)SvIV((SV *)SvRV(sv));
1771 i_nearest_color(im, num, xo, yo, ival, dmeasure);
1785 if (!SvROK(ST(0))) croak("Imager: Parameter 0 must be a reference to a hash\n");
1786 hv=(HV*)SvRV(ST(0));
1787 if (SvTYPE(hv)!=SVt_PVHV) croak("Imager: Parameter 0 must be a reference to a hash\n");
1788 if (getint(hv,"stuff",&stuff)) printf("ok: %d\n",stuff); else printf("key doesn't exist\n");
1789 if (getint(hv,"stuff2",&stuff)) printf("ok: %d\n",stuff); else printf("key doesn't exist\n");
1798 rc=DSO_open(filename,&evstr);
1802 PUSHs(sv_2mortal(newSViv((IV)rc)));
1803 PUSHs(sv_2mortal(newSVpvn(evstr, strlen(evstr))));
1806 PUSHs(sv_2mortal(newSViv((IV)rc)));
1812 DSO_close(dso_handle)
1816 DSO_funclist(dso_handle_v)
1820 DSO_handle *dso_handle;
1822 dso_handle=(DSO_handle*)dso_handle_v;
1824 while( dso_handle->function_list[i].name != NULL) {
1826 PUSHs(sv_2mortal(newSVpv(dso_handle->function_list[i].name,0)));
1828 PUSHs(sv_2mortal(newSVpv(dso_handle->function_list[i++].pcode,0)));
1833 DSO_call(handle,func_index,hv)
1839 if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to a hash\n");
1840 hv=(HV*)SvRV(ST(2));
1841 if (SvTYPE(hv)!=SVt_PVHV) croak("Imager: Parameter 2 must be a reference to a hash\n");
1842 DSO_call( (DSO_handle *)handle,func_index,hv);