pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Advertising: Mortgages - Mortgages - online loans - Arizona Pools
Perl paste-bin - collaborative debugging tool View Help

Posted by Anonymous on Sun 28th Oct 16:26 (modification of post by view diff)
download | new post

  1. package ARS::Coupon::OMR::Clock;
  2.  
  3. require 5.008;
  4.  
  5. use strict;
  6. use List::Util qw(sum);
  7. use Exporter;
  8.  
  9. use base 'Exporter';
  10.  
  11. our %EXPORT_TAGS = (
  12.         'all' => [qw(
  13.                 DISABLE_CACHE  ENABLE_CACHE
  14.                 DISABLE_DRAW   ENABLE_DRAW
  15.                 LINEBIT_IDENTIFIER
  16.                 ON_CLOCK       AFTER_CLOCK
  17.                 SEARCH_DOWN    SEARCH_UP
  18.                 BLACK_PIXEL    WHITE_PIXEL
  19.         )],
  20. );
  21.  
  22. our @EXPORT_OK = (
  23.         @{ $EXPORT_TAGS{'all'} },
  24. );
  25.  
  26. our $VERSION = '1.00';
  27.  
  28. =head1 Name
  29.  
  30. ARS::Coupon::OMR::Clock - Optical Mark Recognition for clock based coupons
  31.  
  32. =head1 VERSION
  33.  
  34. Version 1.0
  35.  
  36. =head1 SYNOPSIS
  37.  
  38.     use ARS::Coupon::OMR::Clock;
  39.     $omr = ARS::Coupon::OMR::Clock->new($img, $struct);
  40.  
  41. =head1 DESCRIPTION
  42.  
  43. C<ARS::Coupon::OMR::Clock> provides an object interface to process an
  44. Image::Magick image resource, converting it into a grid reference
  45.  
  46. =head1 CONSTANTS
  47.  
  48. =item (CACHE) DISABLE_CACHE, ENABLE_CACHE
  49.  
  50. Enable or disable caching
  51.  
  52. =item (DRAW) DISABLE_DRAW, ENABLE_DRAW
  53.  
  54. Enable or disable drawing
  55.  
  56. =item (IDTYPE) LINEBIT_IDENTIFIER
  57.  
  58. The type of identifier to use, only LINEBIT_IDENTIFIER exists at the
  59. moment.
  60.  
  61. =item (CLOCK) ON_CLOCK, AFTER_CLOCK
  62.  
  63. Where to look for cells, on the clock or after the clock?
  64.  
  65. =item (SEARCH) SEARCH_DOWN, SEARCH_UP
  66.  
  67. Which direction to search in
  68.  
  69. =item (COLOR) BLACK_PIXEL, WHITE_PIXEL
  70.  
  71. What color pixel are we looking for
  72.  
  73. =cut
  74.  
  75. use constant DISABLE_CACHE      => 0;
  76. use constant ENABLE_CACHE       => 1;
  77.  
  78. use constant DISABLE_DRAW       => 0;
  79. use constant ENABLE_DRAW        => 1;
  80.  
  81. use constant LINEBIT_IDENTIFIER => 0;
  82.  
  83. use constant LEFT_ALIGNED       => 0;
  84. use constant RIGHT_ALIGNED      => 1;
  85.  
  86. use constant ON_CLOCK           => 0;
  87. use constant AFTER_CLOCK        => 1;
  88.  
  89. use constant SEARCH_DOWN        => 0;
  90. use constant SEARCH_UP          => 1;
  91.  
  92. use constant BLACK_PIXEL        => 0;
  93. use constant WHITE_PIXEL        => 1;
  94.  
  95. =head1 CONSTRUCTOR
  96.  
  97. =over 4
  98.  
  99. =item new ( $img, $struct[, CACHE, DRAW] )
  100.  
  101.         $img is an Image::Magick image handle
  102.         $struct is the structure defined below
  103.         CACHE (default ENABLE_CACHE) set to DISABLE_CACHE to disable caching of information
  104.         DRAW (default DISABLE_DRAW) set to ENABLE_DRAW to write back to the canvas/stdout
  105.  
  106. =item $struct
  107.  
  108. This is a basic definition of the structure the library needs to do the dirty work.
  109.  
  110. All the numbers below are percentages (0.5 = 50%) of page dimensions
  111.  
  112.         $strut = {
  113.                 sensitivity     => 0.5,         # Global Sensitivity - Optional, defaults to 0.01
  114.                 # Clock defines the parameters of the clock and is required once
  115.                 clock => {
  116.                         indent => 0.0123,           # How far to look in from the side for the clock marks
  117.                         side => RIGHT_ALIGNED,      # Which side is the clock on (LEFT_ALIGNED / RIGHT_ALIGNED)
  118.                         type => AFTER_CLOCK,        # ON_CLOCK / AFTER_CLOCK
  119.                         width => 0.01845,           # Approx width of the black clock mark (only used as a guide)
  120.                         height => 0.01231,          # The approx height of the black clock mark (Again only used as a guide)
  121.                         padding => 0.01212,         # The approx spacing between black clock marks (see above)
  122.                         after => 0.1294,            # (Optional) Only start looking X distance down the page for the clock
  123.                         before => 0.9481,           # (Optional) Don't look any further then X distance down the page
  124.                 },
  125.                 # Identifier defines the parameters of an identifier and is optional, but can only be used once
  126.                 # if Identifier is not supplied it guesses that the identifier is the first row of results
  127.                 identifier => {
  128.                         after => 0.1294,            # (Optional) Only start looking X distance down the page for the identifier
  129.                         before => 0.9481,           # (Optional) Don't look any further then X distance down the page
  130.                         width => 0.01845,           # Approx width of the identifier
  131.                         sensitivity => 0.4,         # How much black pixel coverage are we looking for (1px X widthpx)
  132.                         type => LINEBIT_IDENTIFIER, # Reserved, required (IDTYPE)
  133.                         side => LEFT_ALIGNED,       # Which side to look on (LEFT_ALIGNED / RIGHT_ALIGNED)
  134.                 },
  135.                 # Cell lets you define the basic parameters of the cells, you can have more then one
  136.                 # definition - this is required
  137.                 cell => {
  138.                         default => {                # If no other definition exists, use this one
  139.                                 width => 0.123,         # The width of the cell
  140.                                 indent => 0.123,        # Padding between the cell and the reference side
  141.                                 side => RIGHT_ALIGNED,  # Reference side - (LEFT_ALIGNED / RIGHT_ALIGNED)
  142.                                 padding => 0.123,       # Padding between cells
  143.                         }
  144.                         1234 => {                   # Special configuration for game identified as 1234
  145.                                 width => 0.123,         # The width of the cell
  146.                                 indent => 0.123,        # Padding between the cell and the reference side
  147.                                 side => RIGHT_ALIGNED,  # Reference side - (LEFT_ALIGNED / RIGHT_ALIGNED)
  148.                                 padding => 0.123,       # Padding between cells
  149.                         }
  150.                 },
  151.         }
  152.  
  153. =back
  154.  
  155. =cut
  156.  
  157. sub new {
  158.         my $this  = shift;
  159.         my $class = ref($this) || $this;
  160.  
  161.         my $self  = {
  162.                 image   => shift,
  163.                 struct  => shift,
  164.                 caching => shift || ENABLE_CACHE,
  165.                 draw    => shift || DISABLE_DRAW,
  166.                 cache   => {},
  167.         };
  168.  
  169.         bless $self, $class;
  170.  
  171.         my $image_width  = $self->{image}->Get('width');
  172.         my $image_height = $self->{image}->Get('height');
  173.  
  174.         # Calculate the real pixel sizes based on the percentages in the structure
  175.         $self->{clock_height}  = int($image_height * $self->{struct}->{clock}->{height});
  176.         $self->{clock_width}   = int($image_width * $self->{struct}->{clock}->{width});
  177.         $self->{clock_padding} = int($image_width * $self->{struct}->{clock}->{padding});
  178.         $self->{clock_indent}  = int($image_width * $self->{struct}->{clock}->{indent});
  179.  
  180.         $self->{image_height}  = $image_height;
  181.         $self->{image_width}   = $image_width;
  182.  
  183.         # We do our quick searches by halves, technically could do it by 1.1 or something
  184.         # but I dowbt it would make much difference
  185.         $self->{clock_search_y} = $self->{clock_height} / 3;
  186.         $self->{clock_search_x} = $self->{clock_width} / 2;
  187.  
  188.         # Make sure that we have a default sensitivity
  189.         $self->{struct}->{sensitivity} = 0.01 if (!exists $self->{struct}->{sensitivity});
  190.  
  191.         # Left or right orientated clock?
  192.         if ($self->{struct}->{clock}->{side} == 1) {
  193.                 $self->{clock_search_x} = $image_width - ($self->{clock_indent} + $self->{clock_search_x});
  194.         }
  195.         else {
  196.                 $self->{clock_search_x} += $self->{clock_indent};
  197.         }
  198.  
  199.         # Define sensible clock boundaries, notice the 5 px margin on the default def.
  200.         my $clock_before = int(
  201.                 exists $self->{struct}->{clock}->{before}
  202.                         ? $image_height * $self->{struct}->{clock}->{before}
  203.                         : $image_height - 5
  204.         );
  205.  
  206.         my $clock_after = int(
  207.                 exists $self->{struct}->{clock}->{after}
  208.                         ? $image_height * $self->{struct}->{clock}->{after}
  209.                         : 5
  210.         );
  211.  
  212.         # Search down the image looking for the approximate location of the clock
  213.         my $approx
  214.                 = $self->clockSearch( $clock_after, $clock_before, $self->{clock_search_y}, SEARCH_DOWN, BLACK_PIXEL );
  215.  
  216.         # Search back up looking for the first white bit
  217.         $self->{clock_top} = int($self->clockSearch( $clock_after, $approx, 1, SEARCH_UP, WHITE_PIXEL )) + 1;
  218.  
  219.         # Measure the size of the clock (We trust this more then the provided size)
  220.         $self->{clock_height}
  221.                 = int($self->clockSearch( $self->{clock_top}, $clock_before, 1, SEARCH_DOWN, WHITE_PIXEL)) - $self->{clock_top};
  222.  
  223.         # Measure the spacing between clocks (We don't trust the padding either)
  224.         my $clock_mark_bottom = $self->{clock_top} + $self->{clock_height};
  225.         $self->{clock_padding}
  226.                 = int($self->clockSearch( $clock_mark_bottom, $clock_before, 1, SEARCH_DOWN, BLACK_PIXEL)) - $clock_mark_bottom;
  227.  
  228.         # Search up from the bottom of the image looking for the approximate end of the clock
  229.         $approx = $self->clockSearch( $self->{clock_top}, $clock_before, $self->{clock_search_y}, SEARCH_UP, BLACK_PIXEL );
  230.  
  231.         # Search back down looking for the first black bit
  232.         $self->{clock_bottom} = int($self->clockSearch( $approx, $clock_before, 1, SEARCH_DOWN, WHITE_PIXEL ));
  233.  
  234.         # Set ourselves up with the default specs (The tatts cards require this as the identifier is part of the result set)
  235.         $self->{cell} = 'default';
  236.         $self->_defineCell();
  237.  
  238.         # Now that we know about the clock, lets identify the card.
  239.         $self->{card_id} = $self->getIdentifier();
  240.         if (exists($self->{struct}->{cell}->{$self->{card_id}})) {
  241.                 $self->{cell} = $self->{card_id};
  242.                 $self->_defineCell();
  243.         }
  244.  
  245.         return $self;
  246. }
  247.  
  248. sub _defineCell() {
  249.         my $self = shift;
  250.         $self->{cell_indent}   = int($self->{image_width} * $self->{struct}->{cell}->{$self->{cell}}->{indent});
  251.         # We need these not to be whole integers...
  252.         $self->{cell_padding}  = $self->{image_width} * $self->{struct}->{cell}->{$self->{cell}}->{padding};
  253.         $self->{cell_width}    = $self->{image_width} * $self->{struct}->{cell}->{$self->{cell}}->{width};
  254.         $self->{cell_count}    = int(($self->{image_width} - $self->{cell_indent}) / ($self->{cell_width} + $self->{cell_padding}));
  255. }
  256.  
  257. =head1 METHODS
  258.  
  259. getIdentifier
  260. detect
  261. getCellCoverage
  262. getPixelCoverage
  263. countClock
  264. getClock
  265. clockSearch
  266. getSinglePixel
  267.  
  268. =over 4
  269.  
  270. =item getIdentifier
  271.  
  272. Scans the gamecard to return the identifier.
  273.  
  274. If the identifier is defined in $struct then it'll use those parameters to try
  275. and find it. If not, then it guesses that the first line of results from the
  276. clock is what you want.
  277.  
  278. =cut
  279.  
  280. sub getIdentifier {
  281.         my $self = shift;
  282.  
  283.         if ( !( exists $self->{cache}->{getIdentifier} && $self->{caching} ) ) {
  284.                 if (exists($self->{struct}->{identifier})) {
  285.  
  286.                         # Shortcut, I'm lazy
  287.                         my $id_props = $self->{struct}->{identifier};
  288.  
  289.                         # Create logical upper and lower boundries, again notice the 5 px padding
  290.                         my $id_bottom = int(
  291.                                 exists($id_props->{before})
  292.                                         ? $self->{image_height} * $id_props->{before}
  293.                                         : $self->{image_height} - 5
  294.                         );
  295.  
  296.                         my $id_top = int(
  297.                                 exists($id_props->{after})
  298.                                         ? $self->{image_height} * $id_props->{after}
  299.                                         : 5
  300.                         );
  301.  
  302.                         my $line_max_size = 0;
  303.                         my @id_lines = ();
  304.  
  305.                         # The only type here at the moment
  306.                         if ($id_props->{type} == LINEBIT_IDENTIFIER) {
  307.                                 my $id_width  = int($self->{image_width} * $id_props->{width} + 0.5);
  308.                                 my $id_indent = int($self->{image_width} * $id_props->{indent} + 0.5);
  309.  
  310.                                 # Left or right side
  311.                                 my $id_left = int(
  312.                                         $id_props->{side}
  313.                                                 ? $self->{image_width} - ($id_width + $id_indent)
  314.                                                 : $id_indent
  315.                                 );
  316.  
  317.                                 # Find the top of the identifier
  318.                                 my $coverage = 0;
  319.                                 while ($coverage < $id_props->{sensitivity}) {
  320.                                         $coverage = $self->getPixelCoverage($id_left, ++$id_top, $id_width, ENABLE_DRAW);
  321.                                 }
  322.  
  323.                                 # Find the bottom of the identifier
  324.                                 $coverage = 0;
  325.                                 while ($coverage < $id_props->{sensitivity}) {
  326.                                         $coverage = $self->getPixelCoverage($id_left, --$id_bottom, $id_width, ENABLE_DRAW);
  327.                                 }
  328.  
  329.                                 my $cursor = $id_top;
  330.                                 # Assume we're starting in a 100% covered area
  331.                                 $coverage = 1;
  332.  
  333.                                 # Loop through the area defined by $id_top and $id_bottom
  334.                                 while ($cursor < $id_bottom) {
  335.  
  336.                                         # Work the blackspace
  337.                                         my $cur_start = $cursor;
  338.                                         while ($coverage >= $id_props->{sensitivity}) {
  339.                                                 $coverage = $self->getPixelCoverage($id_left, ++$cursor, $id_width, ENABLE_DRAW);
  340.                                         }
  341.  
  342.                                         if ($self->{draw}) {
  343.                                                 $self->{image}->Draw(
  344.                                                         primitive => 'line',
  345.                                                         stroke => 'red',
  346.                                                         points => ($id_left + $id_width) . ",$cur_start $id_left,$cursor"
  347.                                                 );
  348.                                         }
  349.  
  350.                                         my $height = $cursor - $cur_start;
  351.                                         $line_max_size = $height > $line_max_size ? $height : $line_max_size;
  352.                                         push @id_lines, [1,$height];
  353.  
  354.                                         # Work the whitespace
  355.                                         $cur_start = $cursor;
  356.                                         while ($coverage < $id_props->{sensitivity}) {
  357.                                                 last if ($cursor >= $id_bottom);
  358.                                                 $coverage = $self->getPixelCoverage($id_left, ++$cursor, $id_width, ENABLE_DRAW);
  359.                                         }
  360.  
  361.                                         if ($self->{draw}) {
  362.                                                 $self->{image}->Draw(
  363.                                                         primitive => 'line', stroke => 'blue', points => "$id_left,$cur_start $id_left,$cursor "
  364.                                                 );
  365.                                                 $self->{image}->Draw(
  366.                                                         primitive => 'line', stroke => 'blue', points => ($id_left + $id_width) . ",$cur_start " . ($id_left + $id_width) . ",$cursor"
  367.                                                 );
  368.                                         }
  369.                                         push @id_lines, [0,($cursor - $cur_start)];
  370.                                 }
  371.                                 pop(@id_lines);
  372.                         }
  373.  
  374.                         # Convert the array into an integer we can use
  375.                         # Not the best theory used here, but based on the fact
  376.                         # that I have not seen a card with two sequential blank
  377.                         # bits, I've just made the rather poor assumption that
  378.                         # there is none.
  379.                         # Should re-address this when I'm not so tired, I suspect
  380.                         # I can knock something up with ratios against the blank spots
  381.                         # to work out the maximum blank size
  382.                         my $identifier = 0;
  383.                         foreach (@id_lines) {
  384.                                 if (${$_}[0] == 1) {
  385.                                         $identifier = ($identifier << 1) + 1;
  386.                                 } elsif (${$_}[1] > $line_max_size * 2) {
  387.                                         $identifier <<= 1;
  388.                                 }
  389.                         }
  390.  
  391.                         $self->{cache}->{getIdentifier} = $identifier;
  392.                 }
  393.                 # Default action is to extract the identifier from the first clock (ala Tatts)
  394.                 else {
  395.                         my $identifier = 0;
  396.                         for my $cell (0..($self->{cell_count} - 1)) {
  397.                                 if ( $self->getCellCoverage( $cell, 0 ) >= $self->{struct}->{sensitivity} ) {
  398.                                         $identifier += 1 << $cell;
  399.                                 }
  400.                         }
  401.  
  402.                         $self->{cache}->{getIdentifier} = $identifier;
  403.                 }
  404.         }
  405.  
  406.         return $self->{cache}->{getIdentifier};
  407. }
  408.  
  409. =item detect
  410.  
  411. The brains and brawn of this module, this should be the only function you need
  412. to call. Returns a game hash (compatible with Pdi's with the exception of a
  413. new iGame element that has the identifier from the card)
  414.  
  415. =cut
  416.  
  417. sub detect {
  418.         my $self = shift;
  419.  
  420.         my $sensitivity = $self->{struct}->{sensitivity};
  421.         my @indices;
  422.         my $index_counter = 0;
  423.  
  424.         for my $row ( 0..($self->countClock() - 1) ) {
  425.                 my $line = '';
  426.                 for my $column ( 0..($self->{cell_count} - 1) ) {
  427.                         if ($self->getCellCoverage($column,$row) > $sensitivity) {
  428.                                 my %record = (
  429.                                         iOmrRow    => $row + 1,
  430.                                         iOmrColumn => $column + 1,
  431.                                         iIndex     => ++$index_counter,
  432.                                 );
  433.                                 push @indices, \%record;
  434.                                 if ($self->{draw}) {
  435.                                         print "1";
  436.                                 }
  437.                         }
  438.                         elsif ($self->{draw}) {
  439.                                 print "0";
  440.                         }
  441.                 }
  442.                 if ($self->{draw}) {
  443.                         print "\n";
  444.                 }
  445.         }
  446.         my $data = {
  447.                 aZones => [
  448.                         {
  449.                                 pIndices => $index_counter,
  450.                                 aIndices => \@indices,
  451.                         },
  452.                 ],
  453.                 pZones => 1,
  454.                 iGame => $self->getIdentifier(),
  455.         };
  456.  
  457.         return $data;
  458. }
  459.  
  460. =item getCellCoverage($position_x, $position_y)
  461.  
  462. Get the % of black in the cell at $position_x and $position_y.
  463.  
  464. Automatically calculates the geometry and location of the cell.
  465.  
  466. =cut
  467.  
  468. sub getCellCoverage {
  469.         my $self = shift;
  470.         my $position_x = shift;
  471.         my $position_y = shift;
  472.  
  473.         if ( !( exists $self->{cache}->{getCellCoverage}->{$position_x}->{$position_y} ) ) {
  474.                 my $cell_indent = $self->{cell_indent} + (($self->{cell_padding} + $self->{cell_width}) * $position_x);
  475.  
  476.                 # Right aligned cells...
  477.                 if ( $self->{struct}->{cell}->{$self->{cell}}->{side} == RIGHT_ALIGNED ) {
  478.                         $cell_indent = $self->{image_width} - ($cell_indent + $self->{cell_width});
  479.                 }
  480.  
  481.                 my $clock = $self->getClock($position_y);
  482.  
  483.                 # Cell after clock
  484.                 if ($self->{struct}->{clock}->{type} == AFTER_CLOCK) {
  485.                         # Some of the samples provided the cells actually overlap the clock a little, we compensate here for that
  486.                         my $cell_top = $clock->{top} + $self->{clock_height} / 3;
  487.                         $self->{cache}->{getCellCoverage}->{$position_x}->{$position_y}
  488.                                 = $self->getPixelCoverage($cell_indent, $cell_top, $self->{cell_width}, $self->{clock_padding}, ENABLE_DRAW);
  489.                 }
  490.                 # Cell on clock
  491.                 else {
  492.                         my $cell_top = $clock->{top};
  493.                         $self->{cache}->{getCellCoverage}->{$position_x}->{$position_y}
  494.                                 = $self->getPixelCoverage($cell_indent, $cell_top, $self->{cell_width}, $self->{clock_height}, ENABLE_DRAW);
  495.                 }
  496.         }
  497.  
  498.         return $self->{cache}->{getCellCoverage}->{$position_x}->{$position_y};
  499. }
  500.  
  501. =item getPixelCoverage($left, $top, $width, $height[, (DRAW)])
  502.  
  503. Get the % of black in a given area, if $draw is one it draws
  504. a red box around the area on the canvas.
  505.  
  506. =cut
  507.  
  508. sub getPixelCoverage {
  509.                 my $self   = shift;
  510.                 my $left   = shift;
  511.                 my $top    = shift;
  512.                 my $width  = shift;
  513.                 my $height = shift;
  514.                 my $draw   = shift;
  515.  
  516.                 my @pixels = $self->{image}->GetPixels(
  517.                         x => $left,
  518.                         y => $top,
  519.                         width => $width,
  520.                         height => $height,
  521.                         normalize => 'true',
  522.                 );
  523.  
  524.                 # Draw back to the canvas, this is useful for trying to define the slips
  525.                 if ($draw && $self->{draw}) {
  526.                         $self->{image}->Draw(
  527.                                 primitive => 'rectangle',
  528.                                 stroke => "red",
  529.                                 points => ($left-1).','.($top-1).' '.($left+$width+1).','.($top+$height+1),
  530.                         );
  531.                 }
  532.  
  533.                 # We want inverse values, normally white is 1 and black is 0.
  534.                 return 1 - sum( @pixels ) / 3  /  ( @pixels / 3);
  535. }
  536.  
  537. =item countClock
  538.  
  539. Counts the clock marks on the page
  540.  
  541. =cut
  542.  
  543. sub countClock {
  544.         my $self = shift;
  545.  
  546.         if ( !( exists $self->{cache}->{countClock} && $self->{caching} ) ) {
  547.  
  548.                 # Get the first clock mark
  549.                 my $clock = $self->getClock(0);
  550.                 my $count = 1;
  551.  
  552.                 while ($clock) {
  553.                         $clock = $self->getClock($count++);
  554.                 }
  555.  
  556.                 # We end up with one to many (c:
  557.                 $self->{cache}->{countClock} = $count - 1;
  558.         }
  559.  
  560.         return $self->{cache}->{countClock};
  561. }
  562.  
  563. =item getClock($index)
  564.  
  565. Get the co-ordinates of the clock at $index, recursive function
  566. could be very slow without caching turned on.
  567.  
  568. =cut
  569.  
  570. sub getClock {
  571.         my $self = shift;
  572.         my $index = shift;
  573.  
  574.         if ( !( exists $self->{cache}->{getClock}->{$index} && $self->{caching} ) ) {
  575.  
  576.                 my $clock_top = 0;
  577.                 if ($index == 0) {
  578.                         # We already know where clock 0 is.
  579.                         $self->{cache}->{getClock}->{$index} = {
  580.                                 top => $self->{clock_top},
  581.                                 bottom => $self->{clock_top} + $self->{clock_height},
  582.                         };
  583.                 }
  584.                 else {
  585.                         # Provided caching is on, this doesn't hurt us (c:
  586.                         my $clock = $self->getClock($index - 1);
  587.                         $clock_top = $clock->{bottom};
  588.  
  589.                         # Save some looking, guess approximately where the next clock is
  590.                         my $approx = $clock_top + $self->{clock_padding} + $self->{clock_search_y};
  591.  
  592.                         if ($approx < $self->{clock_bottom}) {
  593.  
  594.                                 my $next_clock_top = 0;
  595.                                 my $pixel = $self->getSinglePixel($self->{clock_search_x}, $approx);
  596.  
  597.                                 # If it's a white pixel we'll search down for a black one
  598.                                 if ($pixel == 1) {
  599.                                         $next_clock_top = $self->clockSearch($approx, $self->{clock_bottom}, 1, SEARCH_DOWN, BLACK_PIXEL) - 1;
  600.                                 }
  601.                                 # If ti's a white pixel we'll search up for a white one
  602.                                 else {
  603.                                         $next_clock_top = $self->clockSearch($clock_top, $approx, 1, SEARCH_UP, WHITE_PIXEL) + 1;
  604.                                 }
  605.  
  606.                                 $self->{cache}->{getClock}->{$index} = {
  607.                                         top => $next_clock_top,
  608.                                         bottom => $next_clock_top + $self->{clock_height},
  609.                                 };
  610.                         }
  611.                         else {
  612.                                 $self->{cache}->{getClock}->{$index} = undef;
  613.                         }
  614.                 }
  615.         }
  616.  
  617.         return $self->{cache}->{getClock}->{$index};
  618. }
  619.  
  620. =item clockSearch($start, $stop, $increment[, (SEARCH), (COLOR)])
  621.  
  622. Search the page between $start and $stop in $increment steps
  623.  
  624. =cut
  625.  
  626. sub clockSearch {
  627.         my $self = shift;
  628.         my $start = shift;
  629.         my $stop = shift;
  630.         my $increment = shift;
  631.         my $direction = shift || SEARCH_DOWN;
  632.         my $white = shift || BLACK_PIXEL;
  633.  
  634.         # Saves us an op or two later, we can use all the help we can get (c:
  635.         my $black = !$white;
  636.         if ($direction == SEARCH_DOWN) {
  637.                 $increment = -1 * $increment;
  638.         }
  639.  
  640.         my $search_y = $direction ? $stop : $start;
  641.  
  642.         while ($search_y >= $start && $search_y <= $stop) {
  643.                 $search_y += $increment;
  644.  
  645.                 my $pixel = $self->getSinglePixel($self->{clock_search_x}, $search_y);
  646.  
  647.                 if ($black && $pixel == BLACK_PIXEL) {
  648.                         if ($self->{draw}) {
  649.                                 $self->{image}->Set("pixel[".$self->{clock_search_x}.",$search_y" => '#FF0000');
  650.                         }
  651.                         return  $search_y;
  652.                 }
  653.                 elsif ($white && $pixel == WHITE_PIXEL) {
  654.                         if ($self->{draw}) {
  655.                                 $self->{image}->Set("pixel[".$self->{clock_search_x}.",$search_y" => '#00FF00');
  656.                         }
  657.                         return $search_y;
  658.                 }
  659.         }
  660. }
  661.  
  662. =item getSinglePixel($position_x, $position_y)
  663.  
  664. Shortcut function to get if a single pixel is black or white.
  665.  
  666. =cut
  667.  
  668. sub getSinglePixel {
  669.         my $self = shift;
  670.         my $x = shift;
  671.         my $y = shift;
  672.  
  673.         my @pixel = $self->{image}->GetPixels(
  674.                 x => $x,
  675.                 y => $y,
  676.                 width => 1,
  677.                 height => 1,
  678.                 normalize => 'true',
  679.         );
  680.  
  681.         # Will result in 1 for white, 0 for black (or any strage obscure result)
  682.         return ($pixel[0] & $pixel[1] & $pixel[2]);
  683. }
  684.  
  685. 1;
  686.  
  687. __END__
  688.  
  689. =back
  690.  
  691. =head1 AUTHOR
  692.  
  693. ...
  694.  
  695. =head1 COPYRIGHT
  696.  
  697. ...
  698.  
  699. =cut

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me
: