1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#!/usr/bin/lua
-- ExactImage Lua Example
-- Copyright (C) 2008 - 2010 Rene Rebe, ExactCODE GmbH
package.cpath = "./objdir/api/lua/?.so;" .. package.cpath
require "ExactImage"
-- create an ExactImage
image = ExactImage.newImage ()
-- easy use, use on-disc files:
if ExactImage.decodeImageFile (image, "testsuite/281-4.2.04.tif")
then
print "image decoded all fine."
else
print "something went wrong ..."
os.exit(1)
end
if ExactImage.encodeImageFile (image, "test.jpg", 80, "")
then
print "image written all fine."
else
print "something went wrong writing the image ..."
os.exit(1)
end
-- advanced use, use in memory locations
f = io.open ("testsuite/281-4.2.04.tif")
image_bits = f:read("*all")
if ExactImage.decodeImage (image, image_bits)
then
print "image read from RAM."
else
print "something went wrong decoding the RAM ..."
os.exit(1)
end
f:close()
-- image properties
print ("Width: " .. ExactImage.imageWidth (image))
print ("Height: " .. ExactImage.imageHeight (image))
print ("Xres: " .. ExactImage.imageXres (image))
print ("Yres: " .. ExactImage.imageYres (image))
print ("Channels: " .. ExactImage.imageChannels (image))
print ("Channel depth: " .. ExactImage.imageChannelDepth (image))
-- setable as well
ExactImage.imageSetXres (image, 144);
ExactImage.imageSetYres (image, 144);
print ("Xres: " .. ExactImage.imageXres (image))
print ("Yres: " .. ExactImage.imageYres (image))
-- image data manipulation
--[[
ExactImage.imageRotate (image, 90)
ExactImage.imageScale (image, 4)
ExactImage.imageBoxScale (image, .5) ]]--
for y = 0, ExactImage.imageHeight(image) - 1 do
for x = 0, ExactImage.imageWidth(image) - 1 do
local r, g, b, a = ExactImage.get(image, x, y)
ExactImage.set(image, x, y, 1-r, 1-g, 1-b, 1-a) -- x / 256, y / 256, x*y / 256, 1)
end
end
image_bits = ExactImage.encodeImage (image, "jpeg", 80, "")
print ("size: " .. string.len(image_bits))
if string.len(image_bits) > 0
then
print "image encoded all fine."
else
print "something went wrong encoding the image into RAM ..."
os.exit(1)
end
-- write the file to disc natively
f = io.open ("lua.jpg", "w");
f:write (image_bits)
f:close ()
-- complex all-in-one function
if ExactImage.decodeImageFile (image, "testsuite/281-4.2.04.tif")
then
image_copy = ExactImage.copyImage (image);
ExactImage.imageOptimize2BW (image, 0, 0, 170, 3, 2.1);
ExactImage.encodeImageFile (image, "optimize.tif", 0, "");
is_empty = ExactImage.imageIsEmpty (image, 0.02, 16);
if is_empty then
print "Image is empty"
else
print "Image is not empty, too many pixels ...\n";
end
-- the image is bw, now - but we still have a copy
ExactImage.encodeImageFile (image_copy, "copy.tif", 0, "");
-- and do not forget the free the copy, otherwise it is leaked
ExactImage.deleteImage (image_copy);
else
printf "Error loading testsuite/deskew/01.tif"
end
if ExactImage.decodeImageFile (image, "testsuite/empty-page/empty.tif")
then
is_empty = ExactImage.imageIsEmpty (image, 0.02, 16);
if is_empty then
print "Image is empty";
else
print "Image is not empty, too many pixels ...\n";
end
else
print "Error loading testsuite/empty-page/empty.tif"
end
-- barcode decoding
--while (<Scan-001-4.tif>)
f = "testsuite/barcodes/Scan-001-4.tif"
print ("looking for barcodes in " .. f)
if ExactImage.decodeImageFile (image, f) then
barcodes =
ExactImage.imageDecodeBarcodes (image,
"code39|CODE128|CODE25|EAN13|EAN8|UPCA|UPCE",
3, -- min length
10) -- max length
print ("type: " .. type(barcodes))
--for (my $i;$i< scalar(@$barcodes);$i+=2) {
-- print "@$barcodes[$i] @$barcodes[$i+1]\n";
--}
else
print ("Error loading " .. f)
end
--end
-- we do not want to leak memory, always delete the image
-- when you are done with it!
ExactImage.deleteImage (image);
print "ok, here the example ends (for now) ..."
|