In fact, as an addendum to my previous message, check out this version.
Here I create the large raster (100 x 100, 30000 datapoints)...you can
specify any width and height for TIFFToCanvas LARGER THAN 32 (but still less
than or equal to 100), and it works.  32 or less, it creates errors.

Again, this only occurs under Windows, not Linux (I should add that I am
running Win2000, with the most recent Active State build and most recent
Inline package).

#!perl;
#
# error.pl
#
# A little script which is meant to convert an array of 
# pixel intensities (3 values per pixel) to the raw 
# Tk::Canvas data format (#hhhhhh, where hh is some hex
# value, 0-255).
#
# Unfortunately, it generates errors under windows.
#

use Inline C => DATA; 

for ($x=0; $x<30000; $x++)
{
  push @$imageData, 255;
}

for ($i = 1; $i < 1000; $i++)
{
  print "Attempts TIFFToCanvas call, iteration # $i\n";
  #
  # Substitute a value less than or equal to 32 for each of 
  # the 50's below, and you should see errors under Windows
  #
  $canvasData = TIFFToCanvas($imageData, 50, 50);
}

exit;

__END__

__C__


SV* TIFFToCanvas(SV* svImage, int w, int h)
{
  AV *avImage, *avCanvas;
  int x, y; // loop indices, and the image area
  SV *r, *g, *b, *s; 
  char *pixel = NULL; // a single pixel in #hhhhhh (Canvas) format
  char *pixels = NULL; // the entire row of pixels in Canvas format

  avCanvas = newAV(); // an array to hold transformed data
  r = sv_newmortal(); // the red channel
  g = sv_newmortal(); // the green channel
  b = sv_newmortal(); // the blue channel

  //Extract and dereference image array to AV*
  avImage = (AV*)SvRV(svImage);

  // allocate some memory
  pixel = (char*)malloc(8 * sizeof(char));
  pixels = (char*)malloc(8 * w * sizeof(char));
  if (pixel == NULL)
           handle_error(1, 1);
  if (pixels == NULL)
           handle_error(1, 2);

  //iterate through rows and columns of image data, 
  //and covert to #hhhhhh format             
  for(y = 0; y < h; y++)
  {
    strcpy(pixels, "");
    for (x = 0; x < w; x++)
    {
      r = *av_fetch(avImage, ((y * w) + x) * 3, 0); 
      g = *av_fetch(avImage, ((y * w) + x) * 3 + 1, 0); 
      b = *av_fetch(avImage, ((y * w) + x) * 3 + 2, 0); 
      sprintf(pixel, "#%02X%02X%02X ", SvIV(r), SvIV(g), SvIV(b));
      strcat(pixels, pixel);
    }
    s = newSVpv(pixels, 0);
    av_push(avCanvas, s);
  }    
  free(pixel);
  free(pixels);

  return newRV_noinc((SV*)(avCanvas));
}

int handle_error (int iErrNo, int iTrap)
{
  printf("Error %d at trap %d\n\n", iErrNo, iTrap);
  exit(0);
  return 0;
} 

Reply via email to