Hello, I would like to be able to pass the memory of GD::Image object to the inline C code instead of saving to file and then opening that in the inline C. Is this possible and if so, can someone lead me to some docs or examples...

use Inline C => DATA => LIBS => '-lgd'; use GD::Image; use Barcode::Code128; my $labelgd = new GD::Image(808,200); my $white = $labelgd->colorAllocate(255,255,255); my $black = $labelgd->colorAllocate(0,0,0); my $red = $labelgd->colorAllocate(255, 0, 0); my $arg_ref; my @text; push (@text,{ 'ptsize' => 112, 'xcoord' => 40, 'ycoord' => 135, 'text' + => 'location' }); push (@text,{ 'ptsize' => 14, 'xcoord' => 40, 'ycoord' => 190, 'text' +=> 'L'.'recid' }); $arg_ref->{'text'} = \@text; # Process Text objects to be placed on label my $font = '/usr/share/fonts/truetype/liberation/LiberationMono-Bold.t +tf'; if ($arg_ref->{'text'}) { for (my $i = 0; $i < scalar @{$arg_ref->{'text'}}; $i++) { $labelgd->stringFT( -$black, $font, @{$arg_ref->{'text'}[$i]}{'ptsize'}, @{$arg_ref->{'text'}[$i]}{'angle'} ? @{$arg_ref->{'text'}[ +$i]}{'angle'} : 0, @{$arg_ref->{'text'}[$i]}{'xcoord'}, @{$arg_ref->{'text'}[$i]}{'ycoord'}, @{$arg_ref->{'text'}[$i]}{'text'} ); } } my $labelgdfile = "label.png"; open (PNG, "> ".$labelgdfile) or die "Can't write $labelgdfile: $!\n"; binmode (PNG); print PNG $labelgd->png; close (PNG); my $raw_image = pack "H*" , process_image($labelgdfile); # ~warn $raw_image,$raw_width,$raw_height; #~ warn $raw_image; __END__ __C__ #include <gd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void abort_(const char * s, ...) { va_list args; va_start(args, s); vfprintf(stderr, s, args); fprintf(stderr, "\n"); va_end(args); abort(); } SV* process_image(char* file_name) { FILE *fp = fopen(file_name, "rb"); if (!fp) abort_("[read_png_file] File %s could not be opened fo +r reading", file_name); gdImagePtr im; im = gdImageCreateFromPng(fp); char *raw_hex_image_string = (char *)malloc( ( gdImageSX(im) * + gdImageSY(im))+1); raw_hex_image_string[0] = '\0'; int y; for (y=0; y<gdImageSY(im); y++) { int x; for (x=0; x<gdImageSX(im); x+=4) { int four_tuple[4]; int k = 0; for ( k=0; k<= 3; k++) { int pixel; pixel = gdImageGetPixel(im,(x+k),y); if (gdImageRed(im,pixel) > 0 || gdImag +eGreen(im,pixel) > 0 || gdImageBlue(im,pixel) > 0) { four_tuple[k] = 1; } else { four_tuple[k] = 0; } } int first_nibble = 0; first_nibble = (first_nibble << 1) | four_tupl +e[0]; first_nibble = (first_nibble << 1) | four_tupl +e[1]; first_nibble = (first_nibble << 1) | four_tupl +e[2]; first_nibble = (first_nibble << 1) | four_tupl +e[3]; char hexval; switch ( first_nibble ) { case 0: hexval = '0'; break; case 1: hexval = '1'; break; case 2: hexval = '2'; break; case 3: hexval = '3'; break; case 4: hexval = '4'; break; case 5: hexval = '5'; break; case 6: hexval = '6'; break; case 7: hexval = '7'; break; case 8: hexval = '8'; break; case 9: hexval = '9'; break; case 10: hexval = 'A'; break; case 11: hexval = 'B'; break; case 12: hexval = 'C'; break; case 13: hexval = 'D'; break; case 14: hexval = 'E'; break; case 15: hexval = 'F'; break; default: hexval = 'F'; break; } int o; for(o=0; raw_hex_image_string[o]!='\0'; ++o); raw_hex_image_string[o]=hexval; raw_hex_image_string[o+1]='\0'; } } gdImageDestroy(im); fclose(fp); SV * retval = newSVpvf("%s", raw_hex_image_string); free(raw_hex_image_string); return retval; }

In reply to Passing GD::Image object to Inline C by josiah47

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.