pax_global_header00006660000000000000000000000064147700466400014522gustar00rootroot0000000000000052 comment=9d9b2253e1302f9487708d53eaeeaf0e2db037ab ssocr-2.25.0/000077500000000000000000000000001477004664000127415ustar00rootroot00000000000000ssocr-2.25.0/.gitignore000066400000000000000000000002171477004664000147310ustar00rootroot00000000000000/*.swp /*.o /ssocr /testbild.png /ssocr.1 /ssocr-?.?.? /ssocr-?.??.? /ssocr-?.?.?+ /ssocr-?.??.?+ /*.tar.bz2 /*.deb /*.tmp /ssocr-manpage.html ssocr-2.25.0/AUTHORS000066400000000000000000000001231477004664000140050ustar00rootroot00000000000000Erik Auerswald Cristiano Fontana ssocr-2.25.0/COPYING000066400000000000000000001045131477004664000140000ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . ssocr-2.25.0/INSTALL000066400000000000000000000101171477004664000137720ustar00rootroot00000000000000Building and Installing ----------------------- To build ssocr, type: make You can use ssocr from the directory you built it, no installation required. If you want to install it to your system, type: make install To change the default install location of /usr/local to /usr, type: make PREFIX=/usr install You have to keep track of the installed files yourself, if you want to remove them later on. If you prefer to install a .deb package for easier removal, you can build a .deb package by typing: make selfdeb Prerequisites ------------- - A C compiler compatible with GCC. - A system sufficiently compatible to POSIX and GNU. - POSIX utilities: chmod, head, printf, sh, sed (or compatible) - GNU utilities: date, gzip, install, make, tar (or compatible) - Other utilities: bzip2 - Imlib2 shared library and development headers. On a Debian or Ubuntu system they are provided by the packages libimlib2 and libimlib2-dev. On Mac OS X you can use 'brew' to install Imlib2: brew install imlib2 (see https://brew.sh/) - pkg-config or imlib2-config (part of libimlib2-dev on Debian or Ubuntu) [Imlib2 version 1.7.5 dropped the imlib2-config program in favor of using pkg-config] - Imlib2 requires the X11/Xlib.h header and links against some X11 libraries, at least on GNU/Linux with X11 GUI, thus X11 shared libraries and development headers are required for ssocr, too, although ssocr does not use X11 itself - Build tools, e.g., build-essential on a Debian (or Ubuntu) system, usually contain both make and a C compiler. - To build a .deb package, you probably need the debhelper package. - To create an HTML version of the man page, you need a man utility that can produce HTML output. Additional Makefile Targets --------------------------- - clean: remove generated files except packages - distclean: remove all generated files - tar: create a bzip2 compressed tar-ball of the sources for distribution - ssocr-manpage.html: create HTML version of man page - selfdeb: create a package file in .deb format that can be installed on Debian-like distributions Platform Specifics ------------------ - I am testing on GNU/Linux only - I am testing with GCC only - On Windows, you may need to rename the binary from ssocr to ssocr.exe - On macOS, you may need to specify the X11 include path manually. The following have been reported as working on macOS at some time: - make CPPFLAGS=-I/opt/X11/include - make CPPFLAGS=-I/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers/ C Compiler Problems: -------------------- Some users have reported problems with their C compilers. If you are not using a stable release version of GCC, you may need to disable security features supposed to be provided by the C compiler, but broken in your specific compiler version. To do this, remove the following from the CFLAGS definition in the Makefile: -D_FORTIFY_SOURCE=2 -fstack-protector-all If you need to do this to compile ssocr, please consider reporting the issue to your source for your C compiler (e.g., vendor or distribution). Another problem can be the introduction of new compiler warnings. Some of those are prone to false positives, and this problem does occur for stable GCC releases as well. Because of this I have removed -Werror and -pedantic-errors from ssocr's CFLAGS. If you suspect that your C compiler has problems with the ssocr source code, you can use the minimal CFLAGS definition given below: CFLAGS := $(shell imlib2-config --cflags) You can do this by commenting out the default CFLAGS definition and removing the comment sign in front of the minimal CFLAGS definition in the Makefile. Website ------- You can get the current ssocr version from the official ssocr website: https://www.unix-ag.uni-kl.de/~auerswal/ssocr/ Third Party Packages -------------------- There exist third party packaging efforts for, e.g., GNU/Linux distributions and FreeBSD. Thus you can search the packaging system of your distribution for an ssocr package. ssocr-2.25.0/Makefile000066400000000000000000000051771477004664000144130ustar00rootroot00000000000000# minimal CFLAGS definition (try if compilation fails with default CFLAGS) #CFLAGS := $(shell if command -v imlib2-config >/dev/null; then imlib2-config --cflags; else pkg-config --cflags imlib2; fi) # default CFLAGS definition CFLAGS := -D_FORTIFY_SOURCE=2 -Wall -W -Wextra -pedantic -fstack-protector-all $(shell if command -v imlib2-config >/dev/null; then imlib2-config --cflags; else pkg-config --cflags imlib2; fi) -O3 LDLIBS := -lm $(shell if command -v imlib2-config >/dev/null; then imlib2-config --libs; else pkg-config --libs imlib2; fi) PREFIX := /usr/local BINDIR := $(PREFIX)/bin MANDIR := $(PREFIX)/share/man/man1 DOCDIR := $(PREFIX)/share/doc/ssocr DOCS := AUTHORS COPYING INSTALL README THANKS NEWS VERSION := $(shell sed -n 's/^.*VERSION.*"\(.*\)".*/\1/p' defines.h) CRYEARS := $(shell sed -n 's/^.*fprintf.*Copyright.*\(2004-2[0-9][0-9][0-9]\).*Erik.*Auerswald.*$$/\1/p' help.c) RELDATE := $(shell sed -n 's/^Version [.0-9]* .\([-0-9]*\).*$$/\1/p' NEWS | head -n1) all: ssocr ssocr.1 ssocr: ssocr.o imgproc.o help.o charset.o ssocr.o: ssocr.c ssocr.h defines.h imgproc.h help.h charset.h Makefile imgproc.o: imgproc.c defines.h imgproc.h help.h Makefile help.o: help.c defines.h imgproc.h help.h Makefile charset.o: charset.c charset.h defines.h help.h Makefile ssocr.1: ssocr.1.in Makefile defines.h help.c NEWS sed -e 's/@VERSION@/$(VERSION)/' \ -e 's/@DATE@/$(RELDATE)/' \ -e 's/@CRYEARS@/$(CRYEARS)/' <$< >$@ ssocr-manpage.html: ssocr.1 man -l -Thtml $< >$@ install: all install -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(MANDIR) $(DESTDIR)$(DOCDIR) install -s -m 0755 ssocr $(DESTDIR)$(BINDIR)/ssocr install -m 0644 ssocr.1 $(DESTDIR)$(MANDIR)/ssocr.1 gzip -9 $(DESTDIR)$(MANDIR)/ssocr.1 install -m 0644 $(DOCS) $(DESTDIR)$(DOCDIR) ssocr-dir: install -d ssocr-$(VERSION) install -m 0644 Makefile $(DOCS) *.[ch] *.in ssocr-$(VERSION) install -d ssocr-$(VERSION)/notdebian install -m 0644 notdebian/* ssocr-$(VERSION)/notdebian chmod +x ssocr-$(VERSION)/notdebian/rules notdebian/changelog: printf 'ssocr ($(VERSION)-1) unstable; urgency=low\n\n * self built package of current ssocr version in .deb format\n\n -- $(USER) $(shell date -R)\n' >$@ selfdeb: notdebian/changelog notdebian/control notdebian/rules ssocr-dir (cd ssocr-$(VERSION); ln -sv notdebian debian; fakeroot debian/rules binary; fakeroot debian/rules clean; rm -f debian) tar: ssocr-dir tar cvfj ssocr-$(VERSION).tar.bz2 ssocr-$(VERSION) clean: $(RM) ssocr ssocr.1 *.o *~ testbild.png ssocr-manpage.html $(RM) notdebian/changelog $(RM) -r ssocr-$(VERSION) ssocr-?.?.? ssocr-?.??.? distclean: clean $(RM) *.deb *.bz2 .PHONY: clean tar ssocr-dir install ssocr-2.25.0/NEWS000066400000000000000000000100661477004664000134430ustar00rootroot00000000000000Noteworthy Changes in ssocr Releases ==================================== Version 2.25.0 (2025-03-23): ---------------------------- * New option -F, --adapt-after-crop to skip threshold adjustment directly before cropping the image * Print warning when the two options -a, --absolute-threshold and -T, --iter-threshold are used together because -a inhibits -T * Improved performance when reading image data via standard input * Improved performance when using gray_stretch together with -g * Improved performance when using option -p, --process-only together with only the grayscale and/or mirror commands * Documentation improvements Version 2.24.1 (2024-12-11): ---------------------------- * Print warning when an unknown charset name is ignored * Print warning when an unknown luminance formula name is ignored * Fix one debug message to be printed only with -P, --debug-output * Documentation improvements Version 2.24.0 (2024-06-22): ---------------------------- * Fix decimal separator recognition when widest digit is a one * Add recognition of lower case variant of character 'h' to full charset * Add recognition of lower case variant of character 'r' to full charset * Documentation improvements * Error, warning, and debug message improvements Version 2.23.1 (2023-05-18): ---------------------------- * The man page uses the latest ssocr release date as its date (to help in creating reproducible builds) * Documentation improvements Version 2.23.0 (2023-05-01): ---------------------------- * New option -N, --min-segment to specify the minimum width and height of a segment, for both scanline based and ratio based character recognition * New option -M, --min-char-dims to specify minimum character dimensions * The option -d, --number-digits now also accepts a range description Version 2.22.2 (2023-04-24): ---------------------------- * Documentation improvements Version 2.22.1 (2022-01-25): ---------------------------- * Fix build failure with Imlib 1.7.5 (pkg-config replaces imlib2-config) Version 2.22.0 (2021-11-07): ---------------------------- * Fix compilation with GCC 10 * Add recognition of characters 'j' and 'y' to full charset * Add this "NEWS" file * Add character set "tt_robot" Version 2.21.0 (2021-04-25): ---------------------------- * Add ability to print spaces between digits that are positioned further away Version 2.20.0 (2021-04-19): ---------------------------- * Add options to adjust the ratios used for decimal separator recognition Version 2.19.0 (2019-08-05): ---------------------------- * Add option to select the set of recognized characters * Add option to omit decimal points from the output Version 2.18.0 (2017-11-19): ---------------------------- * Add convenience parameter N to the dilation and erosion command Version 2.17.0 (2017-10-22): ---------------------------- * Add option to print detected segments as ASCII art w/o further debug output * Add option to change the output to a hex string representing set segments * Add 7 characters shown on a display used in a Chinese table tennis robot Version 2.16.0 (2013-12-29): ---------------------------- * Add command to mirror the image horizontally or vertically Version 2.15.0 (2013-12-29): ---------------------------- * Add detection of minus signs, thanks to a patch by Cristiano Fontana Version 2.14.0 (2013-08-04): ---------------------------- * Add alternative version of the digit 9 w/o lower horizontal segment Version 2.13.0 (2012-11-04): ---------------------------- * Add ability to automatically determine the number of digits Version 2.12.0 (2012-11-03): ---------------------------- * Add recognition of hexadecimal digits Version 2.11.0 (2012-11-03): ---------------------------- * Add detection of a decimal point Version 2.9.0 (2009-08-07): --------------------------- * Allow reading image data from a pipe (standard input) Versions 2.x.y: --------------- * ssocrpp is now part of ssocr Versions 1.x.y: --------------- * ssocr w/o image manipulation functions * ssocrpp for image manipulation as preprocessing step before recognition ssocr-2.25.0/README000066400000000000000000000040501477004664000136200ustar00rootroot00000000000000Seven Segment Optical Character Recognition or ssocr for short is a program to recognize digits of a seven segment display. An image of one row of digits is used for input and the recognized number is written to the standard output. The program runs on GNU/Linux (some GNU/Linux distributions provide an ssocr package), FreeBSD (available as a port as well), Mac OS X (Homebrew can be used to install the library Imlib2, used by ssocr), and even on Windows (using Cygwin). ssocr should work on any UNIX-like or POSIX compatible operating system. Unless ssocr is installed via some packaging system, e.g., from a GNU/Linux distribution, it is distributed in source form and needs to be built before it can be used. See the INSTALL file for instructions on how to build ssocr. A manual for ssocr is available in the form of a man page named ssocr.1, you can read it using "make ssocr.1 && man ./ssocr.1" (without the quotes). You can get the current ssocr version from the official ssocr website: https://www.unix-ag.uni-kl.de/~auerswal/ssocr/ (Links to ssocr should point to the official website, not to a convenience copy of the development repository, e.g., on GitHub.) I am usually quicker to reply to emails than to GitHub issues. But increasingly Google blocks emails sent by me, so if you do not receive an answer from me, consider opening a GitHub issue. Perhaps your email provider does not allow you to read my solutions to your ssocr problems. Especially if you are using an @gmail.com or @google.com address, or use your own domain with Google email services, and you do not receive an answer to an email, it is most likely that Google blocked that answer. In that case, you can either follow up with a GitHub issue or use a better email provider than Google. The only problematic email provider I know of is Google. Every file in this repository or archive is licensed under the GNU General Public License version 3 (or later), unless another license is explicitly given in the file itself. This includes all documentation files and the Makefile, and all other files. ssocr-2.25.0/THANKS000066400000000000000000000006761477004664000136650ustar00rootroot00000000000000Alan Bates Russell Currie Bruce Dudek Tim Fardell Cristiano Fontana Emanuel Haupt Francesco Iovine Matthias Axel Kröll Jörg Mayer Steve Meyer Alex Myczko Carlos A. Neves Robert Sund ssocr-2.25.0/charset.c000066400000000000000000000145131477004664000145420ustar00rootroot00000000000000/* Seven Segment Optical Character Recognition Character Set Functions */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* Copyright (C) 2018-2025 Erik Auerswald */ /* standard things */ #include /* puts, printf, BUFSIZ, perror, FILE */ #include /* exit */ /* string manipulation */ #include /* strncasecmp */ /* my headers */ #include "defines.h" /* defines */ #include "help.h" /* character set keyword functions */ /* parse KEYWORD from --charset option */ charset_t parse_charset(char *keyword) { if(strncasecmp(keyword, "help", 4) == 0) { print_cs_help(); exit(42); } else if(strncasecmp(keyword, "full", 4) == 0) { return CS_FULL; } else if(strncasecmp(keyword, "digits", 6) == 0) { return CS_DIGITS; } else if(strncasecmp(keyword, "decimal", 7) == 0) { return CS_DECIMAL; } else if(strncasecmp(keyword, "hex", 3) == 0) { return CS_HEXADECIMAL; } else if(strncasecmp(keyword, "tt_robot", 8) == 0) { return CS_TT_ROBOT; } else { return CS_PARSE_ERROR; } } /* array for character set */ static char charset_array[CHARSET_MAX + 1]; /* initialize the character set array with the given character set */ void init_charset(charset_t cs) { int i; for(i = 0; i < CHARSET_MAX + 1; i++) { charset_array[i] = '_'; } switch(cs) { case CS_FULL: charset_array[D_ZERO] = '0'; charset_array[D_ONE] = '1'; charset_array[D_TWO] = '2'; charset_array[D_THREE] = '3'; charset_array[D_FOUR] = '4'; charset_array[D_FIVE] = '5'; charset_array[D_SIX] = '6'; charset_array[D_SEVEN] = '7'; charset_array[D_ALTSEVEN] = '7'; charset_array[D_EIGHT] = '8'; charset_array[D_NINE] = '9'; charset_array[D_ALTNINE] = '9'; charset_array[D_DECIMAL] = '.'; charset_array[D_MINUS] = '-'; charset_array[D_HEX_A] = 'a'; charset_array[D_HEX_b] = 'b'; charset_array[D_HEX_C] = 'c'; charset_array[D_HEX_c] = 'c'; charset_array[D_HEX_d] = 'd'; charset_array[D_HEX_E] = 'e'; charset_array[D_HEX_F] = 'f'; charset_array[D_U] = 'u'; charset_array[D_T] = 't'; charset_array[D_L] = 'l'; charset_array[D_H] = 'h'; charset_array[D_h] = 'h'; charset_array[D_R] = 'r'; charset_array[D_ALT_R] = 'r'; charset_array[D_r] = 'r'; charset_array[D_P] = 'p'; charset_array[D_N] = 'n'; charset_array[D_n] = 'n'; charset_array[D_Y] = 'y'; charset_array[D_J] = 'j'; break; case CS_DIGITS: charset_array[D_ZERO] = '0'; charset_array[D_ONE] = '1'; charset_array[D_TWO] = '2'; charset_array[D_THREE] = '3'; charset_array[D_FOUR] = '4'; charset_array[D_FIVE] = '5'; charset_array[D_SIX] = '6'; charset_array[D_HEX_b] = '6'; charset_array[D_SEVEN] = '7'; charset_array[D_ALTSEVEN] = '7'; charset_array[D_EIGHT] = '8'; charset_array[D_NINE] = '9'; charset_array[D_ALTNINE] = '9'; break; case CS_DECIMAL: charset_array[D_ZERO] = '0'; charset_array[D_ONE] = '1'; charset_array[D_TWO] = '2'; charset_array[D_THREE] = '3'; charset_array[D_FOUR] = '4'; charset_array[D_FIVE] = '5'; charset_array[D_SIX] = '6'; charset_array[D_HEX_b] = '6'; charset_array[D_SEVEN] = '7'; charset_array[D_ALTSEVEN] = '7'; charset_array[D_EIGHT] = '8'; charset_array[D_NINE] = '9'; charset_array[D_ALTNINE] = '9'; charset_array[D_DECIMAL] = '.'; charset_array[D_MINUS] = '-'; break; case CS_HEXADECIMAL: charset_array[D_ZERO] = '0'; charset_array[D_ONE] = '1'; charset_array[D_TWO] = '2'; charset_array[D_THREE] = '3'; charset_array[D_FOUR] = '4'; charset_array[D_FIVE] = '5'; charset_array[D_SIX] = '6'; charset_array[D_SEVEN] = '7'; charset_array[D_ALTSEVEN] = '7'; charset_array[D_EIGHT] = '8'; charset_array[D_NINE] = '9'; charset_array[D_ALTNINE] = '9'; charset_array[D_DECIMAL] = '.'; charset_array[D_MINUS] = '-'; charset_array[D_HEX_A] = 'a'; charset_array[D_HEX_b] = 'b'; charset_array[D_HEX_C] = 'c'; charset_array[D_HEX_c] = 'c'; charset_array[D_HEX_d] = 'd'; charset_array[D_HEX_E] = 'e'; charset_array[D_HEX_F] = 'f'; break; case CS_TT_ROBOT: charset_array[D_ZERO] = '0'; charset_array[D_ONE] = '1'; charset_array[D_TWO] = '2'; charset_array[D_THREE] = '3'; charset_array[D_FOUR] = '4'; charset_array[D_FIVE] = '5'; charset_array[D_SIX] = '6'; charset_array[D_SEVEN] = '7'; charset_array[D_TT_WRONG_SEVEN_1] = '7'; charset_array[D_TT_WRONG_SEVEN_2] = '7'; charset_array[D_EIGHT] = '8'; charset_array[D_NINE] = '9'; charset_array[D_MINUS] = '-'; charset_array[D_HEX_A] = 'a'; charset_array[D_HEX_b] = 'b'; charset_array[D_HEX_C] = 'c'; charset_array[D_HEX_d] = 'd'; charset_array[D_U] = 'v'; charset_array[D_T] = 't'; charset_array[D_L] = 'l'; charset_array[D_H] = 'h'; charset_array[D_R] = 'r'; charset_array[D_P] = 'p'; charset_array[D_N] = 'n'; break; default: fprintf(stderr, "%s: error: charset %s is not implemented\n", PROG, cs_key(cs)); exit(99); break; } } /* print a digit according to charset, return 1 if unknown, else 0 */ int print_digit(int digit, unsigned int flags) { int unknown_digit = 0; char c = '_'; if(digit <= CHARSET_MAX) { c = charset_array[digit]; } if(c == '_') { unknown_digit = 1; } if(!((c == '.') && (flags & OMIT_DECIMAL))) { putchar(c); } return unknown_digit; } ssocr-2.25.0/charset.h000066400000000000000000000022761477004664000145520ustar00rootroot00000000000000/* Seven Segment Optical Character Recognition Character Set Functions */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* Copyright (C) 2018-2025 Erik Auerswald */ #ifndef SSOCR2_CHARSET_H #define SSOCR2_CHARSET_H /* functions */ /* parse KEYWORD from --charset option */ charset_t parse_charset(char *keyword); /* initialize the character set array with the given character set */ void init_charset(charset_t cs); /* print a digit according to charset, return 1 if unknown, else 0 */ int print_digit(int digit, unsigned int flags); #endif /* SSOCR2_CHARSET_H */ ssocr-2.25.0/defines.h000066400000000000000000000151711477004664000145340ustar00rootroot00000000000000/* Seven Segment Optical Character Recognition Constant Definitions */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* Copyright (C) 2004-2025 Erik Auerswald */ /* Copyright (C) 2013 Cristiano Fontana */ #define PROG "ssocr" #ifndef SSOCR2_DEFINES_H #define SSOCR2_DEFINES_H /* version number */ #define VERSION "2.25.0" /* states */ #define FIND_DARK 0 #define FIND_LIGHT 1 /* boarder between dark and light */ #define THRESHOLD 50.0 #define DARK 0 #define LIGHT 1 #define UNKNOWN 2 /* segments * * 1 - * 2 3 | | * 4 - * 5 6 | | * 7 - * * */ #define HORIZ_UP 1 #define VERT_LEFT_UP 2 #define VERT_RIGHT_UP 4 #define HORIZ_MID 8 #define VERT_LEFT_DOWN 16 #define VERT_RIGHT_DOWN 32 #define HORIZ_DOWN 64 #define ALL_SEGS 127 #define DECIMAL 128 #define MINUS HORIZ_MID /* maximum number used for a character #define */ #define CHARSET_MAX 128 /* digits */ #define D_ZERO (ALL_SEGS & ~HORIZ_MID) #define D_ONE (VERT_RIGHT_UP | VERT_RIGHT_DOWN) #define D_TWO (ALL_SEGS & ~(VERT_LEFT_UP | VERT_RIGHT_DOWN)) #define D_THREE (ALL_SEGS & ~(VERT_LEFT_UP | VERT_LEFT_DOWN)) #define D_FOUR (ALL_SEGS & ~(HORIZ_UP | VERT_LEFT_DOWN | HORIZ_DOWN)) #define D_FIVE (ALL_SEGS & ~(VERT_RIGHT_UP | VERT_LEFT_DOWN)) #define D_SIX (ALL_SEGS & ~VERT_RIGHT_UP) #define D_SEVEN (HORIZ_UP | VERT_RIGHT_UP | VERT_RIGHT_DOWN) #define D_ALTSEVEN (VERT_LEFT_UP | D_SEVEN) #define D_EIGHT ALL_SEGS #define D_NINE (ALL_SEGS & ~VERT_LEFT_DOWN) #define D_ALTNINE (ALL_SEGS & ~(VERT_LEFT_DOWN | HORIZ_DOWN)) #define D_DECIMAL DECIMAL #define D_MINUS MINUS #define D_HEX_A (ALL_SEGS & ~HORIZ_DOWN) #define D_HEX_b (ALL_SEGS & ~(HORIZ_UP | VERT_RIGHT_UP)) #define D_HEX_C (ALL_SEGS & ~(VERT_RIGHT_UP | HORIZ_MID | VERT_RIGHT_DOWN)) /* a C in the lower half can only happen when digit boundary detection fails */ #define D_HEX_c (HORIZ_MID | VERT_LEFT_DOWN | HORIZ_DOWN) #define D_HEX_d (ALL_SEGS & ~(HORIZ_UP | VERT_LEFT_UP)) #define D_HEX_E (ALL_SEGS & ~(VERT_RIGHT_UP | VERT_RIGHT_DOWN)) #define D_HEX_F (ALL_SEGS & ~(VERT_RIGHT_UP | VERT_RIGHT_DOWN | HORIZ_DOWN)) #define D_U (D_ZERO & ~HORIZ_UP) #define D_T (ALL_SEGS & ~(HORIZ_UP | VERT_RIGHT_UP | VERT_RIGHT_DOWN)) #define D_L (D_T & ~HORIZ_MID) #define D_H (ALL_SEGS & ~(HORIZ_UP | HORIZ_DOWN)) #define D_h (VERT_LEFT_UP | VERT_LEFT_DOWN | HORIZ_MID | VERT_RIGHT_DOWN) #define D_R (D_ZERO & ~(VERT_RIGHT_DOWN | HORIZ_DOWN)) #define D_ALT_R (VERT_LEFT_UP | VERT_LEFT_DOWN | HORIZ_UP) /* an r in the lower half can only happen when digit boundary detection fails */ #define D_r (VERT_LEFT_DOWN | HORIZ_MID) #define D_P (D_HEX_F | VERT_RIGHT_UP) #define D_N (D_ZERO & ~HORIZ_DOWN) /* an N in the lower half can only happen when digit boundary detection fails */ #define D_n (VERT_LEFT_DOWN | VERT_RIGHT_DOWN | HORIZ_MID) #define D_Y (ALL_SEGS & ~(HORIZ_UP | VERT_LEFT_DOWN)) #define D_J (HORIZ_DOWN | VERT_RIGHT_UP | VERT_RIGHT_DOWN) /* add two "wrong" 7 definitions used in a character set for a Chinese * table tennis robot */ #define D_TT_WRONG_SEVEN_1 (D_SEVEN | VERT_LEFT_DOWN | HORIZ_DOWN) #define D_TT_WRONG_SEVEN_2 (D_SEVEN | HORIZ_DOWN) #define D_UNKNOWN 0 #define NUMBER_OF_DIGITS 6 /* in this special case */ /* a decimal point (or thousands separator) is recognized by height less * than / DEC_H_RATIO and width less than * / DEC_W_RATIO */ #define DEC_H_RATIO 5 #define DEC_W_RATIO 2 /* needs to work with just ones in the display, too */ /* a one is recognized by a height/width ratio > ONE_RATIO (as ints) */ #define ONE_RATIO 3 /* a minus sign is recognized by a width/height ratio > MINUS_RATIO (as ints) */ #define MINUS_RATIO 2 /* add space characters if digit distance is greater than SPC_FAC * min dist */ #define SPC_FAC 1.4 /* number of set pixels needed in a scanline to recognize a segment */ #define NEED_PIXELS 1 /* minimum number of pixels required for a segment * this generalizes to both scanline and ratio based digit detection, * but only when the digit is built using segments, i.e., this is not * used for decimal separators */ #define MIN_SEGMENT 1 /* minimum width of a character respectively digit */ #define MIN_CHAR_W 1 /* minimum height of a character respectively digit */ #define MIN_CHAR_H 1 /* ignore # of pixels when checking a column fo black or white */ #define IGNORE_PIXELS 0 #define DIR_SEP "/" #define TMP_FILE_DIR "/tmp" #define TMP_FILE_PATTERN "ssocr.img.XXXXXX" #define DEBUG_IMAGE_NAME "testbild.png" /* flags set by options */ #define ABSOLUTE_THRESHOLD 1 #define DO_ITERATIVE_THRESHOLD (1<<1) #define VERBOSE (1<<2) #define USE_DEBUG_IMAGE (1<<3) #define PROCESS_ONLY (1<<4) #define PRINT_INFO (1<<5) #define ADJUST_GRAY (1<<6) #define DEBUG_OUTPUT (1<<7) #define ASCII_ART_SEGMENTS (1<<8) #define PRINT_AS_HEX (1<<9) #define OMIT_DECIMAL (1<<10) #define PRINT_SPACES (1<<11) #define SPC_USE_AVG_DST (1<<12) #define ADAPT_AFTER_CROP (1<<13) /* colors used by ssocr */ #define SSOCR_BLACK 0 #define SSOCR_WHITE 255 #define SSOCR_DEFAULT_FOREGROUND SSOCR_BLACK #define SSOCR_DEFAULT_BACKGROUND SSOCR_WHITE /* maximum RGB component value */ #define MAXRGB 255 /* doubles are assumed equal when they differ less than EPSILON */ #define EPSILON 0.0000001 /* default luminance formula */ #define DEFAULT_LUM_FORMULA REC709 /* when to adapt threshold values to the image */ #define INITIAL 0 /* adapt threshold unless is was already adapted */ #define UPDATE 1 /* adapt threshold even if it was adapted before */ /* foreground and background */ typedef enum fg_bg_e { FG, BG } fg_bg_t; /* luminance types resp. formulas */ typedef enum luminance_e { REC601, REC709, LINEAR, MINIMUM, MAXIMUM, RED, GREEN, BLUE, LUM_PARSE_ERROR } luminance_t; /* direction, to mirror horizontally or vertically, or for a scanline */ typedef enum direction_e { HORIZONTAL, VERTICAL } direction_t; /* character sets to choose from */ typedef enum charset_e { CS_FULL, CS_DIGITS, CS_DECIMAL, CS_HEXADECIMAL, CS_TT_ROBOT, CS_PARSE_ERROR } charset_t; #define DEFAULT_CHARSET CS_FULL #endif /* SSOCR2_DEFINES_H */ ssocr-2.25.0/help.c000066400000000000000000000314151477004664000140410ustar00rootroot00000000000000/* Seven Segment Optical Character Recognition Help Functions */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* Copyright (C) 2004-2025 Erik Auerswald */ /* Copyright (C) 2013 Cristiano Fontana */ /* standard things */ #include /* puts, printf, BUFSIZ, perror, FILE */ /* my headers */ #include "defines.h" /* defines */ /* global variables */ extern int ssocr_foreground; extern int ssocr_background; /* functions */ /* print luminance keyword */ void print_lum_key(luminance_t lt, FILE *f) { switch(lt) { case REC601: fprintf(f, "Rec601"); break; case REC709: fprintf(f, "Rec709"); break; case LINEAR: fprintf(f, "linear"); break; case MINIMUM: fprintf(f, "minimum"); break; case MAXIMUM: fprintf(f, "maximum"); break; case RED: fprintf(f, "red"); break; case GREEN: fprintf(f, "green"); break; case BLUE: fprintf(f, "blue"); break; default: fprintf(f, "UNKNOWN"); break; } } /* print help for luminance functions */ void print_lum_help(void) { puts("rec601 use gamma corrected RGB values according to ITU-R Rec. BT.601-4"); puts("rec709 use linear RGB values according to ITU-R Rec. BT.709"); puts("linear use (R+G+B)/3 as done by cvtool 0.0.1"); puts("minimum use min(R,G,B) as done by GNU Ocrad 0.14"); puts("maximum use max(R,G,B)"); puts("red use R value"); puts("green use G value"); puts("blue use B value"); } /* get keyword corresponding to character set define */ const char *cs_key(charset_t cs) { switch(cs) { case CS_FULL: return "full"; case CS_DIGITS: return "digits"; case CS_DECIMAL: return "decimal"; case CS_HEXADECIMAL: return "hex"; case CS_TT_ROBOT: return "tt_robot"; default: return "UNKNOWN"; } } /* print character set keyword */ void print_cs_key(charset_t cs, FILE *f) { fputs(cs_key(cs), f); } /* print help for character sets */ void print_cs_help(void) { puts("full all characters supported by ssocr"); puts("digits digits 0 to 9"); puts("decimal digits 0 to 9, decimal point, minus"); puts("hex digits 0 to f, decimal point, minus"); puts("tt_robot digits and characters used by a Chinese table tennis robot"); } /* print version */ void print_version(FILE *f) { fprintf(f, "Seven Segment Optical Character Recognition Version %s\n", VERSION); fprintf(f, "Copyright (C) 2004-2025 Erik Auerswald" " \n"); fprintf(f, "This program comes with ABSOLUTELY NO WARRANTY.\n"); fprintf(f, "This is free software, and you are welcome to redistribute it" " under the terms\nof the GNU GPL (version 3 or later).\n"); } /* print short usage */ void short_usage(char *name, FILE *f) { fprintf(f, "\nUsage: %s [OPTION]... [COMMAND]... IMAGE\n", name); fprintf(f, "Use '%s --help' to print long help describing all options\n", name); } /* print usage */ void usage(char *name, FILE *f) { print_version(f); fprintf(f, "\nUsage: %s [OPTION]... [COMMAND]... IMAGE\n", name); fprintf(f, "\nOptions: -h, --help print this message\n"); fprintf(f, " -v, --verbose talk about program execution\n"); fprintf(f, " -V, --version print version information\n"); fprintf(f, " -t, --threshold=THRESH use THRESH (in percent) to distinguish black\n"); fprintf(f, " from white\n"); fprintf(f, " -a, --absolute-threshold don't adjust threshold to image\n"); fprintf(f, " -T, --iter-threshold use iterative thresholding method\n"); fprintf(f, " -n, --number-pixels=# number of pixels needed to recognize a segment\n"); fprintf(f, " -N, --min-segment=SIZE minimum width and height of a segment\n"); fprintf(f, " -i, --ignore-pixels=# number of pixels ignored when searching digit\n"); fprintf(f, " boundaries\n"); fprintf(f, " -M, --min-char-dims=WxH minimum width and height of a character/digit\n"); fprintf(f, " -d, --number-digits=RNG number of digits in image (-1 for auto,\n"); fprintf(f, " positive number, or positive range)\n"); fprintf(f, " -r, --one-ratio=# height/width ratio to recognize a \'one\'\n"); fprintf(f, " -m, --minus-ratio=# width/height ratio to recognize a minus sign\n"); fprintf(f, " -H, --dec-h-ratio=# max_dig_h/h ratio to recognize decimal point\n"); fprintf(f, " -W, --dec-w-ratio=# max_dig_w/w ratio to recognize decimal point\n"); fprintf(f, " -o, --output-image=FILE write processed image to FILE\n"); fprintf(f, " -O, --output-format=FMT use output format FMT (Imlib2 formats)\n"); fprintf(f, " -p, --process-only do image processing only, no OCR\n"); fprintf(f, " -D, --debug-image[=FILE] write a debug image to FILE or %s\n",DEBUG_IMAGE_NAME); fprintf(f, " -P, --debug-output print debug information\n"); fprintf(f, " -f, --foreground=COLOR set foreground color (black or white)\n"); fprintf(f, " -b, --background=COLOR set background color (black or white)\n"); fprintf(f, " -I, --print-info print image dimensions and used lum values\n"); fprintf(f, " -g, --adjust-gray use T1 and T2 from gray_stretch as\n"); fprintf(f, " percentages of used values\n"); fprintf(f, " -l, --luminance=KEYWORD compute luminance using formula KEYWORD\n"); fprintf(f, " use -l help for list of KEYWORDS\n"); fprintf(f, " -s, --print-spaces print spaces between more distant digits\n"); fprintf(f, " -A, --space-factor=FAC relative distance to add spaces\n"); fprintf(f, " -G, --space-average use average instead of minimum distance\n"); fprintf(f, " to determine spaces between digits\n"); fprintf(f, " -S, --ascii-art-segments print recognized segments a ASCII art\n"); fprintf(f, " -X, --print-as-hex change output format to hexadecimal\n"); fprintf(f, " -C, --omit-decimal-point omit decimal points from output\n"); fprintf(f, " -c, --charset=KEYWORD select recognized characters\n"); fprintf(f, " use -c help for list of KEYWORDS\n"); fprintf(f, " -F, --adapt-after-crop do not adapt threshold to image directly\n" " before, only after, cropping\n"); fprintf(f, "\nCommands: dilation [N] [N times] dilation algorithm" "\n (set_pixels_filter with mask" " of 1 pixel)\n"); fprintf(f, " erosion [N] [N times] erosion algorithm\n" " (set_pixels_filter with mask" " of 9 pixels)\n"); fprintf(f, " closing [N] closing algorithm\n"); fprintf(f, " ([N times] dilation then [N times] erosion)\n"); fprintf(f, " opening [N] opening algorithm\n"); fprintf(f, " ([N times] erosion then [N times] dilation)\n"); fprintf(f, " remove_isolated remove isolated pixels\n"); fprintf(f, " make_mono make image monochrome\n"); fprintf(f, " grayscale transform image to grayscale\n"); fprintf(f, " invert make inverted monochrome image\n"); fprintf(f, " gray_stretch T1 T2 stretch luminance values from [T1,T2] to\n"); fprintf(f, " [0,255] (use --adjust-gray for percentages)\n"); fprintf(f, " dynamic_threshold W H make image monochrome w. dynamic thresholding\n"); fprintf(f, " with a window of width W and height H\n"); fprintf(f, " rgb_threshold make image monochrome by setting every pixel\n"); fprintf(f, " with any values of red, green or blue below\n"); fprintf(f, " the threshold to black\n"); fprintf(f, " r_threshold make image monochrome using only red channel\n"); fprintf(f, " g_threshold make image monochrome using only green channel\n"); fprintf(f, " b_threshold make image monochrome using only blue channel\n"); fprintf(f, " white_border [WIDTH] make border of WIDTH (or 1) in background\n"); fprintf(f, " color\n"); fprintf(f, " shear OFFSET shear image OFFSET pixels (at bottom) to the\n"); fprintf(f, " right\n"); fprintf(f, " rotate THETA rotate image clockwise by THETA degrees\n"); fprintf(f, " mirror {horiz|vert} mirror image horizontally or vertically\n"); fprintf(f, " crop X Y W H crop image with upper left corner (X,Y) with\n"); fprintf(f, " width W and height H\n"); fprintf(f, " set_pixels_filter MASK set pixels that have at least MASK neighbor\n"); fprintf(f, " pixels set (including checked position)\n"); fprintf(f, " keep_pixels_filter MASK keeps pixels that have at least MASK neighbor\n"); fprintf(f, " pixels set (not counting the checked pixel)\n"); fprintf(f, "\nDefaults: needed pixels = %2d\n", NEED_PIXELS); fprintf(f, " minimum segment size = %2d\n", MIN_SEGMENT); fprintf(f, " minimum character width = %2d\n", MIN_CHAR_W); fprintf(f, " minimum character height = %2d\n", MIN_CHAR_H); fprintf(f, " ignored pixels = %2d\n", IGNORE_PIXELS); fprintf(f, " minimum number of digits = %2d\n", NUMBER_OF_DIGITS); fprintf(f, " maximum number of digits = %2d\n", NUMBER_OF_DIGITS); fprintf(f, " threshold = %5.2f\n", THRESHOLD); fprintf(f, " foreground = %s\n", (SSOCR_DEFAULT_FOREGROUND == SSOCR_BLACK) ? "black" : "white"); fprintf(f, " background = %s\n", (SSOCR_DEFAULT_BACKGROUND == SSOCR_BLACK) ? "black" : "white"); fprintf(f, " luminance = "); print_lum_key(DEFAULT_LUM_FORMULA, f); fprintf(f, "\n"); fprintf(f, " height/width threshold for digit one = %2d\n", ONE_RATIO); fprintf(f, " width/height threshold for minus sign = %2d\n", MINUS_RATIO); fprintf(f, " max_dig_h/h threshold for decimal sep = %2d\n", DEC_H_RATIO); fprintf(f, " max_dig_w/w threshold for decimal sep = %2d\n", DEC_W_RATIO); fprintf(f, " space width factor = %.2f\n", SPC_FAC); fprintf(f, " character set = "); print_cs_key(DEFAULT_CHARSET, f); fputs("\n", f); fprintf(f, "\nOperation: The IMAGE is read, the COMMANDs are processed in the sequence\n"); fprintf(f, " they are given, in the resulting image the given number of digits\n"); fprintf(f, " are searched and recognized, after which the recognized number is\n"); fprintf(f, " written to STDOUT.\n"); fprintf(f, " The recognition algorithm works with set or unset pixels and uses\n"); fprintf(f, " the given THRESHOLD to decide if a pixel is set or not.\n"); fprintf(f, " Use - for IMAGE to read the image from STDIN.\n\n"); fprintf(f, "Exit Codes: 0 if correct number of digits have been recognized\n"); fprintf(f, " 1 if a different number of digits have been found\n"); fprintf(f, " 2 if one of the digits could not be recognized\n"); fprintf(f, " 3 if successful image processing only\n"); fprintf(f, " 42 if -h, -V, -l help, or -c help\n"); fprintf(f, " 99 otherwise\n"); } ssocr-2.25.0/help.h000066400000000000000000000025771477004664000140550ustar00rootroot00000000000000/* Seven Segment Optical Character Recognition Help Functions */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* Copyright (C) 2004-2025 Erik Auerswald */ #ifndef SSOCR2_HELP_H #define SSOCR2_HELP_H /* functions */ /* print usage */ void short_usage(char *name, FILE *f); void usage(char *name, FILE *f); /* print help for luminance functions */ void print_lum_help(void); /* print version */ void print_version(FILE *f); /* print luminance keyword */ void print_lum_key(luminance_t lt, FILE *f); /* get keyword corresponding to character set define */ const char *cs_key(charset_t cs); /* print character set keyword */ void print_cs_key(charset_t cs, FILE *f); /* print help for character sets */ void print_cs_help(void); #endif /* SSOCR2_HELP_H */ ssocr-2.25.0/imgproc.c000066400000000000000000001037031477004664000145510ustar00rootroot00000000000000/* Seven Segment Optical Character Recognition Image Processing Functions */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* Copyright (C) 2004-2025 Erik Auerswald */ /* ImLib2 Header */ #include /* needed by Imlib2.h */ #include /* standard things */ #include /* puts, printf, BUFSIZ, perror, FILE */ #include /* exit */ /* string manipulation */ #include /* strcasecmp, strcmp, strrchr */ /* trigonometry */ #include /* sin, cos, M_PI */ #ifndef M_PI /* sometimes, M_PI is not defined */ #define M_PI 3.14159265358979323846 #endif /* my headers */ #include "defines.h" /* defines */ #include "imgproc.h" /* image processing */ #include "help.h" /* online help */ /* global variables */ extern int ssocr_foreground; extern int ssocr_background; /* functions */ /*** image processing ***/ /* set foreground color */ void set_fg_color(int color) { ssocr_foreground = color; } /* set background color */ void set_bg_color(int color) { ssocr_background = color; } /* set imlib color */ void ssocr_set_color(fg_bg_t color) { switch(color) { case FG: imlib_context_set_color(ssocr_foreground, ssocr_foreground, ssocr_foreground, 255); break; case BG: imlib_context_set_color(ssocr_background, ssocr_background, ssocr_background, 255); break; default: fprintf(stderr, "%s: error: ssocr_set_color(): unknown color %d\n", PROG, color); exit(99); break; } } /* draw a fore- or background pixel */ void draw_fg_bg_pixel(Imlib_Image *image, int x, int y, fg_bg_t color) { Imlib_Image *current_image; /* save current image */ current_image = imlib_context_get_image(); imlib_context_set_image(*image); ssocr_set_color(color); imlib_image_draw_pixel(x,y,0); imlib_context_set_image(current_image); } /* draw a foreground pixel */ void draw_fg_pixel(Imlib_Image *image, int x, int y) { draw_fg_bg_pixel(image, x, y, FG); } /* draw a background pixel */ void draw_bg_pixel(Imlib_Image *image, int x, int y) { draw_fg_bg_pixel(image, x, y, BG); } /* draw a pixel of a given color */ void draw_color_pixel(Imlib_Image *image, int x, int y, Imlib_Color color) { Imlib_Image *current_image; /* save current image */ current_image = imlib_context_get_image(); imlib_context_set_image(*image); imlib_context_set_color(color.red, color.green, color.blue, color.alpha); imlib_image_draw_pixel(x, y, 0); imlib_context_set_image(current_image); } /* check if a pixel is set regarding current foreground/background colors */ int is_pixel_set(int value, double threshold) { switch(ssocr_foreground) { case SSOCR_BLACK: if(value < threshold/100.0*MAXRGB) { return 1; } else { return 0; } break; case SSOCR_WHITE: if(value >= threshold/100.0*MAXRGB) { return 1; } else { return 0; } break; default: fprintf(stderr, "%s: error: is_pixel_set(): foreground color neither" " black nor white\n", PROG); exit(99); break; } } /* set pixels that have at least mask pixels around it set (including the * examined pixel itself) to black (foreground), all other pixels to white * (background) */ Imlib_Image set_pixels_filter(Imlib_Image *source_image, double thresh, luminance_t lt, int mask) { Imlib_Image new_image; /* construct filtered image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y,i,j; /* iteration variables */ int set_pixel; /* should pixel be set or not? */ Imlib_Color color; int lum; /* luminance value of pixel */ /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* check for every pixel if it should be set in filtered image */ for(x=0; x=0 && i=0 && j= mask) { draw_fg_pixel(&new_image, x, y); } else { draw_bg_pixel(&new_image, x, y); } } } /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; } Imlib_Image set_pixels_filter_iter(Imlib_Image *source_image, double thresh, luminance_t lt, int mask, int iter) { int i; Imlib_Image temp_image1, temp_image2; imlib_context_set_image(*source_image); temp_image1 = temp_image2 = imlib_clone_image(); for(i=0; i=0 && i=0 && j mask) { draw_fg_pixel(&new_image, x, y); } else { draw_bg_pixel(&new_image, x, y); } } } /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; } Imlib_Image remove_isolated(Imlib_Image *source_image, double thresh, luminance_t lt) { return keep_pixels_filter(source_image, thresh, lt, 1); } /* gray stretching, i.e. lum lum=0, lum>t2 => lum=100, * else lum=((lum-t1)*MAXRGB)/(t2-t1) */ Imlib_Image gray_stretch(Imlib_Image *source_image, double t1, double t2, luminance_t lt) { Imlib_Image new_image; /* construct filtered image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y; /* iteration variables */ Imlib_Color color; int lum; /* luminance value of pixel */ /* do nothing if t1>=t2 */ if(t1 >= t2) { fprintf(stderr, "%s: error: gray_stretch(): t1=%.2f >= t2=%.2f\n", PROG, t1, t2); exit(99); } /* check if 0 < t1,t2 < MAXRGB */ if(t1 <= 0.0) { fprintf(stderr, "%s: error: gray_stretch(): t1=%.2f <= 0.0\n", PROG, t1); exit(99); } if(t2 >= MAXRGB) { fprintf(stderr, "%s: error: gray_stretch(): t2=%.2f >= %d.0\n", PROG, t2, MAXRGB); exit(99); } /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* gray stretch image */ for(x=0; x=t2) { imlib_context_set_color(MAXRGB, MAXRGB, MAXRGB, color.alpha); } else { imlib_context_set_color(clip(((lum-t1)*255)/(t2-t1),0,255), clip(((lum-t1)*255)/(t2-t1),0,255), clip(((lum-t1)*255)/(t2-t1),0,255), color.alpha); } imlib_image_draw_pixel(x, y, 0); imlib_context_set_image(*source_image); } } /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; } /* use dynamic (aka adaptive) local thresholding to create monochrome image */ /* ww and wh are the width and height of the rectangle used to find the * threshold value */ Imlib_Image dynamic_threshold(Imlib_Image *source_image,double t,luminance_t lt, int ww, int wh) { Imlib_Image new_image; /* construct filtered image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y; /* iteration variables */ Imlib_Color color; int lum; double thresh; /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* check for every pixel if it should be set in filtered image */ for(x=0; x", t); t = get_threshold(image, thresh/100.0, lt, 0, 0, -1, -1); if(flags & DEBUG_OUTPUT) fprintf(stderr, " %f\n", t); if(flags & DO_ITERATIVE_THRESHOLD) { if(flags & DEBUG_OUTPUT) fprintf(stderr, "doing iterative_thresholding: %f ->", t); t = iterative_threshold(image, t, lt); if(flags & DEBUG_OUTPUT) fprintf(stderr, " %f\n", t); } is_adapted = 1; } if((flags & VERBOSE) || (flags & DEBUG_OUTPUT)) { fprintf(stderr, "using threshold %.2f\n", t); } return t; } /* compute dynamic threshold value from the rectangle (x,y),(x+w,y+h) of * source_image */ double get_threshold(Imlib_Image *source_image, double fraction, luminance_t lt, int x, int y, int w, int h) { Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int xi,yi; /* iteration variables */ Imlib_Color color; int lum; /* luminance of pixel */ double minval=(double)MAXRGB, maxval=0.0; /* save pointer to current image */ current_image = imlib_context_get_image(); /* get image dimensions */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); /* special value -1 for width or height means image width/height */ if(w == -1) w = width; if(h == -1) h = width; /* assure valid coordinates */ if(x+w > width) x = width-w; if(y+h > height) y = height-h; if(x<0) x=0; if(y<0) y=0; /* find the threshold value to differentiate between dark and light */ for(xi=0; (xi maxval) maxval = lum; } } /* restore image from before function call */ imlib_context_set_image(current_image); return (minval + fraction * (maxval - minval)) * 100 / MAXRGB; } /* determine threshold by an iterative method */ double iterative_threshold(Imlib_Image *source_image, double thresh, luminance_t lt) { Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int xi,yi; /* iteration variables */ Imlib_Color color; int lum; /* luminance of pixel */ unsigned int size_white, size_black; /* size of black and white groups */ unsigned long int sum_white, sum_black; /* sum of black and white groups */ unsigned int avg_white, avg_black; /* average values of black and white */ double old_thresh; /* old threshold computed by last iteration step */ double new_thresh; /* new threshold computed by current iteration step */ int thresh_lum; /* luminance value of threshold */ /* normalize threshold (was given as a percentage) */ new_thresh = thresh / 100.0; /* save pointer to current image */ current_image = imlib_context_get_image(); /* get image dimensions */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); /* find the threshold value to differentiate between dark and light */ do { thresh_lum = MAXRGB * new_thresh; old_thresh = new_thresh; size_black = sum_black = size_white = sum_white = 0; for(xi=0; xi EPSILON); /* restore image from before function call */ imlib_context_set_image(current_image); return new_thresh * 100; } /* get minimum and maximum lum values */ void get_minmaxval(Imlib_Image *source_image, luminance_t lt, double *min, double *max) { Imlib_Image current_image; /* save image pointer */ int w, h; /* image dimensions */ int xi,yi; /* iteration variables */ Imlib_Color color; /* Imlib2 RGBA color structure */ int lum = 0; *min = MAXRGB; *max = 0; /* save pointer to current image */ current_image = imlib_context_get_image(); /* get image dimensions */ imlib_context_set_image(*source_image); h = imlib_image_get_height(); w = imlib_image_get_width(); /* find the minimum value in the image */ for(xi=0; xi *max) *max = lum; } } /* restore image from before function call */ imlib_context_set_image(current_image); } /* draw a white (background) border around image, overwriting image contents * beneath border*/ Imlib_Image white_border(Imlib_Image *source_image, int bdwidth) { Imlib_Image new_image; /* construct filtered image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y; /* coordinates of upper left corner of rectangles */ /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* assure border width has a legal value */ if(bdwidth > width/2) bdwidth = width/2; if(bdwidth > height/2) bdwidth = height/2; /* draw white (background) rectangle around new image */ for(x=0, y=0; x=shift; x--) { imlib_image_query_pixel(x-shift, y, &color_return); draw_color_pixel(&new_image, x, y, color_return); } /* fill with background */ for(x=0; x= 0) && (sx <= width) && (sy >= 0) && (sy <= height)) { imlib_image_query_pixel(sx, sy, &c); draw_color_pixel(&new_image, x, y, c); } else { draw_bg_pixel(&new_image, x, y); } } } /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; } /* mirror image horizontally or vertically */ Imlib_Image mirror(Imlib_Image *source_image, direction_t direction) { Imlib_Image new_image; /* construct filtered image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y; /* iteration variables / target coordinates */ Imlib_Color c; /* for imlib_query_pixel() */ /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* create mirrored image */ if(direction == HORIZONTAL) { for(x = width-1; x>=0; x--) { for(y = 0; y < height; y++) { imlib_image_query_pixel(width - 1 - x, y, &c); draw_color_pixel(&new_image, x, y, c); } } } else if(direction == VERTICAL) { for(x = 0; x < width; x++) { for(y = height-1; y >= 0; y--) { imlib_image_query_pixel(x, height - 1 - y, &c); draw_color_pixel(&new_image, x, y, c); } } } /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; } /* turn image to grayscale */ Imlib_Image grayscale(Imlib_Image *source_image, luminance_t lt) { Imlib_Image new_image; /* construct grayscale image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y; /* iteration variables */ Imlib_Color color; /* Imlib2 color structure */ int lum=0; /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* transform image to grayscale */ for(x=0; x= width) x = width - 1; if(y >= height) y = height - 1; if(x + w > width) w = width - x; if(y + h > height) h = height - x; /* create the new image */ imlib_context_set_image(*source_image); new_image = imlib_create_cropped_image(x, y, w, h); /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; } /* compute luminance from RGB values */ int get_lum(Imlib_Color *color, luminance_t lt) { switch(lt) { case REC709: return get_lum_709(color); case REC601: return get_lum_601(color); case LINEAR: return get_lum_lin(color); case MINIMUM: return get_lum_min(color); case MAXIMUM: return get_lum_max(color); case RED: return get_lum_red(color); case GREEN: return get_lum_green(color); case BLUE: return get_lum_blue(color); default: fprintf(stderr, "%s: error: get_lum(): unknown transfer function" " no. %d\n", PROG, lt); exit(99); } } /* compute luminance Y_709 from linear RGB values */ int get_lum_709(Imlib_Color *color) { return 0.2125*color->red + 0.7154*color->green + 0.0721*color->blue; } /* compute luminance Y_601 from gamma corrected (non-linear) RGB values */ int get_lum_601(Imlib_Color *color) { return 0.299*color->red + 0.587*color->green + 0.114*color->blue; } /* compute luminance Y = (R+G+B)/3 */ int get_lum_lin(Imlib_Color *color) { return (color->red + color->green + color->blue) / 3; } /* compute luminance Y = min(R,G,B) as used in GNU Ocrad 0.14 */ int get_lum_min(Imlib_Color *color) { return (color->red < color->green) ? ((color->red < color->blue) ? color->red : color->blue) : ((color->green < color->blue) ? color->green : color->blue); } /* compute luminance Y = max(R,G,B) */ int get_lum_max(Imlib_Color *color) { return (color->red > color->green) ? ((color->red > color->blue) ? color->red : color->blue) : ((color->green > color->blue) ? color->green : color->blue); } /* compute luminance Y = R */ int get_lum_red(Imlib_Color *color) { return color->red; } /* compute luminance Y = G */ int get_lum_green(Imlib_Color *color) { return color->green; } /* compute luminance Y = B */ int get_lum_blue(Imlib_Color *color) { return color->blue; } /* clip value thus that it is in the given interval [min,max] */ int clip(int value, int min, int max) { return (value < min) ? min : ((value > max) ? max : value); } /* save image to file */ void save_image(const char *image_type, Imlib_Image *image, const char *fmt, const char *filename, unsigned int flags) { const char *tmp; Imlib_Image *current_image; Imlib_Load_Error save_error=0; const char *const stdout_file = "/proc/self/fd/1"; current_image = imlib_context_get_image(); imlib_context_set_image(image); /* interpret - as STDOUT */ if(strcmp("-", filename) == 0) filename = stdout_file; /* get file format for image */ if(fmt) { /* use provided format string */ tmp = fmt; } else { /* use file name extension */ tmp = strrchr(filename, '.'); if(tmp) tmp++; } if(tmp) { if(flags & VERBOSE) fprintf(stderr, "using %s format for %s image\n", tmp, image_type); imlib_image_set_format(tmp); } else { /* use png as default */ if(flags & VERBOSE) fprintf(stderr, "using png format for %s image\n", image_type); imlib_image_set_format("png"); } /* write image to disk */ if(flags & VERBOSE) fprintf(stderr, "writing %s image to file %s\n", image_type, filename); imlib_save_image_with_error_return(filename, &save_error); if(save_error && save_error != IMLIB_LOAD_ERROR_NONE) { fprintf(stderr, "%s: error saving image file %s\n", PROG, filename); report_imlib_error(save_error); } imlib_context_set_image(current_image); } /* parse KEYWORD from --luminace option */ luminance_t parse_lum(char *keyword) { if(strcasecmp(keyword, "help") == 0) { print_lum_help(); exit(42); } else if(strcasecmp(keyword, "rec601") == 0) { return REC601; } else if(strcasecmp(keyword, "rec709") == 0) { return REC709; } else if(strcasecmp(keyword, "linear") == 0) { return LINEAR; } else if(strcasecmp(keyword, "minimum") == 0) { return MINIMUM; } else if(strcasecmp(keyword, "maximum") == 0) { return MAXIMUM; } else if(strcasecmp(keyword, "red") == 0) { return RED; } else if(strcasecmp(keyword, "green") == 0) { return GREEN; } else if(strcasecmp(keyword, "blue") == 0) { return BLUE; } else { return LUM_PARSE_ERROR; } } /* report Imlib2 load/save error to stderr */ void report_imlib_error(Imlib_Load_Error error) { fputs(PROG ": Imlib2 error code: ",stderr); switch (error) { case IMLIB_LOAD_ERROR_NONE: fputs("IMLIB_LOAD_ERROR_NONE\n", stderr); break; case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST: fputs("IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST\n", stderr); break; case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY: fputs("IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY\n", stderr); break; case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ: fputs("IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ\n", stderr); break; case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT: fputs("IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT\n", stderr); break; case IMLIB_LOAD_ERROR_PATH_TOO_LONG: fputs("IMLIB_LOAD_ERROR_PATH_TOO_LONG\n", stderr); break; case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT: fputs("IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT\n", stderr); break; case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY: fputs("IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY\n", stderr); break; case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE: fputs("IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE\n", stderr); break; case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS: fputs("IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS\n", stderr); break; case IMLIB_LOAD_ERROR_OUT_OF_MEMORY: fputs("IMLIB_LOAD_ERROR_OUT_OF_MEMORY\n", stderr); break; case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS: fputs("IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS\n", stderr); break; case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE: fputs("IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE\n", stderr); break; case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE: fputs("IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE\n", stderr); break; case IMLIB_LOAD_ERROR_UNKNOWN: fputs("IMLIB_LOAD_ERROR_UNKNOWN\n", stderr); break; default: fprintf(stderr, "unknown error code %d, please report\n", error); break; } } ssocr-2.25.0/imgproc.h000066400000000000000000000152041477004664000145540ustar00rootroot00000000000000/* Seven Segment Optical Character Recognition Image Processing Functions */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* Copyright (C) 2004-2025 Erik Auerswald */ #ifndef SSOCR2_IMGPROC_H #define SSOCR2_IMGPROC_H /* parse luminance keyword */ luminance_t parse_lum(char *keyword); /* set foreground color */ void set_fg_color(int color); /* set background color */ void set_bg_color(int color); /* set imlib color */ void ssocr_set_color(fg_bg_t color); /* draw a fore- or background pixel */ void draw_fg_bg_pixel(Imlib_Image *image, int x, int y, fg_bg_t color); /* draw a foreground pixel */ void draw_fg_pixel(Imlib_Image *image, int x, int y); /* draw a background pixel */ void draw_bg_pixel(Imlib_Image *image, int x, int y); /* draw a pixel of a given color */ void draw_color_pixel(Imlib_Image *image, int x, int y, Imlib_Color color); /* check if a pixel is set regarding current foreground/background colors */ int is_pixel_set(int value, double threshold); /* set pixel if at least mask neighboring pixels (including the pixel to be set) * are set * a pixel is set if its luminance value is less than thresh * mask=1 is the standard dilation filter * mask=9 is the standard erosion filter */ Imlib_Image set_pixels_filter(Imlib_Image *source_image, double thresh, luminance_t lt, int mask); /* perform set pixel filter operation iter times */ Imlib_Image set_pixels_filter_iter(Imlib_Image *source_image, double thresh, luminance_t lt, int mask, int iter); /* shortcut for dilation */ Imlib_Image dilation(Imlib_Image *source_image, double thresh, luminance_t lt, int n); /* shortcut for erosion */ Imlib_Image erosion(Imlib_Image *source_image, double thresh, luminance_t lt, int n); /* shortcut for closing */ Imlib_Image closing(Imlib_Image *source_image, double thresh, luminance_t lt, int n); /* shortcut for opening */ Imlib_Image opening(Imlib_Image *source_image, double thresh, luminance_t lt, int n); /* keep only pixels that have at least mask-1 neighbors set */ Imlib_Image keep_pixels_filter(Imlib_Image *source_image, double thresh, luminance_t lt, int mask); /* remove isolated pixels (shortcut for keep_pixels_filter with mask = 2) */ Imlib_Image remove_isolated(Imlib_Image *source_image, double thresh, luminance_t lt); /* gray stretching, i.e. lum lum=0, lum>t2 => lum=100, * else lum=((lum-t1)*MAXRGB)/(t2-t1) */ Imlib_Image gray_stretch(Imlib_Image *source_image, double t1, double t2, luminance_t lt); /* use dynamic (aka adaptive) local thresholding to create monochrome image */ Imlib_Image dynamic_threshold(Imlib_Image *source_image, double t, luminance_t lt ,int ww, int wh); /* make black and white */ Imlib_Image make_mono(Imlib_Image *source_image, double thresh, luminance_t lt); /* set pixel to black (0,0,0) if R Build-Depends: debhelper (>= 7), libimlib2-dev, libfreetype6-dev, zlib1g-dev, libx11-dev, libxext-dev Standards-Version: 3.8.1 Homepage: https://www.unix-ag.uni-kl.de/~auerswal/ssocr/ Package: ssocr Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends} Description: OCR for seven segment displays Seven Segment Optical Character Recognition or ssocr for short is a program to recognize digits of a seven segment display. An image of one row of digits is used for input and the recognized number is written to the standard output. ssocr-2.25.0/notdebian/copyright000066400000000000000000000023571477004664000166460ustar00rootroot00000000000000This package was debianized by: Erik Auerswald on Thu, 06 Aug 2009 09:16:53 +0200 It was downloaded from: https://www.unix-ag.uni-kl.de/~auerswal/ssocr/ Upstream Author(s): Erik Auerswald Copyright: Copyright (C) 2004-2025 Erik Auerswald License: This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . On Debian systems, the complete text of the GNU General Public License version 3 can be found in `/usr/share/common-licenses/GPL-3'. The Debian packaging is: Copyright (C) 2009 Erik Auerswald and is licensed under the GPL version 3, see above. ssocr-2.25.0/notdebian/rules000077500000000000000000000022731477004664000157700ustar00rootroot00000000000000#!/usr/bin/make -f # -*- makefile -*- # Sample debian/rules that uses debhelper. # This file was originally written by Joey Hess and Craig Small. # As a special exception, when this file is copied by dh-make into a # dh-make output file, you may use that output file without restriction. # This special exception was added by Craig Small in version 0.37 of dh-make. # Uncomment this to turn on verbose mode. #export DH_VERBOSE=1 configure: configure-stamp configure-stamp: dh_testdir touch configure-stamp build: build-stamp build-stamp: configure-stamp dh_testdir $(MAKE) touch $@ clean: dh_testdir dh_testroot rm -f build-stamp configure-stamp $(MAKE) clean dh_clean install: build dh_testdir dh_testroot dh_prep dh_installdirs $(MAKE) PREFIX=/usr DESTDIR=$(CURDIR)/debian/ssocr install # Build architecture-independent files here. binary-indep: install # We have nothing to do by default. # Build architecture-dependent files here. binary-arch: install dh_testdir dh_testroot dh_link dh_fixperms dh_installdeb dh_shlibdeps dh_gencontrol dh_md5sums dh_builddeb binary: binary-indep binary-arch .PHONY: build clean binary-indep binary-arch binary install configure ssocr-2.25.0/ssocr.1.in000066400000000000000000000371461477004664000145740ustar00rootroot00000000000000.TH ssocr 1 "@DATE@" "@VERSION@" "OCR for seven segment displays" .SH NAME ssocr \- optical recognition of seven segment displays .SH SYNOPSIS .B ssocr [OPTION]... [COMMAND]... IMAGE .SH DESCRIPTION .B ssocr reads an image file containing the picture of a seven segment display, recognizes the displayed digits and prints them to standard output. All image formats known by imlib2 are supported. Use .B \- as file name to read the image from standard input. .B ssocr provides several image manipulation algorithms to enhance noisy images. .PP .B Options can be used to change .B ssocr behavior. .PP .B Commands can be used to manipulate the input .I IMAGE before starting the recognition algorithm. .PP Two hyphens .RB ( \-\- ) can be used as a special argument to end option-scanning, e.g., in order to use a negative number as argument to a command. .PP When using a single character (i.e., short) option, arguments can either directly follow the option character, or can be separated from the option character by whitespace. When using a multi character (i.e., long) option, arguments must be separated from the option by either an equals sign .RB ( = ), or whitespace. .SH OPTIONS .SS \-h, \-\-help Write a help message to standard output. The default settings are shown as well. .SS \-V, \-\-version Write version information to standard output. .SS \-v, \-\-verbose Print information about program execution to standard error. .SS \-t, \-\-threshold THRESHOLD Specify a percentage value as luminance threshold to differentiate between black and white. This threshold is adjusted to the luminance values occurring in the image, unless option .B \-\-absolute\-threshold is used. The default threshold is .IR 50 . .SS \-a, \-\-absolute\-threshold Do not adjust the threshold to the luminance values occurring in the image. Using this option also inhibits iterative thresholding using option .BR \-\-iter\-threshold . Consider this option when using the .B dynamic_threshold or .B gray_stretch commands. .SS \-T, \-\-iter\-threshold Use an iterative method (one-dimensional k-means clustering) to determine the threshold. The starting value can be specified with the .B \-\-threshold option. Option .B \-\-absolute\-threshold inhibits iterative threshold determination. .SS \-n, \-\-number\-pixels NUMBER Set the number of foreground pixels that have to be found in a scanline to recognize a segment. This does not apply to ratio based recognition. Can be used to ignore some noise in the picture. See the web page of .BR ssocr (1) for a description of the algorithm. .SS \-N, \-\-min\-segment SIZE Set the minimum number of pixels required for width and height of an individual segment of a seven segment display. A set segment in the display must have both a width and height of at least .B SIZE pixels. This minimum is used for both scanline based and ratio based recognition. It is not applied to decimal separator detection, because those are not comprised of regular segments. This option can be used to ignore some noise in the picture. See the web page of .BR ssocr (1) for a description of the algorithm. .SS \-i, \-\-ignore\-pixels NUMBER Set the number of foreground pixels that are ignored when deciding if a column or row consists only of background or foreground pixels. Can be used to ignore some noise in the picture. See the web page of .BR ssocr (1) for a description of the algorithm. .SS \-M, \-\-min\-char\-dims WIDTHxHEIGHT Specify the minimum dimensions of characters respectively digits. When the segmentation step finds potential digits, those with a width less than .B WIDTH or a height less than .B HEIGHT are ignored. Can be used to ignore some noise in the picture. See the web page of .BR ssocr (1) for a description of the algorithm. .SS \-d, \-\-number\-digits RANGE Specifies the number of digits shown in the image. Default value is .IR 6 . Use .I \-1 to automatically detect the number of digits. Use a single positive number to specify an exact number of digits. Use two positive numbers separated with a hyphen .RI ( - ) to specify an inclusive range of acceptable values for the number of digits. .SS \-r, \-\-one\-ratio RATIO Set the height/width ratio threshold to recognize a digit as a one. A digit with a height/width ratio greater than .B RATIO is recognized as a one. .B RATIO takes integers only. See the web page of .BR ssocr (1) for a description of the algorithm. .SS \-m, \-\-minus\-ratio RATIO Set the width/height ratio to recognize a minus sign. A digit with a width/height ratio greater than or equal to .B RATIO is recognized as a minus sign. .B RATIO takes integers only. This uses the same idea as recognizing the digit one. .SS \-H, \-\-dec\-h\-ratio RATIO Set the max_digit_height/height ratio used for recognition of a decimal separator. .B RATIO takes integers only. This value is used in combination with the max_digit_width/width ratio. If the height of a digit is less than .RB 1/ RATIO of the maximum digit height in the image and the max_digit_width/width threshold is also reached, it is recognized as a decimal separator. .SS \-W, \-\-dec\-w\-ratio RATIO Set the max_digit_width/width ratio used for recognition of a decimal separator. .B RATIO takes integers only. This value is used in combination with the max_digit_height/height ratio. If the width of a digit is less than .RB 1/ RATIO of the maximum digit width in the image and the max_digit_height/height threshold is also reached, it is recognized as a decimal separator. .SS \-o, \-\-output\-image FILE Write the processed image to .BR FILE . Use .B \- to write to standard output. Unless this option is used no image is written to disk. If a standard filename extension is used it is interpreted as the image format to use. Can be useful together with the .B \-\-process\-only option. .SS \-O, \-\-output\-format FORMAT Specify the image format to use with .BR \-\-output\-image . This format must be recognized by imlib2. Standard filename extensions are used to describe the format. Overwrites the image file format automatically determined via the filename. If no format is specified via this option or the filename, .IR png is used. .SS \-p, \-\-process\-only Use .BR ssocr (1) as an image manipulation program. No image recognition is performed. Should be used together with the \-B \-\-output\-image option. .SS \-D, \-\-debug\-image[=FILE] Write a debug image showing the results of thresholding, segmentation and character recognition to disk. The image is written to the file .I testbild.png unless a filename .B FILE is given. This debug image often helps in understanding why .BR ssocr (1) does not recognize the number from a given image. .SS \-P, \-\-debug\-output Print information helpful for debugging to standard error. .SS \-f, \-\-foreground COLOR Specify the foreground color (either .I black or .IR white ). This automatically sets the background color as well. Default is .IR black . .SS \-b, \-\-background COLOR Specify the background color (either .I black or .IR white ). This automatically sets the foreground color as well. Default is .IR white . .SS \-I, \-\-print\-info Prints image dimensions and range of used luminance values to standard error. .SS \-g, \-\-adjust\-gray Interpret the values .B T1 and .B T2 given to the command .B gray_stretch as percentages instead of absolute luminance values. .SS \-l, \-\-luminance KEYWORD Choose the type of luminace computation. Using .I help as .B KEYWORD prints the list of implemented luminance keywords with a short description of the used formula. The default of .I Rec709 should work well in most cases. .SS \-s, \-\-print\-spaces Print space characters between digits (characters) that are farther apart than a factor times the minimum (default) or average distance between digits (characters). .SS \-A, \-\-space\-factor FACTOR Use the given .B FACTOR instead of the default value to determine white space between digits (characters). .SS \-G, \-\-space\-average Use the average distance between digits (characters) instead of the minimum distance to determine white space between digits. .SS \-S, \-\-ascii\-art\-segments Prints the recognized segments, i.e. the display as seen by .BR ssocr , as ASCII art to standard error. .SS \-X, \-\-print\-as\-hex Prints the recognized segments as a string of hexadecimal numbers separated by a colon instead of digits and characters. Each number comprises two hexadecimal digits (one byte). .I 0x01 represents the upper horizontal segment, .I 0x02 represents the upper left vertical segment, .I 0x04 represents the upper right vertical segment, .I 0x08 represents the middle horizontal segment, .I 0x10 represents the lower left vertical segment, .I 0x20 represents the lower right vertical segment, .I 0x40 represents the lower horizontal segment, .I 0x80 represents a decimal point (or comma or thousands separator). Each hexadecimal number printed is the logical .I or of the set segments. .SS \-C, \-\-omit\-decimal\-point Omit decimal points from output. Decimal points are still recognized and counted against the number of digits. This can be used together with automatically detecting the number of digits to ignore isolated groups of pixels in an image. .SS \-c, \-\-charset KEYWORD Select the set of characters that ssocr shall recognize. This affects, e.g., if a display showing a six with missing top segment is recognized as .I 6 (with digits and decimal) or .I b (with hexadecimal and full). Using .I help as .B KEYWORD prints the list of implemented character set keywords with a short description of the included characters. The default is .I full (recognizing all characters known to ssocr in the image). .SS \-F, \-\-adapt\-after\-crop When using the .B crop command, adjust (adapt) the threshold to image luminance values only after cropping, not also directly before. Using other commands before .B crop can still lead to adapting the threshold to the original image. .SH COMMANDS Most commands do not change the image dimensions. The .B crop command is a notable exception to this rule. .SS dilation [N] Filter image using dilation algorithm. Any pixel with at least one neighbour pixel set in the source image will be set in the filtered image. If a number .B N > .I 1 is specified, the dilation algorithm is executed .B N times. .SS erosion [N] Filter image using erosion algorithm. Any pixel with every neighbour pixel set in the source image will be set in the filtered image. If a number .B N > .I 1 is specified, the erosion algorithm is executed .B N times. .SS closing [N] Filter image using closing algorithm, i.e. erosion and then dilation. If a number .B N > .I 1 is specified, .B N times dilation and then .B N times erosion is executed. .SS opening [N] Filter image using opening algorithm, i.e. dilation and then erosion. If a number .B N > .I 1 is specified, .B N times dilation and then .B N times erosion is executed. .SS remove_isolated Remove any foreground pixels without neighbouring foreground pixels. .SS make_mono Convert the image to monochrome using thresholding. The threshold can be specified with option .B \-\-threshold and is adjusted to the used luminance interval of the image unless option .B \-\-absolute\-threshold is used. .SS grayscale Transform image to gray values using luminance. The formula to compute luminance can be specified using option .BR \-\-luminance . .SS invert Set every foreground pixel to background color and vice versa. .SS gray_stretch T1 T2 Transform image so that the luminance interval [ .BR T1 , T2 ] is projected to [ .IR 0 , 255 ] with any value below .B T1 set to .I 0 and any value above .B T2 set to .IR 255 . Together with the option .BR \-\-adjust\-gray , the values .B T1 and .B T2 are interpreted as percentages. Consider using the .B \-\-absolute\-threshold option together with a manually adjusted .B \-\-threshold for predictable results. .SS dynamic_threshold W H Convert the image to monochrome using dynamic thresholding a.k.a. local adaptive thresholding. A window of width .B W and height .B H around the current pixel is used to determine the (local) thresholding value. Consider using the .B \-\-absolute\-threshold option together with a manually adjusted .B \-\-threshold for predictable results. .SS rgb_threshold Convert the image to monochrome using simple thresholding for every color channel. This is the same as .B \-\-luminance=minimum make_mono. You should use .B \-\-luminance=minimum and .B make_mono or .B dynamic_threshold instead. .SS r_threshold Convert the image to monochrome using simple thresholding. Only the red color channel is used. This is the same as .B \-\-luminance=red make_mono. You should use .B \-\-luminance red and .B make_mono or .B dynamic_threshold instead. .SS g_threshold Convert the image to monochrome using simple thresholding. Only the green color channel is used. This is the same as .B \-\-luminance=green make_mono. You should use .B \-\-luminance green and .B make_mono or .B dynamic_threshold instead. .SS b_threshold Convert the image to monochrome using simple thresholding. Only the blue color channel is used. This is the same as .B \-\-luminance=blue make_mono. You should use .B \-\-luminance blue and .B make_mono or .B dynamic_threshold instead. .SS white_border [WIDTH] The border of the image is set to the background color. This border is one pixel wide unless a .B WIDTH > .I 1 is specified. .SS shear OFFSET Shear the image .B OFFSET pixels to the right. The .B OFFSET is used at the bottom. Image dimensions do not change, pixels in background color are used for pixels that are outside the image and shifted inside. Pixels shifted out of the image are dropped. Many seven segment displays use slightly skewed digits, this command can be used to compensate this. Sometimes .BR ssocr (1) cannot separate a decimal point from the preceding digit without shearing the image. .SS rotate THETA Rotate the image .B THETA degrees clockwise around the center of the image. Image dimensions do not change, pixels rotated out of the image area are dropped, pixels from outside the image rotated into the new image are set to the background color. .SS mirror { horiz | vert } Mirror the image horizontally or vertically. .SS crop X Y W H Use only the subpicture with upper left corner ( .BR X , Y ), width .B W and height .BR H . This command changes the image dimensions. .SS set_pixels_filter MASK Set every pixel in the filtered image that has at least .B MASK neighbour pixels set in the source image. .SS keep_pixels_filter MASK Keep only those foreground pixels in the filtered image that have at least .B MASK neighbour pixels set in the source image (not counting the checked pixel itself). .SH "LUMINANCE KEYWORDS" .IP \(bu rec601 .IP \(bu rec709 .IP \(bu linear .IP \(bu minimum .IP \(bu maximum .IP \(bu red .IP \(bu green .IP \(bu blue .SH "CHARACTER SET KEYWORDS" .IP \(bu full .IP \(bu digits .IP \(bu decimal .IP \(bu hex .IP \(bu tt_robot .SH EXIT STATUS .IP \(bu 0, if the correct number of digits have been recognized .IP \(bu 1, if an incorrect number of digits have been found .IP \(bu 2, if not all digits have been recognized .IP \(bu 3, if only image processing was requested and successful .IP \(bu 42, if help or version info was requested .IP \(bu 99, if some other error occurred .SH ENVIRONMENT .B TMP can be used to specify a different directory for temporary files than .BR /tmp . .SH BUGS Imlib2 (and therefore .BR ssocr (1)) does not work well with .BR Netpbm (1) images. .SH AUTHOR .B ssocr was written by Erik Auerswald . .SH COPYRIGHT Copyright \(co @CRYEARS@ Erik Auerswald. License GPLv3+: GNU GPL version 3 or later .UR https://gnu.org/licenses/gpl.html .UE . .br This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. .SH "SEE ALSO" .BR netpbm (1), .BR ImageMagick (1), .P .UR https://www.unix\-ag.uni\-kl.de/~auerswal/ssocr/ .UE ssocr-2.25.0/ssocr.c000066400000000000000000002100051477004664000142340ustar00rootroot00000000000000/* Seven Segment Optical Character Recognition */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* Copyright (C) 2004-2025 Erik Auerswald */ /* Copyright (C) 2013 Cristiano Fontana */ /* ImLib2 Header */ #include /* needed by Imlib2.h */ #include /* standard things */ #include /* INT_MAX */ #include /* SIZE_MAX */ #include /* puts, printf, BUFSIZ, perror, FILE */ #include /* exit */ /* string manipulation */ #include /* memcpy, strchr, strdup, strlen */ /* option parsing */ #include /* getopt */ #include /* getopt, read, write, STDIN_FILENO */ /* file permissions */ #include /* umask */ /* my headers */ #include "defines.h" /* defines */ #include "ssocr.h" /* types */ #include "imgproc.h" /* image processing */ #include "help.h" /* online help */ #include "charset.h" /* character set selection and printing */ /* global variables */ int ssocr_foreground = SSOCR_DEFAULT_FOREGROUND; int ssocr_background = SSOCR_DEFAULT_BACKGROUND; /* functions */ /* copy image from stdin to a temporary file and return the filename */ static char * tmp_imgfile(unsigned int flags) { char *dir; char *name; size_t pattern_len; int handle; unsigned char buf[BUFSIZ]; ssize_t read_count = 0, write_count = 0; size_t pat_suffix_len = strlen(DIR_SEP TMP_FILE_PATTERN); size_t dir_len; /* find a suitable place (directory) for the tmp file and create pattern */ dir = getenv("TMP"); if(dir && strlen(dir) >= SIZE_MAX - pat_suffix_len - 1) { dir = TMP_FILE_DIR; fprintf(stderr, "%s: warning: ignoring too long $TMP env variable\n", PROG); } else if(!dir) { dir = TMP_FILE_DIR; } if(flags & DEBUG_OUTPUT) { fprintf(stderr, "using directory %s for temporary files\n", dir); } dir_len = strlen(dir); pattern_len = dir_len + pat_suffix_len + 1; if(pattern_len <= dir_len) { fputs(PROG ": error: temporary file name length overflow\n", stderr); exit(99); } name = calloc(pattern_len, sizeof(char)); if(!name) { perror(PROG ": could not allocate memory for name of temporary file"); exit(99); } memcpy(name, dir, dir_len); memcpy(name + dir_len, DIR_SEP TMP_FILE_PATTERN, pat_suffix_len); if(flags & DEBUG_OUTPUT) fprintf(stderr, "pattern for temporary file is %s\n", name); /* create temporary file */ umask(S_IRWXG | S_IRWXO); handle = mkstemp(name); if(handle < 0) { perror(PROG ": could not create temporary file"); exit(99); } /* copy image data from stdin to tmp file */ while((read_count = read(STDIN_FILENO, buf, sizeof(buf))) > 0) { write_count = write(handle, buf, read_count); if (write_count <= 0) break; } close(handle); /* filehandle is no longer needed, Imlib2 uses filename */ if(read_count < 0) { perror(PROG ": could not read image from standard input"); exit(99); } if(write_count <= 0) { perror(PROG ": could not copy image data to temporary file"); unlink(name); exit(99); } return name; } /* return number of foreground pixels in a scanline */ static unsigned int scanline(Imlib_Image *debug_image, int x, int y, int len, direction_t dir, color_struct d_color, double thresh, luminance_t lt, unsigned int flags) { Imlib_Color imlib_color, debug_color; int lum, i, ix=x, iy=y, start, end; unsigned int found_pixels = 0; start = (dir == HORIZONTAL) ? x : y; end = start + len; debug_color.red = d_color.R; debug_color.green = d_color.G; debug_color.blue = d_color.B; debug_color.alpha = d_color.A; for (i = start; i <= end; i++) { if (dir == HORIZONTAL) ix = i; else iy = i; imlib_image_query_pixel(ix, iy, &imlib_color); lum = get_lum(&imlib_color, lt); if(is_pixel_set(lum, thresh)) { if(flags & USE_DEBUG_IMAGE) { draw_color_pixel(debug_image, ix, iy, debug_color); } found_pixels++; } } return found_pixels; } /* print given number of space characters to given stream */ static void print_spaces(FILE *f, int n) { int i; for (i = 0; i < n; i++) { fputc(' ', f); } } /* parse dimensions given as a string in the format "WxH" */ static int parse_width_height(const char *s, dimensions_struct *d) { size_t l; const char *width_string; char *height_string; int w, h; if (!s || !d) { fputs(PROG ": error: parse_width_height() called with NULL pointer\n", stderr); return 1; } l = strlen(s); if (l == 0) { fputs(PROG ": error: parse_width_height() called with empty string\n", stderr); return 1; } width_string = s; height_string = strchr(s, 'x'); if (!height_string) { fputs(PROG ": error: no 'x' in dimension specification\n", stderr); return 1; } if (width_string == height_string) { fputs(PROG ": error: width missing from dimension specification\n", stderr); return 1; } height_string++; if (strlen(height_string) == 0) { fputs(PROG ": error: height missing from dimension specification\n",stderr); return 1; } w = atoi(width_string); h = atoi(height_string); if (w < 1 || h < 1) { fprintf(stderr, PROG ": warning: ignoring mininmum character dimensions %dx%d\n", w, h); return 1; } d->w = w; d->h = h; return 0; } /* parse description of interval in one of two formats: * 1) a single number giving both upper and lower bound * 2) two numbers separated by a hyphen */ static int parse_interval(const char *s, interval_struct *i) { int min, max; char *upper; if (!s) { fputs(PROG ": error: empty interval description string\n", stderr); return 1; } min = atoi(s); if (min == -1) { i->min = i->max = min; return 0; } if (min < 1) { fputs(PROG ": error: lower interval bound cannot be less than 1\n", stderr); return 1; } upper = strchr(s, '-'); if (!upper) { i->min = i->max = min; return 0; } if (upper == s) { fputs(PROG ": error: lower interval bound cannot be omitted\n", stderr); return 1; } max = atoi(++upper); if (max < min) { fputs(PROG ": error: upper bound less than lower bound\n", stderr); return 1; } i->min = min; i->max = max; return 0; } /*** main() ***/ int main(int argc, char **argv) { Imlib_Image image=NULL; /* an image handle */ Imlib_Image new_image=NULL; /* a temporary image handle */ Imlib_Image debug_image=NULL; /* DEBUG */ Imlib_Load_Error load_error=0; /* save Imlib2 error code on image I/O*/ char *imgfile=NULL; /* filename of image file */ int use_tmpfile=0; /* flag to know if temporary image file is used */ int i, j, d; /* iteration variables */ size_t cur_digit_mem, new_digit_mem; /* for overflow checks */ int unknown_digit=0; /* was one of the 6 found digits an unknown one? */ int need_pixels = NEED_PIXELS; /* pixels needed to set segment in scanline */ int min_segment = MIN_SEGMENT; /* minimum pixels needed for a segment */ dimensions_struct min_char_dims; /* minimum character dimensions (W x H) */ int potential_digits; /* number of potential digits after segmentation */ interval_struct expected_digits; /* expect number of digits is inside this */ int number_of_digits; /* number of digits found and accepted */ int ignore_pixels = IGNORE_PIXELS; /* pixels to ignore when checking column */ int one_ratio = ONE_RATIO; /* height/width > one_ratio => digit 'one' */ int minus_ratio = MINUS_RATIO; /* height/width > minus_ratio => char 'minus'*/ int dec_h_ratio = DEC_H_RATIO; /* max_dig_h/h > dec_h_ratio => possibly '.' */ int dec_w_ratio = DEC_W_RATIO; /* max_dig_w/w > dec_w_ratio => possibly '.' */ double spc_fac = SPC_FAC; /* add spaces if digit distance > spc_fac*min_dst */ double thresh=THRESHOLD; /* border between light and dark */ int offset; /* offset for shear */ double theta; /* rotation angle */ char *output_file=NULL; /* write processed image to file */ char *output_fmt=NULL; /* use this format */ char *debug_image_file=NULL; /* ...to this file */ unsigned int flags=0; /* set by options, see #defines in .h file */ luminance_t lt=DEFAULT_LUM_FORMULA; /* luminance function */ charset_t charset=DEFAULT_CHARSET; /* character set */ int w, h, lum; /* width, height, pixel luminance */ int col=UNKNOWN; /* is column dark or light? */ int row=UNKNOWN; /* is row dark or light? */ int dig_w; /* width of digit part of image */ int dig_h; /* height of digit part of image */ int max_dig_h=0, max_dig_w=0; /* maximum height & width of digits found */ int widest_dig_is_one=0; /* set to one if the widest digit is a one */ Imlib_Color color; /* Imlib2 RGBA color structure */ /* state of search */ int state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; digit_struct *digits=NULL; /* position of digits in image */ int found_pixels=0; /* how many pixels are already found */ color_struct d_color = {0, 0, 0, 0}; /* drawing color */ /* initialize structures */ min_char_dims.w = MIN_CHAR_W; min_char_dims.h = MIN_CHAR_H; expected_digits.min = expected_digits.max = NUMBER_OF_DIGITS; /* if we provided no arguments to the program exit */ if (argc < 2) { usage(PROG, stderr); exit(99); } /* parse command line */ while (1) { int option_index = 0; int c; static struct option long_options[] = { {"help", 0, 0, 'h'}, /* print help */ {"version", 0, 0, 'V'}, /* show version */ {"threshold", 1, 0, 't'}, /* set threshold (instead of THRESHOLD) */ {"verbose", 0, 0, 'v'}, /* talk about programm execution */ {"absolute-threshold", 0, 0, 'a'}, /* use treshold value as provided */ {"iter-threshold", 0, 0, 'T'}, /* use treshold value as provided */ {"number-pixels", 1, 0, 'n'}, /* pixels needed to regard segment as set */ {"min-segment", 1, 0, 'N'}, /* minimum pixels needed for a segment */ {"min-char-dims", 1, 0, 'M'}, /* minimum character (digit) dimensions */ {"ignore-pixels", 1, 0, 'i'}, /* pixels ignored when searching digits */ {"number-digits", 1, 0, 'd'}, /* number of digits in image */ {"one-ratio", 1, 0, 'r'}, /* height/width threshold to recognize a one */ {"minus-ratio", 1, 0, 'm'}, /* w/h threshold to recognize a minus sign */ {"output-image", 1, 0, 'o'}, /* write processed image to given file */ {"output-format", 1, 0, 'O'}, /* format of output image */ {"debug-image", 2, 0, 'D'}, /* write a debug image */ {"process-only", 0, 0, 'p'}, /* image processing only */ {"debug-output", 0, 0, 'P'}, /* print debug output? */ {"foreground", 1, 0, 'f'}, /* set foreground color */ {"background", 1, 0, 'b'}, /* set background color */ {"print-info", 0, 0, 'I'}, /* print image info */ {"adjust-gray", 0, 0, 'g'}, /* use T1 and T2 as perecntages of used vals*/ {"luminance", 1, 0, 'l'}, /* luminance formula */ {"ascii-art-segments", 0, 0, 'S'}, /* print found segments in ASCII art */ {"print-as-hex", 0, 0, 'X'}, /* change output format to hex */ {"omit-decimal-point", 0, 0, 'C'}, /* omit decimal points from output */ {"charset", 1, 0, 'c'}, /* select character set of display */ {"dec-h-ratio", 1, 0, 'H'}, /* height ratio for decimal point detection */ {"dec-w-ratio", 1, 0, 'W'}, /* width ratio for decimal point detection */ {"print-spaces", 0, 0, 's'}, /* print spaces between distant digits */ {"space-factor", 1, 0, 'A'}, /* relative distance to add spaces */ {"space-average", 0, 0, 'G'}, /* avg instead of min dst for spaces */ {"adapt-after-crop", 0, 0, 'F'}, /* don't adapt threshold before crop */ {0, 0, 0, 0} /* terminate long options */ }; c = getopt_long (argc, argv, "hVt:vaTn:N:i:d:r:m:M:o:O:D::pPf:b:Igl:SXCc:H:W:sA:GF", long_options, &option_index); if (c == -1) break; /* leaves while (1) loop */ switch (c) { case 'h': usage(PROG,stdout); exit (42); break; case 'V': print_version(stdout); exit (42); break; case 'v': flags |= VERBOSE; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "flags & VERBOSE=%d\n", flags & VERBOSE); } break; case 't': if(optarg) { thresh = atof(optarg); if(flags & DEBUG_OUTPUT) { fprintf(stderr, "thresh = %f (default: %f)\n", thresh, THRESHOLD); } if(thresh < 0.0 || 100.0 < thresh) { thresh = THRESHOLD; if(flags & VERBOSE) { fprintf(stderr, "ignoring --treshold=%s\n", optarg); } } if(flags & DEBUG_OUTPUT) { fprintf(stderr, "thresh = %f (default: %f)\n", thresh, THRESHOLD); } } break; case 'a': flags |= ABSOLUTE_THRESHOLD; break; case 'T': flags |= DO_ITERATIVE_THRESHOLD; break; case 'n': if(optarg) { need_pixels = atoi(optarg); if(need_pixels < 1) { fprintf(stderr, PROG ": warning: ignoring --number-pixels=%s\n", optarg); need_pixels = NEED_PIXELS; } if(flags & DEBUG_OUTPUT) { fprintf(stderr, "need_pixels = %d\n", need_pixels); } } break; case 'N': if(optarg) { min_segment = atoi(optarg); if(min_segment < 1) { fprintf(stderr, PROG ": warning: ignoring --min-segment=%s\n", optarg); min_segment = MIN_SEGMENT; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "min_segment = %d\n", min_segment); } } else { need_pixels = min_segment; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "min_segment = need_pixels = %d\n", min_segment); } } } break; case 'M': if(optarg) { int ret; ret = parse_width_height(optarg, &min_char_dims); if (ret) { fprintf(stderr, PROG ": warning: ignoring --min-char-dims=%s\n", optarg); } if(flags & DEBUG_OUTPUT) { fprintf(stderr, "min_char_dims = %dx%d\n", min_char_dims.w, min_char_dims.h); } } break; case 'i': if(optarg) { ignore_pixels = atoi(optarg); if(ignore_pixels < 0) { fprintf(stderr, PROG ": warning: ignoring --ignore-pixels=%s\n", optarg); ignore_pixels = IGNORE_PIXELS; } } break; case 'd': if(optarg) { int ret; ret = parse_interval(optarg, &expected_digits); if(ret) { fprintf(stderr, PROG ": warning: ignoring --number-digits=%s\n", optarg); expected_digits.min = expected_digits.max = NUMBER_OF_DIGITS; } if (flags & DEBUG_OUTPUT) { fprintf(stderr, "expected_digits.min = %d\n", expected_digits.min); fprintf(stderr, "expected_digits.max = %d\n", expected_digits.max); } } break; case 'r': if(optarg) { one_ratio = atoi(optarg); if(one_ratio < 2) { fprintf(stderr, PROG ": warning: ignoring --one-ratio=%s\n",optarg); one_ratio = ONE_RATIO; } } break; case 'm': if(optarg) { minus_ratio = atoi(optarg); if(minus_ratio < 1) { fprintf(stderr, PROG ": warning: ignoring --minus-ratio=%s\n", optarg); minus_ratio = MINUS_RATIO; } } break; case 'o': if(optarg) { output_file = strdup(optarg); } break; case 'O': if(optarg) { output_fmt = strdup(optarg); } break; case 'D': flags |= USE_DEBUG_IMAGE; if(optarg) { debug_image_file = strdup(optarg); } else { debug_image_file = strdup(DEBUG_IMAGE_NAME); } break; case 'p': flags |= PROCESS_ONLY; break; case 'P': flags |= (VERBOSE | DEBUG_OUTPUT); break; case 'f': if(optarg) { if(strcasecmp(optarg, "black") == 0) { set_fg_color(SSOCR_BLACK); set_bg_color(SSOCR_WHITE); } else if(strcasecmp(optarg, "white") == 0) { set_fg_color(SSOCR_WHITE); set_bg_color(SSOCR_BLACK); } else { fprintf(stderr, "%s: error: unknown foreground color %s," " color must be black or white\n", PROG, optarg); exit(99); } } break; case 'b': if(optarg) { if(strcasecmp(optarg, "black") == 0) { set_bg_color(SSOCR_BLACK); set_fg_color(SSOCR_WHITE); } else if(strcasecmp(optarg, "white") == 0) { set_bg_color(SSOCR_WHITE); set_fg_color(SSOCR_BLACK); } else { fprintf(stderr, "%s: error: unknown background color %s," " color must be black or white\n", PROG, optarg); exit(99); } } break; case 'I': flags |= PRINT_INFO; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "flags & PRINT_INFO=%d\n", flags & PRINT_INFO); } break; case 'g': flags |= ADJUST_GRAY; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "flags & ADJUST_GRAY=%d\n", flags & ADJUST_GRAY); } break; case 'l': if(optarg) { lt = parse_lum(optarg); if(lt == LUM_PARSE_ERROR) { fprintf(stderr, PROG ": warning: ignoring unknown luminance formula '%s'\n", optarg); lt = DEFAULT_LUM_FORMULA; } } break; case 'S': flags |= ASCII_ART_SEGMENTS; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "flags & ASCII_ART_SEGMENTS=%d\n", flags & ASCII_ART_SEGMENTS); } break; case 'X': flags |= PRINT_AS_HEX; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "flags & PRINT_AS_HEX=%d\n", flags & PRINT_AS_HEX); } break; case 'C': flags |= OMIT_DECIMAL; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "flags & OMIT_DECIMAL=%d\n", flags & OMIT_DECIMAL); } break; case 'c': if(optarg) { charset = parse_charset(optarg); if(charset == CS_PARSE_ERROR) { fprintf(stderr, PROG ": warning: ignoring unknown charset '%s'\n", optarg); charset = DEFAULT_CHARSET; } } break; case 'H': if(optarg) { dec_h_ratio = atoi(optarg); if(dec_h_ratio < 2) { fprintf(stderr, PROG ": warning: ignoring --dec-h-ratio=%s\n", optarg); dec_h_ratio = DEC_H_RATIO; } } break; case 'W': if(optarg) { dec_w_ratio = atoi(optarg); if(dec_w_ratio < 1) { fprintf(stderr, PROG ": warning: ignoring --dec-w-ratio=%s\n", optarg); dec_w_ratio = DEC_W_RATIO; } } break; case 's': flags |= PRINT_SPACES; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "flags & PRINT_SPACES=%d\n", flags & PRINT_SPACES); } break; case 'A': if(optarg) { spc_fac = atof(optarg); if(spc_fac < 1.0) { spc_fac = SPC_FAC; if(flags & (VERBOSE | DEBUG_OUTPUT)) { fprintf(stderr, "ignoring --space-factor=%s\n", optarg); } } if(flags & DEBUG_OUTPUT) { fprintf(stderr, "spc_fac = %f (default: %f)\n", spc_fac, SPC_FAC); } } break; case 'G': flags |= SPC_USE_AVG_DST; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "flags & SPC_USE_AVG_DST=%d\n", flags & SPC_USE_AVG_DST); } break; case 'F': flags |= ADAPT_AFTER_CROP; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "flags & ADAPT_AFTER_CROP=%d\n", flags & ADAPT_AFTER_CROP); } break; case '?': /* missing argument or character not in optstring */ short_usage(PROG,stderr); exit (2); break; default: /* this should not be reached */ if((c>31) && (c<127)) { fprintf (stderr, "%s: error: getopt returned unhandled character %c" " (code %X)\n", PROG, c, c); } else { fprintf (stderr, "%s: error: getopt returned unhandled character code" " %X\n", PROG, c); } short_usage(PROG, stderr); exit(99); } } if((flags & ABSOLUTE_THRESHOLD) && (flags & DO_ITERATIVE_THRESHOLD)) fprintf(stderr, "%s: warning: -T has no effect due to -a\n", PROG); if(flags & DEBUG_OUTPUT) { fprintf(stderr, "================================================================================\n"); fprintf(stderr, "VERSION=%s\n", VERSION); fprintf(stderr, "flags & VERBOSE=%d\nthresh=%f\n", flags & VERBOSE, thresh); fprintf(stderr, "flags & PRINT_INFO=%d\nflags & ADJUST_GRAY=%d\n", flags & PRINT_INFO, flags & ADJUST_GRAY); fprintf(stderr, "flags & ABSOLUTE_THRESHOLD=%d\n",flags&ABSOLUTE_THRESHOLD); fprintf(stderr, "flags & DO_ITERATIVE_THRESHOLD=%d\n", flags & DO_ITERATIVE_THRESHOLD); fprintf(stderr, "flags & USE_DEBUG_IMAGE=%d\n", flags & USE_DEBUG_IMAGE); fprintf(stderr, "flags & DEBUG_OUTPUT=%d\n", flags & DEBUG_OUTPUT); fprintf(stderr, "flags & PROCESS_ONLY=%d\n", flags & PROCESS_ONLY); fprintf(stderr, "flags & ASCII_ART_SEGMENTS=%d\n", flags & ASCII_ART_SEGMENTS); fprintf(stderr, "flags & PRINT_AS_HEX=%d\n", flags & PRINT_AS_HEX); fprintf(stderr, "flags & OMIT_DECIMAL=%d\n", flags & OMIT_DECIMAL); fprintf(stderr, "flags & PRINT_SPACES=%d\n", flags & PRINT_SPACES); fprintf(stderr, "flags & SPC_USE_AVG_DST=%d\n", flags & SPC_USE_AVG_DST); fprintf(stderr, "flags & ADAPT_AFTER_CROP=%d\n", flags & ADAPT_AFTER_CROP); fprintf(stderr, "need_pixels = %d\n", need_pixels); fprintf(stderr, "min_segment = %d\n", min_segment); fprintf(stderr, "min_char_dims = %dx%d\n",min_char_dims.w,min_char_dims.h); fprintf(stderr, "ignore_pixels = %d\n", ignore_pixels); fprintf(stderr, "expected_digits.min = %d\n", expected_digits.min); fprintf(stderr, "expected_digits.max = %d\n", expected_digits.max); fprintf(stderr, "foreground = %d (%s)\n", ssocr_foreground, (ssocr_foreground == SSOCR_BLACK) ? "black" : "white"); fprintf(stderr, "background = %d (%s)\n", ssocr_background, (ssocr_background == SSOCR_BLACK) ? "black" : "white"); fprintf(stderr, "luminance = "); print_lum_key(lt, stderr); fprintf(stderr, "\n"); fprintf(stderr, "charset = "); print_cs_key(charset, stderr); fprintf(stderr, "\n"); fprintf(stderr, "height/width threshold for one = %d\n", one_ratio); fprintf(stderr, "width/height threshold for minus = %d\n", minus_ratio); fprintf(stderr, "max_dig_h/h threshold for decimal = %d\n", dec_h_ratio); fprintf(stderr, "max_dig_w/w threshold for decimal = %d\n", dec_w_ratio); fprintf(stderr, "distance factor for adding spaces = %.2f\n", spc_fac); fprintf(stderr, "optind=%d argc=%d\n", optind, argc); fprintf(stderr, "================================================================================\n"); } /* if no argument left exit the program */ if(optind >= argc) { fprintf(stderr, "%s: error: no image filename given\n", PROG); short_usage(PROG, stderr); exit(99); } if(flags & DEBUG_OUTPUT) { fprintf(stderr, "argv[argc-1]=%s used as image file name\n", argv[argc-1]); } /* load the image */ imgfile = argv[argc-1]; if(strcmp("-", imgfile) == 0) /* read image from stdin? */ { if(flags & VERBOSE) fprintf(stderr, "using temporary file to hold data from stdin\n"); use_tmpfile = 1; imgfile = tmp_imgfile(flags); } if(flags & VERBOSE) { fprintf(stderr, "loading image %s\n", imgfile); } image = imlib_load_image_with_error_return(imgfile, &load_error); if(use_tmpfile) { if(flags & VERBOSE) fprintf(stderr, "removing temporary image file %s\n", imgfile); unlink(imgfile); free(imgfile); imgfile = argv[argc-1]; } if(!image) { fprintf(stderr, "%s: error: could not load image %s\n", PROG, imgfile); report_imlib_error(load_error); exit(99); } /* set the image we loaded as the current context image to work on */ imlib_context_set_image(image); /* get image parameters */ w = imlib_image_get_width(); h = imlib_image_get_height(); if((flags & DEBUG_OUTPUT) || (flags & PRINT_INFO)) { fprintf(stderr, "image width: %d\nimage height: %d\n",w,h); } /* get minimum and maximum "value" values */ if((flags & DEBUG_OUTPUT) || (flags & PRINT_INFO)) { double min, max; get_minmaxval(&image, lt, &min, &max); fprintf(stderr, "%.2f <= lum <= %.2f (lum should be in [0,255])\n", min, max); } /* process commands */ if(flags & VERBOSE) /* then print found commands */ { if(optind >= argc-1) { fprintf(stderr, "no commands given, using image %s unmodified\n", imgfile); } else { fprintf(stderr, "got commands"); for(i=optind; i0) && (i+10) && (i+10) && (i+10) && (i+10) && (i+1 ignore_pixels) { /* 1 not ignored dark pixel darkens the whole column */ col = (ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT; } } else if(col == UNKNOWN) /* light */ { col = (ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK; } } /* save digit position and draw partition line for DEBUG */ if((state == ((ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT)) && (col == ((ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT))) { /* beginning of digit */ if (flags & DEBUG_OUTPUT) { fprintf(stderr, " start of potential digit %d in image column %d\n", d, i); } digits[d].x1 = i; digits[d].y1 = 0; if(flags & USE_DEBUG_IMAGE) { imlib_context_set_image(debug_image); imlib_context_set_color(255,0,0,255);/* red line for start of digit */ imlib_image_draw_line(i,0,i,h-1,0); imlib_context_set_image(image); } state = (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK; } else if((state == ((ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK)) && (col == ((ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK))){ /* end of digit */ if (flags & DEBUG_OUTPUT) { fprintf(stderr, " end of potential digit %d in image column %d\n",d,i); } digits[d].x2 = i; digits[d].y2 = h-1; if((d >= INT_MAX - 1) || (d < 0)) { fputs(PROG ": error: too many potential digits (integer overflow)\n", stderr); exit(99); } d++; if(flags & USE_DEBUG_IMAGE) { imlib_context_set_image(debug_image); imlib_context_set_color(0,0,255,255); /* blue line for end of digit */ imlib_image_draw_line(i,0,i,h-1,0); imlib_context_set_image(image); } /* add memory for another digit */ cur_digit_mem = d * sizeof(digit_struct); new_digit_mem = (d+1) * sizeof(digit_struct); if(new_digit_mem <= cur_digit_mem) { fputs(PROG ": error: size_t overflow (memory for digits)\n", stderr); exit(99); } if(!(digits = realloc(digits, new_digit_mem))) { perror(PROG ": digits = realloc()"); exit(99); } /* initialize additional memory */ memset(&digits[d], 0, sizeof(digit_struct)); state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; } } /* after the loop above the program should be in state FIND_DARK, * i.e. after the last digit some light was found * if it is still searching for light then end the digit at the border of the * image */ if(state == (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK) { if (flags & DEBUG_OUTPUT) { fprintf(stderr, " end of potential digit %d in image column %d\n",d,w-1); } digits[d].x2 = w-1; digits[d].y2 = h-1; d++; state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; } /* horizontal partitioning has found "d" potential characters / digits */ potential_digits = d; if(flags & DEBUG_OUTPUT) { fprintf(stderr, "horizontal partitioning found %d digit(s)\n", potential_digits); } /* find upper and lower boundaries of every digit */ if (flags & DEBUG_OUTPUT) { fputs("looking for upper and lower digit boundaries\n", stderr); } for(d=0; d ignore_pixels) { /* 1 pixels darken row */ row = (ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT; } } else if(row == UNKNOWN) { row = (ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK; } } /* save position of digit and draw partition line for DEBUG */ if((state == ((ssocr_foreground == SSOCR_BLACK)?FIND_DARK:FIND_LIGHT)) && (row == ((ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT))) { if(found_top) /* then we are searching for the bottom */ { digits[d].y2 = j; state = (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK; if(flags & USE_DEBUG_IMAGE) { imlib_context_set_image(debug_image); imlib_context_set_color(0,255,0,255); /* green line */ imlib_image_draw_line(digits[d].x1,digits[d].y2, digits[d].x2,digits[d].y2,0); imlib_context_set_image(image); } } else /* found the top line */ { digits[d].y1 = j; found_top = 1; state = (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK; if(flags & USE_DEBUG_IMAGE) { imlib_context_set_image(debug_image); imlib_context_set_color(0,255,0,255); /* green line */ imlib_image_draw_line(digits[d].x1,digits[d].y1, digits[d].x2,digits[d].y1,0); imlib_context_set_image(image); } } } else if((state == ((ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK)) && (row == ((ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK))) { /* found_top has to be 1 because otherwise we were still looking for * dark */ digits[d].y2 = j; state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; if(flags & USE_DEBUG_IMAGE) { imlib_context_set_image(debug_image); imlib_context_set_color(0,255,0,255); /* green line */ imlib_image_draw_line(digits[d].x1,digits[d].y2, digits[d].x2,digits[d].y2,0); imlib_context_set_image(image); } } } /* if we are still looking for light, use the bottom */ if(state == ((ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK)){ digits[d].y2 = h-1; state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; if(flags & USE_DEBUG_IMAGE) { imlib_context_set_image(debug_image); imlib_context_set_color(0,255,0,255); /* green line */ imlib_image_draw_line(digits[d].x1,digits[d].y2, digits[d].x2,digits[d].y2,0); imlib_context_set_image(image); } } } if (flags & DEBUG_OUTPUT) { fprintf(stderr, "image segmentation found %d potential digits\n", potential_digits); } /* image has been segmented into potential digits, ignore too small ones */ if (min_char_dims.w > 1 || min_char_dims.h > 1) { int digit_count = 0, pos; digit_struct *tmp; if (flags & DEBUG_OUTPUT) { fputs("dropping too small potential digits\n", stderr); } /* count sufficiently large digits */ for (d = 0; d < potential_digits; d++) { if (digits[d].x2 - digits[d].x1 >= min_char_dims.w && digits[d].y2 - digits[d].y1 >= min_char_dims.h) { if (flags & DEBUG_OUTPUT) { fprintf(stderr, " keeping sufficiently large digit %d\n", d); } digit_count += 1; } else if (flags & DEBUG_OUTPUT) { fprintf(stderr, " dropping too small potential digit %d\n", d); } } if (flags & DEBUG_OUTPUT) { fprintf(stderr, "keeping %d of %d potential digits\n", digit_count, potential_digits); } /* at least one digit is required */ if (digit_count < 1) { fputs(PROG ": error: no sufficiently large digits found\n", stderr); exit(1); } /* ensure we do not try to keep more digits than we have found */ if(digit_count > potential_digits) { fprintf(stderr, PROG ": error: trying to keep more digits (%d) than found (%d)\n", digit_count, potential_digits); exit(99); } /* if potential digits are discarded, copy remaining ones to new memory */ if(digit_count < potential_digits) { /* allocate memory for sufficiently large digits we want to keep */ if(!(tmp = calloc(digit_count, sizeof(digit_struct)))) { perror(PROG ": tmp = calloc()"); exit(99); } /* keep only sufficiently large digits */ pos = 0; for (d = 0; d < potential_digits; d++) { if (digits[d].x2 - digits[d].x1 >= min_char_dims.w && digits[d].y2 - digits[d].y1 >= min_char_dims.h) { if (pos >= digit_count) { fputs(PROG ": error copying digit information", stderr); exit(99); } memcpy(tmp + pos, digits + d, sizeof(digit_struct)); pos++; } } free(digits); digits = tmp; potential_digits = digit_count; } } /* check if expected number of digits have been found */ if ((expected_digits.min > -1) && ((potential_digits < expected_digits.min) || (potential_digits > expected_digits.max))) { if (expected_digits.min != expected_digits.max) { fprintf(stderr, PROG ": expected between %d and %d digits, but found %d\n", expected_digits.min, expected_digits.max, potential_digits); } else { fprintf(stderr, PROG ": expected %d digit%s, but found %d\n", expected_digits.min, expected_digits.min > 1 ? "s" : "", potential_digits); } imlib_free_image_and_decache(); if(flags & USE_DEBUG_IMAGE) { save_image("debug", debug_image, output_fmt,debug_image_file,flags); imlib_context_set_image(debug_image); imlib_free_image_and_decache(); } exit(1); } /* continue to work with the accepted number of characters / digits */ number_of_digits = potential_digits; if (flags & DEBUG_OUTPUT) { fprintf(stderr, "image segmentation found %d digits\n", number_of_digits); } /* draw rectangles around accepted digits */ if(flags & USE_DEBUG_IMAGE) { imlib_context_set_image(debug_image); imlib_context_set_color(128,128,128,255); /* gray line */ for(d=0; d (%d,%d), width: %d (%5.2f%%) " "height: %d (%5.2f%%)\n", d, digits[d].x1, digits[d].y1, digits[d].x2, digits[d].y2, digits[d].x2 - digits[d].x1, ((digits[d].x2 - digits[d].x1) * 100.0) / dig_w, digits[d].y2 - digits[d].y1, ((digits[d].y2 - digits[d].y1) * 100.0) / dig_h ); fprintf(stderr, " height/width (int): "); if(digits[d].x1 == digits[d].x2) { fprintf(stderr, "NaN, max_dig_w/width (int): NaN, "); } else { fprintf(stderr, "%d, max_dig_w/width (int): %d, ", (digits[d].y2-digits[d].y1)/(digits[d].x2-digits[d].x1), max_dig_w / (digits[d].x2 - digits[d].x1) ); } fprintf(stderr, "max_dig_h/height (int): "); if(digits[d].y1 == digits[d].y2) { fprintf(stderr, "NaN\n"); } else { fprintf(stderr, "%d\n", max_dig_h / (digits[d].y2 - digits[d].y1) ); } } } /* at this point the digit 1 can be identified, because it is smaller than * the other digits */ if(flags & DEBUG_OUTPUT) fputs("looking for digit 1\n",stderr); for(d=0; d one_ratio){ if(flags & DEBUG_OUTPUT) { fprintf(stderr, " digit %d is a 1 (height/width = %d/%d = (int) %d)\n", d, digits[d].y2 - digits[d].y1, digits[d].x2 - digits[d].x1, (digits[d].y2 - digits[d].y1) / (digits[d].x2 - digits[d].x1)); } digits[d].digit = D_ONE; } } /* identify a decimal point (or thousands separator) by relative size */ if(flags & DEBUG_OUTPUT) fputs("looking for decimal points\n",stderr); for(d=0; d dec_h_ratio) && (max_dig_w / (digits[d].x2 - digits[d].x1) > dec_w_ratio)) { digits[d].digit = D_DECIMAL; if(flags & DEBUG_OUTPUT) fprintf(stderr, " digit %d is a decimal point\n", d); } } /* identify a minus sign */ if(flags & DEBUG_OUTPUT) fputs("looking for minus signs\n",stderr); for(d=0; d=minus_ratio)) { if(flags & DEBUG_OUTPUT) { fprintf(stderr, " digit %d is a minus (width/height = %d/%d = (int) %d)\n", d, digits[d].x2 - digits[d].x1, digits[d].y2 - digits[d].y1, (digits[d].x2 - digits[d].x1) / (digits[d].y2 - digits[d].y1)); } digits[d].digit = D_MINUS; } } /* If the widest digit is a one, decimal points may be of the same width, * and may thus not be detected. Now that minus signs have been selected, * if the widest digit still is a one (i.e., no minus signs), then decimal * separators may also be recognized by checking only the height, not the * width. */ if(flags & DEBUG_OUTPUT) fputs("checking for special case of a one as widest character\n",stderr); /* check if the widest digit is a one */ for(d=0; d= max_dig_w)) { widest_dig_is_one = 1; if(flags & DEBUG_OUTPUT) fputs(" widest digit is a one -> additional decimal point search\n", stderr); break; } } if(!widest_dig_is_one) { if(flags & DEBUG_OUTPUT) fputs(" widest digit is not a one, skipping extra decimal point search\n", stderr); } else { /* widest digit is a one, thus decimal seperators may have been missed: * identify a decimal point (or thousands separator) by relative height */ if(flags & DEBUG_OUTPUT) fputs("looking for decimal points again\n",stderr); for(d=0; d dec_h_ratio)) { digits[d].digit = D_DECIMAL; if(flags & DEBUG_OUTPUT) fprintf(stderr, " digit %d is a decimal point\n", d); } } } /* now the digits are located and they have to be identified */ if(flags & DEBUG_OUTPUT) fputs("starting scanline based recognition for remaining digits\n", stderr); /* iterate over digits */ for(d=0; d= need_pixels) { digits[d].digit |= HORIZ_UP; /* add upper segment */ } d_color.G = d_color.A = 255; d_color.R = d_color.B = 0; found_pixels = scanline(&debug_image, middle, digits[d].y1 + d_height/3, d_height/3, VERTICAL, d_color, thresh, lt, flags); if(found_pixels >= need_pixels) { digits[d].digit |= HORIZ_MID; /* add middle segment */ } d_color.B = d_color.A = 255; d_color.R = d_color.G = 0; found_pixels = scanline(&debug_image, middle, digits[d].y1 + 2*d_height/3, d_height/3, VERTICAL, d_color, thresh, lt, flags); if(found_pixels >= need_pixels) { digits[d].digit |= HORIZ_DOWN; /* add lower segment */ } /* check upper vertical segments (horizontal scan, y == quarter) */ d_color.R = d_color.A = 255; d_color.G = d_color.B = 0; found_pixels = scanline(&debug_image, digits[d].x1, quarter, (digits[d].x2 - digits[d].x1) / 2, HORIZONTAL, d_color, thresh, lt, flags); if (found_pixels >= need_pixels) { digits[d].digit |= VERT_LEFT_UP; /* add upper left segment */ } d_color.G = d_color.A = 255; d_color.R = d_color.B = 0; found_pixels = scanline(&debug_image, (digits[d].x1 + digits[d].x2) / 2 + 1, quarter, (digits[d].x2 - digits[d].x1) / 2 - 1, HORIZONTAL, d_color, thresh, lt, flags); if (found_pixels >= need_pixels) { digits[d].digit |= VERT_RIGHT_UP; /* add upper right segment */ } /* check lower vertical segments (horizontal scan, y == three_quarters) */ d_color.R = d_color.A = 255; d_color.G = d_color.B = 0; found_pixels = scanline(&debug_image, digits[d].x1, three_quarters, (digits[d].x2 - digits[d].x1) / 2, HORIZONTAL, d_color, thresh, lt, flags); if (found_pixels >= need_pixels) { digits[d].digit |= VERT_LEFT_DOWN; /* add lower left segment */ } d_color.G = d_color.A = 255; d_color.R = d_color.B = 0; found_pixels = scanline(&debug_image, (digits[d].x1 + digits[d].x2) / 2 + 1, three_quarters, (digits[d].x2-digits[d].x1)/2 - 1, HORIZONTAL, d_color, thresh, lt, flags); if (found_pixels >= need_pixels) { digits[d].digit |= VERT_RIGHT_DOWN; /* add lower right segment */ } } } /* check spacing of digits when --print-spaces is given and there are more * more than two digits */ if ((flags & PRINT_SPACES) && (number_of_digits > 2)) { int min_dst, avg_dst, dst_sum, cur_dst, base_dst, num_spc; if (flags & DEBUG_OUTPUT) { fputs("looking for white space\n", stderr); } /* determine distance between digits */ min_dst = dst_sum = digits[1].x2 - digits[0].x2; if (flags & DEBUG_OUTPUT) { fprintf(stderr, " distance between digits 0 and 1 is %d\n", min_dst); } for (i = 2; i < number_of_digits; i++) { cur_dst = digits[i].x2 - digits[i-1].x2; if (flags & DEBUG_OUTPUT) { fprintf(stderr, " distance between digits %d and %d is %d\n", i-1, i, cur_dst); } if (cur_dst < min_dst) { min_dst = cur_dst; } dst_sum += cur_dst; } avg_dst = dst_sum / (number_of_digits - 1); base_dst = (flags & SPC_USE_AVG_DST) ? avg_dst : min_dst; if (base_dst < 1) { base_dst = 1; } if (flags & DEBUG_OUTPUT) { fprintf(stderr, " minimum digit distance: %d\n", min_dst); fprintf(stderr, " average digit distance: %d\n", avg_dst); fprintf(stderr, " adding spaces for distance greater than: %d\n", (int) (spc_fac * base_dst)); } /* determine number of spaces after each digit */ for (i = 0; i < (number_of_digits - 1); i++) { num_spc = (int) ((digits[i+1].x2 - digits[i].x2) / (spc_fac * base_dst)); if (num_spc > 0) { if (flags & DEBUG_OUTPUT) { fprintf(stderr, " adding %d space character(s) after digit %d\n", num_spc, i); } digits[i].spaces = num_spc; } } } /* print found segments as ASCII art if debug output is enabled * or ASCII art output is requested explicitely * example digits known by ssocr: * _ _ _ _ _ _ _ _ _ * | | | _| _| |_| |_ |_ | | | |_| |_| * |_| | |_ _| | _| |_| | | |_| _| */ if(flags & (DEBUG_OUTPUT | ASCII_ART_SEGMENTS)) { fputs("Display as seen by ssocr:\n", stderr); /* top row */ for(i=0; i 0) putchar(':'); printf("%02x", digits[i].digit); print_spaces(stdout, digits[i].spaces); } } else { init_charset(charset); for(i=0; i. */ /* Copyright (C) 2004-2025 Erik Auerswald */ #ifndef SSOCR2_H #define SSOCR2_H typedef struct { int x1,y1,x2,y2,digit,spaces; } digit_struct; typedef struct { int R, G, B, A; } color_struct; typedef struct { int w, h; } dimensions_struct; typedef struct { int min, max; } interval_struct; #endif /* SSOCR2_H */