![]() |
Bienvenue ! - Articles : Article -
|
News | Archives | FAQ | Communauté | Projets |
News Non Officielles | Le site | TiWiki | Liens |
Bienvenue sur Ti-Fr v3!
News
Officieuses
BTS en bâtiment...
Message personnel... Romain Liévin quitte la communauté TI - la suite TIGCC v0.95 moins pratique que GTC v0.90 ? sortie de ppglib Et 100000, ça fait un joli nombre, non ? Problème d'accès MERCI !
Anciennes news
Heartbleed
Avec un peu de retard InfinitEye, un projet de casque de réalité virtuelle De retour... Questions posées en messages privés. Bonne année ! Messages... Du nouveau sur Punix
Plus...
Articles
Programmation Tutorial : Programmation C [OLD] (205838) Tutorial : Programmation C [NEW] (120382) Tutorial sur Vertel3 (62225) Hardware
Dernières archives
Assembleur & C
Basic
nSpire & nSpire CAS
Production Clickpad Nspire infor (15/09/2012)
imgmanip pour Nspire (3/04/2012) imgdump Clickpad - Touchpad - CX (4/03/2012)
Meilleurs DL
La plus consultée
Plus...
Infos
|
Article TI [Programmation] Articles sur la programmation des TI68k Compression RLETutorial (en anglais) sur la programmation RLE en C par ThorVoici le tutorial sur la compression proposé par Thor Vous pouvez trouver l'archive complète ici ou directement ci dessous (liste des fichiers dans l'archive puis tutorial en lui meme)
This tutorial had been written by Cyril Mottier (orlin61@wanadoo.fr) and may be distributed by any other website for non-commercial use only. The author makes no representations or warranties about the suitability of the software and/or the data files, either express or implied. The author shall not be liable for any damages suffered as a result of using or distributing this software and/or data files. Obviously, you are free to re-use any part of the sourcecode in your products as long as you give credits including a reference to me : Cyril Mottier :-).
In this tutorial we'll see : Note: This tutorial is based on Ti68k programming so it assumes that you have bases in programming with TIGCC, especially in C using pointers, while-loops, for-loops and arrays. Nevertheless, the ideas are the most important because they can be used in every development platforms. To conclude, you need some different softwares to understand each part of this tutorial :
RLE compression is probably the easier compression algorithm there is. Unfortunately, when a compression algorithm is easy to use it is also not very efficient. RLE compression can be very efficient in some special case like black and white pictures (the goal of this tutorial) or text files which contain lots of spaces for indenting (like a C source). I'll try to explain you this property in this part. Everybody know the mp3 or jpeg compression. These technics extract all useless entities : for exemple mp3 extract all inaudible sounds giving a file sometimes 10 times smaller than the original file. Nevertheless, this mp3-file is 'impoverished' because you can't decompress the mp3-file to re-find the original file. This technic is very usefull when you want to compress some pictures (composed of lots of colors) or sounds. But it's impossible to use this compression way to reduce an executable file or a black and white picture (due to the fact that it's very easy to see the difference between two opposite colors). On our calculators all picture are black and white so we are 'obliged' to use a 'fast' and 'lossless' compression algorithm : the RLE algorithm. RLE means Run Lenght Encoding and it's also called Run Length Coding (RLC). This technic of compression is defined as a 'lossless' compression because the output file can be decompressed with no loss. When you execute a RLE algorithm on a file, you 'cut' all bytes which are repeated. It replaces sequences of the same data values within a file by a count number and a single value. I'll try to explain you this method by compressing the (senseless) word :
This 'word' is composed of 12 letters (or bytes because a letter is encoded on a single bytes with ASCII). RLE compression consist on changing all repeated bytes with an control character followed by the number of letters and finally the letter compressed. Suppose the control caracter '#', with this method the precedent word becomes :
This last word is composed of 9 bytes so you win 12-9=3 bytes. I'm okay that you don't find the compression amazing but the compression rate (ratio) is equal to ((12-9)*100)/12 = 25%. Imagine a word made up of 2000 bytes and compressed with a ratio of 25%, the final file is 2000*0.75 = 1500 so you win 500 bytes! As you can see, the ratio can be found with the followed function or routine :
The RLE compression suffer from many disadvantages : suppose the word and the control caracter '#' :
The compressed word becomes :
The compressed word contains 36 letters and the original word is composed of 12 bytes so the routine ratio (12,36) will returns -200%! So the algorithm is useless! To overcome this problem RLE compression is only used if there are sequences of four or more repeating characters because three characters are used to conduct RLE so coding one or two repeating characters would even lead to an increase in file size and it's contrary to our goal! Then we use to conduct this compression a control caracter coded on a bytes but if this caracter appears in the file it will 'crash' our routine because it will interpret this caracter as a control caracter and not a 'normal' caracter. So if the control character itself appears in the file then one extra character is coded. For exemple the word :
will become :
To conclude, the RLE algorithm needs, in special cases, some alternatives to problems which are very easy to overcome. We are going to see in the second part how decompressing a picture compressed with this esay-to-use RLE compression.
To start, we need a picture which will be compressed : for this tutorial, I have taken a picture representing a little animal : a cat in a famous cartoon. To begin with this problem, you need to learn many tips in preparing the picture: Firstly, IStudio can just compress the bitmap picture. So, you need to change a .jpg or .gif picture in .bmp. With Paint Shop Pro, open the picture and after you may save as a .bmp. Secondly, IStudio contains two option which can change the contrast and brightness. What a shame that these options are not very efficient! The solution consist on changing the contrast and brightness by clarifying the picture with Paint Shop Pro. Thirdly, you can see the result of a reduction of colors without IStudio. In Paint Shop Pro, open the picture, switch this one in grayscales and reduce the number of colors to 4. With this method you have an idee of the final project and it can be very usefull! Now it's high time to RLE_compress our picture. To explain easily the process, all steps are stored and showed in the following list :
Our picture in now compressed but the output file contains odd datas for the C-compiler. These datas need to be modified with a text editor. I will use ConText in this tutorial. Open ConText software and then open the Istudio file (normally this file has the extension .asm) by cliking 'File' and 'Open'. The file is now open : Click on 'Edit' and then on 'Replace' to replace '$' by '0x' and ' dc.b ' by ','. Note that the process is sometimes very long (it depends on file size). Finally change the string 'Layer1' by 'unsigned char name_of_picture1[]={' and 'Layer2' by 'unsigned char name_of_picture2[]={'. Don't forget to erase the first virgule and place a '}' at the end of a layer! Datas are now 'clean' and can be used in your programs.
If you watched attentively output datas of Istudio you can remark these datas contain lots of 0x91. In fact, Istudio use the byte 0x91 as special caracter. With this last information, it's easy to create an algorithm which can decompress RLE_compressed pictures. I've decided to split this part in two routine. The first routine will explain you how decompressing a RLE_compressed picture to a BITMAP structure (after you can use the basic function BitmapPut to display the result). The second routine will particular touch all ExtGraph's users because it shows you how decompressing a RLE_compressed picture to the specific format of the extraph functions : (Gray)SpriteX8_(AND|OR|XOR|MASK|BLIT). A) Bitmap version routine To understand easily our next routine, we need to know the structure of a BITMAP structure. In TIGCC documentation, this structure is defined like that :
With this definition, it's easy to see what sort of datas are compulsory. The process to obtain a BITMAP structure is first to define a new BITMAP structure and then to fill all variables with parameters passed in argument (some paramaters like 'size' need to be calculated first).To fill the field 'Data' we will use a pointer 'dest' which point to the 'Data' field of our new BITMAP structure. Finally, the pointer to our final BITMAP structure is returned. Very simple, isn't it ? Don't be afraid, just watch attentively the following code (again and again if necessary). An exemple is available to show how to display a decompressed picture : see rletobmp.c for more informations.
Note : The function 'RLE_Decompress_To_BITMAP()' may crash your calculator if the pointer 'scr' and arguments 'rows' and 'cols' are in contradiction. For exemple if you try to decompress a 64x40 picture pointed by 'ptr' with the following routine : RLE_Decompress_To_BITMAP (ptr, 72, 40)! B) Extgraph version routine In Extgraph documentation, (Gray)SpriteX8_(AND|OR|XOR) are defined like that :
'x' and 'y' are the coordinates of the upper left corner of the sprite. 'h' is the height of the sprite. 'sprite1' and 'sprite2' are some pointers to arrays of unsigned char. 'bytewidth' is the width of the picture in bytes and finally 'dest1' and 'dest2' are some pointers to the video planes (GetPlane(LIGHT_PLANE) for exemple). Note that blackj and white variant functions have the same syntax exept 'sprite2' and 'dest2'.The sprite data has to be organized like this (example for bytewidth == 3): line1/byte1, line1/byte2, line1/byte3, line2/byte1, line2/byte2, line2/byte3 etc. It's now esay to create our decompressing routine because these functions don't need to create a specific structure. We just have to decompress our compressed datas in an allocated block.The following code explain you the decompression technic. Read it attentively! An exemple is available to show how to display a decompressed picture : see rletoext.c for more informations.
Note : The function 'RLE_Decompress_To_ExtGraph()' may crash your calculator if the pointer 'scr' and argument 'size' are in contradiction. For exemple if you try to decompress a 64x40 (320 bytes) picture pointed by 'ptr' with the following routine : RLE_Decompress_To_ExtGraph (ptr, 340)!
It's the end of this tutorial. I hope that you have easily understand each parts of this tutorial! If you have some problems in understanding one part (ore more), or if your program to test this method in not a success and don't run correctly, feel free to mail me. Suggestions, bug reports, and similar are VERY welcome. You can reach me at : orlin61@wanadoo.fr, on this forum of this site : www.tigen.org and at my adress : Mottier Cyril test is often compulsory Don't hesitate to send me a postcard from your country. I prefer to receive A beautiful and well-written postcard than an e-mail rapidly and badly written :-). Cyril Mottier, France 21/08/04 Thor(2004-08-31 00:00:00) En cas de problème avec un article veuillez contacter DIRECTEMENT l'auteur. Merci. |
- Ti FR v3 - Ce site n'est pas le site officiel de texas instruments. En cas de problèmes techniques sur le site veuillez contacter l'administrateur. Merci de vos visites ! |