Fractal Compression ISAF

Embed Size (px)

Citation preview

  • 8/6/2019 Fractal Compression ISAF

    1/25

    Fractal Image Compression throughIterated Function SystemsA tutorial for the IFS Application Framework (IFSAF):a software for experiencing with Fractal Image Compression

    Last revision 0.0 September 1995

    WARNINGWORKS IN PROGRESS

    THIS DOCUMENT IS A PRE-RELEASE, SO IT IS INCOMPLETE AND, VERY LIKELY, IT CONTAINS ALOT OF ERRORS AND IMPERFECTIONS (NOT TO MENTION THE LACK OF FORMATTING AND

    RATIONAL ORGANIZATION).THIS IS AN UNDERGRADUATE STUDENTS WORK: BE WARNED OF INACCURACIES.

    Index - workmapAbout IFSAFAuthorsDisclaimsFeaturesInstallationRequirementsFile formatsA coding/decoding example+ How to read the STS file - only the c-decl by nowApproximation of shade blocks with polynomials

    The algorithm for gray-scale images- Calculating and quantizing alpha and beta - just as Prof. Fisher does.- True color image encoding- Using IFSAF- The classification of blocks- The tool for classifying these blocks, gradients etc., the two classifications compare tool+ Simulated annealing- Orthonormalization for beta of 2nd deg- Different metrics- Recursive encoding- The INI file- Error evaluation over blocks+ About the source code

    The color encoding function- The porting on Linux/UNIX: surely command-line, perhaps X.

    ["-" : Not yet written; "+" : To be completed.]

    About IFSAFThe IFSAF program was written by G.Pulcini, V.Verrando, R.Rossi and C.Meloni as final half-year work forthe 1995 course in microelectronics at "La Sapienza" University, Rome.

    The goal of this work was to explore some algorithms for fractal image compression through Iterated

    Functions Systems (IFS).Our results are: compression ratio up to 25:1 in 6 minutes, with good image quality.

  • 8/6/2019 Fractal Compression ISAF

    2/25

    IFSAF Theory and Applications 2

    The compression algorithm is based upon a paper of A.E.Jaquin1

    and on some extensions outlined thereby, between the others, Prof. Yuval Fisher

    2, whom we thank for addressing us to his paper SIGGRAPH '92

    course notes3.

    Even more info you can found on Prof. Fisher's book: Fractal Image Compression: Theory and Applicationto Digital Images ( Springer Verlag, New York, 1995 (URL :)).

    AuthorsGiovambattista Pulcini

    e-mail: [email protected]: http://www.webcom.com/~verrando/pulciniphone: +39-775-726038

    Valerio Verrandoe-mail: [email protected]: http://www.webcom.com/~verrandophone: +39-6-3243486

    Riccardo Rossiphone: +39-6-5811847

    Carlo Meloniphone: ?

    For any question, feel free to contact us by e-mail: we'll answer all the mail we get, just let us take sometime to answer, up to 2-3 weeks, in some cases, because of our student duties at the University.

    DisclaimsWe may have used some portions of copyrighted source code (certainly portions of Prof. Fisher and somelibraries of Borland C++ 3.0 compiler). We wrote this program at the only educational purpose, so any otheruse may be prohibited, including the reproduction/distribution of this software not for free.

    We think that this can be only distributed FREELY for the only EDUCATIONAL purpose to experimentfractal image encoding.That's why, also, we didn't make the complete source code available.We are not lawyer anyway, so this may be not so correct.

    WE ARE NOT RESPONSIBLE FOR ANY KIND OF DAMAGES THIS SOFTWARE MAY CAUSE TO YOU. IF YOU DON'T AGREE THIS,SIMPLY DON'T USE IT AND DELETE YOUR COPY OF THIS SOFTWARE.

    Just to put us in safe. :-)

    Features

    True color image compression by the means of a new approach. We do not make 3 separatecompressions.

    During the encoding, it shows graphically each step of the computation, while building the "collage".

    Gray level encoding with several different selectable algorithms.

    Different strategies for searching in the domain pool

    Exhaustive.

    Reduced by Local Sub Search.

    Reduced around neighbourhood of the range block.

    Reduced by Isometry prediction.1

    - Fractal Image Coding: a review. Proceedings of IEEE, vol. 81, n. 10, October 1993, 1451-14652

    - http://www.ucsd.edu/y/Fractals/ e-mail: [email protected]

    - ftp://ftp.ucsd.edu/y/Fractals/fractal_paper.ps.Z

  • 8/6/2019 Fractal Compression ISAF

    3/25

    IFSAF Theory and Applications 3

    We also considered the Prof. Fisher's block classification but, we didn't implemented yet. Simulated Annealing.

    Selectable dimensions of range blocks. (domain blocks are fixed to be 4 times the area of range blocks)

    Selectable number of bits for encoding massic transform parameters.

    Polynomials of the 2nd degree for skipping the search on flatten (shade) blocks.

    Polynomials of the 2nd degree used as the shift term in the massic transform, with an orthonormalizationscheme for computing the coefficients (does not work well, yet).

    Recursive encoding with quadtree partition, limited to 2 levels, by now.

    Several selectable metrics to evaluate differences between blocks. Both for encoding or only forreporting.

    Log of each processing step, you can find a lot of useful information. Warning, these files may get verylong.

    In the early stage of this project we implemented some methods to classify blocks, as in Jaquin article, toreduce the search space. While we dropped that way of encoding during the implementation of theprocedure, these tools are still available:

    Block classification based on block histogram:

    dynamic range evaluation (histogram); evaluation of 2 max peaks (twin peaks); projection onto a 9 dimensional vector subspace with a Frei and Chen's basis.

    The software makes available a tool for interactive view of these values.

    Also, you can compute 2 different classifications of blocks on the same image and then you can showeach class for each block of the image or the differences between the two.

    In the study of block orientations, for predicting the isometry, we developed an interactive tool that shows(for gray-scale images) some gradients for each block you click on.

    Notices:

    Several features can not be mixed together, even if the program allows to do that. This is because wemade the program for flexibility: it was intended to run with the program on one hand and the compiler onthe other hand. :-)So don't blame us if the program hangs or behaves strange.

    The program makes almost no check on what you are doing, eg. neither if you give it a color image whenit needs a gray-scale one.

    Even if it may look somewhat user-friendly, due to the windows interface, it's joking you! :-)It has nothing of user friendly, you are on your own for assuring consistency of what you are doing. That'sway you should have a deep knowledge of how fractal image compression works.

    There is, anyway, an easier way to get some results from it: you can select short menu and follow theprocedure outlined in the section: An encoding/decoding example.

    It only supports the windows DIB RGB encoded format, both in true-color (24bpp) and in gray-scale(8bpp). See the section Requirements for more info.

    It is well known by us that the program has a lot of bugs and inconsistencies.Some options in dialogs box are not available, even if they are not dimmed, and some other can be only

    changed editing the .INI file.See the section: Using IFSAF for more info on this subject.

  • 8/6/2019 Fractal Compression ISAF

    4/25

    IFSAF Theory and Applications 4

    RequirementsA 386/387 CPU (at least 486-66 for speed) with 8Mb of RAM, SVGA card capable of 15 or more bpp videomode (this is only for best viewing the results, since IFSAF doesn't handle dithering on 8bpp displays,anyway you may view them with another program as LView or PSP).Your machine should run Windows 3.10 or higher in Enhanced mode with a swap file of at least 15Mb andsome available space on the HD: say 2-3 Mb at least.No other program should be running while IFSAF is running. You should keep free as much physical RAMas you can to prevents your Windows to go into trashing. Some swap during decoding is OK.

    These are not absolute minimal requirements, but those for a reasonable working configuration for encodingimages 256x256 or 512x512, depending on options settings.

    IFSAF is not a well behaved windows program: during encoding it monopolizes the CPU anddoesn't release it until the encoding is complete, preventing other programs to run.There is no way to stop the encoding or decoding when started, besides you can kill the program pressingCtrl-Alt-Del once and then Return.

    The input image must be a .BMP windows file in true-color RGB format, not RLE compressed, whichis not supported. The dimensions of the image must be power of two or at least divisible by the size of rangeblocks: they must contain an integer number of range blocks.

    With parameters at extreme values, you may get some sqrt:DOMAIN errors or program may hang.

    Sometimes it's better to close the status window bebore you start the encoding or the decoding, sinceprogram may hang, in some cases.

    InstallationYou have to put the IFSAF program and all its relating files (unzipping them with pkunzip 2.04g) under thetop-level directory \IFS in any drive, otherwise IFSAF won't be able to find some files it needs.

    The images should be put in the same directory too. All output will be generated under this directory.

    File formats

    Input file:

    The only supported file format is the windows DIB RGB encoded format (.BMP) both for true colorimages at 24bpp and for gray-scale ones at 8bpp. Be warned that any other format or even subformat suchas the RLE encoded .BMP are not supported.For color images you have to use only 24bpp ones, 8bpp color mapped images will be seen as gray-scaleones.

    For gray-level images, we take the pixel color index as the level of gray, not the value index in thepalette. This should not cause problems to you, because most of programs, when converting to a gray-levelimage, do the same, assigning an "identity" map to the palette:index 0 is RGB 0,0,0, index 1 is RGB 1,1,1 and so on.

    Output files:

    IFSxxx.BMP : is the collage or decoded bitmap, as you save them.IFSxxx.IFS : is the file containing the transformations, it is in binary form.IFSxxx.STS : some accurate statistics in binary format that will be shown by the status window, there are

    also other statistical info that aren't currently shown. You can extract those with a simple Cprogram: see the section How to read the STS file.

    IFSxxx.RPT : this is a text file that is a log-report of the encoding, several useful information can be

    obtained in the header and footer of the file. The body of the file contains lines like this:

    R(0,0)

  • 8/6/2019 Fractal Compression ISAF

    5/25

    IFSAF Theory and Applications 5

    R(0)

  • 8/6/2019 Fractal Compression ISAF

    6/25

    IFSAF Theory and Applications 6

    Good luck and have fun. :-)

    IFS - Iterated Function SystemsHere we will briefly describe how fractal image compression through IFS is done.

    A picture is considered as a point in a complete metric space, where the metrics is the Hausdorff metric.So, we find a set of transformations that, when iterated and applied to an arbitrary starting point (anarbitrary picture), converges toward a point that depends only on the set of transformations, not on thestarting point. This point is called the attractor.

    The question is: given an arbitrary image, it is possible to find a set of transformations whose attractor isthe given image itself? The collage theorem answers yes, stating that: as far better we can cover (as in acollage) the original image with scaled, rotated and translated copies of the same image (or of its parts), asthe attractor will be closer to the original image.

    We build the collage image with the following algorithm:let we have a gray-scale image, we subdivide it in blocks (the range blocks) of - say - 8x8 pixels, then foreach of them, we search in the whole image, a block (the domain block) of - say - 16x16 pixels, whichreduced(to the 8x8 size), transformed by one of 8 isometriesand scaled in luminanceis the best match forthe original 8x8 range block.So, the original image is encoded in a set of transformations, (scaling, isometry application, luminancescaling and shifting). There are not pixel values in the encoded image, there are only the values of

    parameters involved in the transformation applied to each domain block to map the range block.

    The decoding process consists in the reconstruction of the attractor, well, an approximation of the trueattractor after a finite number of iterations (typically 6-8 should suffice).

    The range block size defaults to 8, but it can be changed both from Option/Block Settings or with theRangeBlockSize entry in the INI file.Even if there is an entry DomainBlockSize in the INI and a combobox in Option/Block Settings for changingthe Domain block size, it is actually hard-coded in the program to be 2 times the range block size.

    More in detail, the transformations applied are:scaling of the domain block to match the size of the range block (typical a reduction from 16x16 pixel to8x8 averaging the pixels in 2x2 blocks.

    one of the 8 isometries that led a square in a square, we use the one that minimizes the rms errorbetween range and transformed domain block.These two are often said "geometric transforms"

  • 8/6/2019 Fractal Compression ISAF

    7/25

    IFSAF Theory and Applications 7

    non homogeneous scaling of the luminance. We apply to each pixel of the partially transformed domainblock the following:

    r[i,j]=alpha*d[i,j]+betawhere r[i,j] indicated the resulting pixel of the partially transformed domain block whose pixels are d[i,j].The alpha parameters controls the contrast of the block and the beta controls the luminance.This is what is called massic transform.translation of the domain block to cover the range block.

    The search of the domain block.

    The simplest and slowest way of finding a matching block for the selected range block is to test eachdomain block. This is the exhaustive search.

    We can imagine that we move a 16x16 square window all around the original image, apply thetransformations and calculate the rms error, for each position of the window. When we have explored all theimage, we simply select the position (which identify a domain block) that had the least rms error.Since the domain blocks are contracted to half of their original size, we can move the window by a step of 2pixels, with almost no change for the domain block and halving the search space. From here: as larger thestep as smaller the search space and faster encoding. Too large steps, however led to too small searchspaces leading to poor image quality. That is the parameter Domain Step.

    So one may think: I can move with large steps, evaluating the rms error, when I get the minimum, I searchin the neighborhood with a smaller step.This is what the Local Sub Search (LSS) does. The parameters Overstep simply tells the program howmany Domain Step to skip in the search of the first minimum, after that the search is done with step Domain

    Step in an square of size Overstep * Domain Step pixels,as shown in the picture.

    Another way, probably better, is to search with finer step each time the rms error falls below a threshold (notimplemented in IFSAF).In order to reduce the time in the search, we can try another approach: we could search for matchingdomain blocks only in a square neighborhood of the original range. This is what we call Local Search. Thediameter in of this region is the Diameter parameter in the INI fi le.We can combine LSS with local search, of course.

    A completely different way to reduce the search space is to use the Simulated Annealing Algorithm. See thespecial section on this topic.

    For each domain block we should test all values of alpha, all values of beta, all isometries.This leads to asearch space which is simply too big.

    We can simply avoid the search for alpha and beta values, since there is a simple relation between the rms

    error and the values of the 2 parameters alpha, and beta. So expressing the rms error as a function of alphaand beta, we can simply calculate the values of alpha and beta for which the rms reach its minimum.This calculation consists in the inversion of a 2x2 matrix.

  • 8/6/2019 Fractal Compression ISAF

    8/25

    IFSAF Theory and Applications 8

    This matrix may be singular ? We don't know: only in few cases the program reported this event, but couldbe caused by round-off errors. In any case, never this prevented the program to find another block for whichthis matrix was not singular.

    For stripping out the isometries from the search space, the things are not so simple. We should be able todecide what isometry will cause the least rms, without trying it. The trial is exactly what we want to get rid of.

    For the human eye this may not be a difficult task, but for computer the things stand a little different. It is atypical problem of pattern matching, at the time, as far we know, there is no definitive solution to thisproblem.You may try to use fuzzy logic, neural networks, and several other interesting methods to do this, weencourage the use of these methods. By now we implemented a trivial classification scheme which led togood results.This is what we call Iso(metry) Prediction.We subdivided each block in 4 sub-blocks, we calculate the mean luminance of each block and sort these inincreasing order. So, each block, even range or domain ones, has a list and a sorted list of the luminance ofthe four sub-blocks. Comparing the two lists (the sorted one and the other one), for each block, we get thenumber of the permutation that transforms the unsorted list into the sorted one. This is called thelexicographic order of the permutation. It can be computed as

    perm# = inv(x1) 3! + inv(x2) 2! + inv(x3) 1! + inv(x4) 0!

    where inc(xi) is the number of inversions relating to the index in the i-th position.

    In the example above we have: 2*3!+1*2!+1*1!+0*0! = 15.

    Each block (range and domain) now has its own lexicographic order, so we can build a table of 24x24isometries which tell us what isometry to use for "best mapping" a range block with a given permutationnumber onto a domain block with another permutation number.

    So, what is wrong with that ?Well, the "best mapping" is not assured to be the least rms error, that's why:we say that a block best matches another with isometry i, if with that isometry the two most bright subblocksare led one over another, and possibly the other subblock in order of luminance are led one over another.So, this does not assure the rms to be minimum, since we are dealing with means we may undertake someinternal orientation of the blocks. The perfect overlapping of the blocks is not always possible, there arecases in which we can only impose the matching of the only brightest block and, in this case we will haveseveral isometries that leads to this, so what is the one to select? We get the first. Anyway this is an openproblem.A question remains: we should first apply the isometry and then calculating the alpha and beta. Is thiscorrect or may be that with an alpha and beta that don't minimize the rms and another isometry the rms

    error could be less than the previous one. We don't know.

    Another method both to reduce the search space and to find directly the isometry is the classification ofblocks by the means of some of their statistical moments. This is what Dr. Fisher does in his enc.c. (wedidn't implement this classification scheme).

    Another classification is based on the "typology" of the blocks, as suggested by Jaquin. We can classifyeach block as a shade block, a middle-range blockand an edge-block. So as dr. Jaquin says we couldapproximate the shade blocks with polynomials, no search for them is needed, we could map the middle-edge block (think of them as texture blocks) without checking the isometry (we assume them as somewhatorientation-invariant) and finally the full search is done for edge blocks.As we said, there are tools in IFSAF to help to find a classification algorithm for those, but the classificationdoesn't not work during encoding. We, besides, implemented the polynomial approximation of shade blocks.

    When Polynomials are active, each range block is approximated with a 2nd degree polynomial in x and y.If the approximation rms error is below a selectable threshold (ErrorThreshold parameter), we simplyencode the range block as the coefficients of the polynomial, otherwise we encode the block traditionally.That way the classification is somewhat implicit.

  • 8/6/2019 Fractal Compression ISAF

    9/25

    IFSAF Theory and Applications 9

    While using polynomials, assuming that all range blocks classified as shade will be approximated withpolynomials, we may get rid of shade blocks in the domain pool, that is what the option Strip-off shadesshould do (it actually doesn't).The quantization of the polynomials coefficients is a big deal. We should use as few bits as we can, in orderto keep high the compression ratio, but if we assign too few bits to them the approximation with real numberbecome looser and the projection theorem (which we use to calculate the coefficients of the polynomial)

    may loose it validity in a discrete space.On the other hand, with few bits the polynomials will approximate well fewer blocks, increasing thecompression time. Another question arises: how to allocate n bits on 6 coefficients so that the error ofquantization will be generally low? We proportionally allocated bits with the degree of the monomialassociated with the coefficient, imposing an equal error for each monomial.

    Approximation of shade blocks with 2nd degree polynomialsWe want to approximate the function f(i,j), defined over a square domain NxN, with a polynomial p(i,j) =

    x1+x2i+x3j+x4i2+x5ij+x6j2 with i,j {0,1,...,N}We impose that, in each pixel (i,j), p(i,j) = f(i,j) and so we have m=N2 equations with n=6 unknown

    quantities. The system, in general, has no solution "too many equations for few unknown quantities".So we will use approximated methods for solving the system.Let be:

    b f h N h N h mh= ( / , mod ), { ,..., }0

    a

    a i

    a j

    a i

    a ij

    a j

    h

    h i h N

    h j h N

    h i h N

    hi h N j h N

    h j h N

    1

    2

    3

    4

    2

    5

    6

    2

    1=

    =

    =

    =

    =

    =

    =

    =

    =

    =

    =

    =

    /

    mod

    /

    /mod

    mod

    we have:

    a x a x a x b

    a x a x a x b

    a x a x a x bm m m m

    11 1 12 2 16 6 1

    21 1 22 2 26 6 2

    1 1 2 2 6 6

    ...

    ...

    .......

    ...

    =

    =

    =

    with an error:

    e b a x a x a x

    e b a x a x a x

    e b a x a x a xm m m m m

    1 1 11 1 12 2 16 6

    2 2 21 1 22 2 26 6

    1 1 2 2 6 6

    = + + +

    = + + +

    = + + +

    ( ... )

    ( ... )

    .......

    ( ... )

    We solve the system with vectorial methods:

    in the space F=m we define the inner product:

    < >= >

    =

    x y, ,w x y wi i ii

    m

    i1

    0A b e

    k

    k

    mk m m

    a

    a

    b

    b

    e

    e

    k n=

    =

    =

    1 1 1

    1... ... ... { ,..., }

    that is:

    x x x x x x

    n n

    n n

    1 1 2 2

    1 1 2 2

    A A A be b A A A

    + + + =

    = + + +...

    ( ... )

    with a global error equal to:

  • 8/6/2019 Fractal Compression ISAF

    10/25

    IFSAF Theory and Applications 10

    E d x x x x x x

    E w e

    n n n n

    i ii

    m

    = + + + = + + +

    = =< >=

    =

    2

    1 1 2 2 1 1 2 2

    2

    1

    ( , ... ) ( ... )

    ,

    b A A A b A A A

    e e e

    The best approximation for a solution of the system is given by the orthogonal projection of the vector b

    onto the space U with a basis A1, ..., An.Letx b A A A* P( , ) * * ... *= = + + +U x x x

    n n1 1 2 2

    be the best approximation of b in U. This can be found solving the system:

    < > + < > + + < > =< >

    < > + < > + + < > =< >

    < > + < > + + < > =< >

    A A A A A A b A

    A A A A A A b A

    A A A A A A b A

    1 1 1 1 2 2 1 1

    2 1 1 2 2 2 2 2

    1 1 2 2

    , , ... , ,

    , , ... , ,

    ........

    , , ... , ,

    x x x

    x x x

    x x x

    n n

    n n

    n n n n n n

    [ ]G A AG

    = =< >

    g gij ij i j , ,

    det 0where G is a Graham matrix and the Ak are independent. Then we let:

    B

    b A

    b A

    X=

    < >

    < >

    =

    ,

    ...

    ,

    ...

    1 1

    n n

    x

    x

    with:

    GX B X G B= = 1

    Notice that the inversion of G can be done once. The computational cost of the approximation of a blockNxN with a polynomial of the 2nd degree in x,y is the cost of the computation of the product of a matrix nxneg. 6x6 by a vector 6x1 plus the cost of the computation of n=6 inner products between vectors with m=NxN

    components.

    We could consider to orthonormalize the basis Ak.

    We could consider different masks for the weights wi in order to try to reduce the border effects between

    blocks.

    Quantization of coefficientsThe coefficients have to be quantized in order to reduce the number of bits used to encode the block.As in the decoding we evaluate the polynomial:

    p(i,j) = x1+x2i+x3j+x4i2+x5ij+x6j2 con i,j {0,1,...,N}

    we impose an equal percentile error on each monomial, accepting, in the worst case, a global error 6 timesgreater.

    It is interesting to evaluate how much the value is different from the optimal value due to the quantization ofcoefficients. There may be another set of discrete coefficients values that lead to a smaller error between6e..e.

    The Simulated AnnealingIt is a stochastic method of optimization of the parameters of a system, based upon the process ofannealing, that is the crystallization of a liquid from the fusion of a solid: we gradually decrease thetemperature of the system so that the crystal structure modify slightly going through states of thermalequilibrium.

  • 8/6/2019 Fractal Compression ISAF

    11/25

    IFSAF Theory and Applications 11

    For us the states at low level of energy are the solutions that are closer to the optimal one, while thetemperature is our control-parameter.The main advantage of the simulated annealing is the ability of reaching the global minimum with lowprobability to remain caught in a local minimum.We have a system with a certain configuration x, with energy E(x). From this state we generate a new statey, with energy E(y), moving one of the particles only a bit far from the position it has in x. If E(y)

  • 8/6/2019 Fractal Compression ISAF

    12/25

    IFSAF Theory and Applications 12

    // Written by G.Pulcini// Last revised 1995/4/17 Rev 0.30

    #include "rcencdec.h"

    static BYTE GetOrientation(WORD SSum[4]){// ordering luminance sub-blocks 0123 index in a[] induce increasing order

    // a[0] brightest sub-block, ..., a[3] darkest sub-block (or its derivative)int i,k,sw,s=0,a[4]={0,1,2,3},FattTable[]={1,1,2,6,24};for(k=0; k

  • 8/6/2019 Fractal Compression ISAF

    13/25

    IFSAF Theory and Applications 13

    WORD B2 = B/2, I2, B4 = B/4, I4;WORD Sum = 0;DWORD Sum2= 0;WORD SSum[4]={0,0,0,0};DWORD SSum2[4]={0,0,0,0};WORD SSSum[4][4]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};for (WORD i=0; I=DWORD(id+i)

  • 8/6/2019 Fractal Compression ISAF

    14/25

    IFSAF Theory and Applications 14

    Id=(DWORD(id0+i)

  • 8/6/2019 Fractal Compression ISAF

    15/25

    IFSAF Theory and Applications 15

    break;case iso_ROT270:

    for(i=0; I=i*N, i

  • 8/6/2019 Fractal Compression ISAF

    16/25

    IFSAF Theory and Applications 16

    #define PROFILE(x) ;#endif

    #define ShowReset() {ShowEnd(); ShowBegin();}

    void Kernel::EncodeImageCR1(TEncStats &S, LPSTR szDefName, const RTImage img1, RTImage img2, TRpoolRhuge * RPI, TDpoolR huge * DPI1, PTBLOCK DPool){

    DWORD Time = GetTimeTicks();#ifdef PROFILINGDWORD Time1= GetTimeTicks();#endif

    char szFileName[80];strcpy(szFileName, szDefName);strcat(szFileName, ".RPT");rpt = fopen(szFileName,"w");

    if (Weight) { delete Weight; Weight=NULL; };CalcScheme();

    ShowBegin();ShowMessage("Converting image...");ShowRatio(0.0);

    HPBYTE lpDibRGB = img1.lpDib; // We should here test type of the image...

    HPBYTE lpY, lpI, lpQ, // YIQ color componentslpcY, lpcI, lpcQ; // YIQ contracted color components

    HPDPI DPI; // color Domain Pool InfoHPBYTE lpRGBbits = GetDibBitsAddr(lpDibRGB);

    if ( (1u

  • 8/6/2019 Fractal Compression ISAF

    17/25

    IFSAF Theory and Applications 17

    lpcY= (LPBYTE) GlobalAllocPtr(GMEM_MOVEABLE|GMEM_ZEROINIT, dwcImageSize);lpcI= (LPBYTE) GlobalAllocPtr(GMEM_MOVEABLE|GMEM_ZEROINIT, dwcImageSize);lpcQ= (LPBYTE) GlobalAllocPtr(GMEM_MOVEABLE|GMEM_ZEROINIT, dwcImageSize);if (!lpY || !lpI || !lpQ || !lpcY || !lpcI || !lpcQ) // do something{ MsgBox("Error", "Can't allocate memory #02"); return; }

    PROFILE("Memory allocation")

    // separating color componentsShowReset();ShowMessage("1 of 4 - Splitting image into YIQ color components...");DWORD dwSI, dwDI, dwSP, dwDP;for(WORD i=0; dwSI=dwLS*i, dwDI=(DWORD(i)

  • 8/6/2019 Fractal Compression ISAF

    18/25

    IFSAF Theory and Applications 18

    ShowMessage("4 of 4 - Encoding...");// we should end show here, because UpdateImage call by itself Showxxxx

    img2.Copy(img1);RGBLevelScale(img2, 1,2,1);InitImage(&img2);UpdateImage(0);

    PROFILE("Image setup and level scaling")strcpy(szFileName, szDefName);strcat(szFileName, ".IFS");FILE * cod = fopen(szFileName,"wb");

    double GlobalError = 0, GlobalYError=0, GlobalIError=0, GlobalQError=0;WORD N = B, N2=N/2,

    bN=bB, bN2=bN-1;

    BYTE * BlockY= new BYTE[N*N];BYTE * BlockI= new BYTE[N*N];BYTE * BlockQ= new BYTE[N*N];BYTE * BlockRGB= new BYTE[N*N*3];

    DWORD ID = 0x0AFFF08ul; // stepping 08 : Color IFS !!DWORD PackedHeaderSize;// WriteColorHeader{Pack(0, 0, cod); // initsPack(32, ID, cod);Pack(16, W, cod);Pack(16, H, cod);Pack( 8, B, cod);Pack( 8, did, cod);Pack( 8, djd, cod);Pack(16, dRadius, cod);for(int k=0; k

  • 8/6/2019 Fractal Compression ISAF

    19/25

    IFSAF Theory and Applications 19

    if (jdStart==0) jdEnd = Min(diameter, (int)md);if (jdEnd==md) jdStart=Max(0, int(md)-diameter);

    fprintf(rpt, "i::%d %d [%d] j::%d %d [%d]", idStart, idEnd, idEnd-idStart, jdStart,jdEnd, jdEnd-jdStart);

    fflush(rpt);DWORD I;for (WORD id=idStart; I=DWORD(id)*md, id

  • 8/6/2019 Fractal Compression ISAF

    20/25

    IFSAF Theory and Applications 20

    BlockIsoCrossSum(lpQ, Wbits, lpcQ, wbits, N, isoBest, y,x, id0,jd0, dwSumRD);Calculate(lpQ, Wbits, y, x, N, SumR, SumR2, SSumR, SSumR2, SSSumR);CalculateSums(lpcQ, wbits, id0, jd0, B, &dpi);SumD = dpi.Sum;SumD2= dpi.Sum2;MinimizeCRms(S, ALPHAQ, BETAQ, N, SumR, SumR2, SumD, SumD2, dwSumRD,

    ialpha, ibeta, alpha, beta, EQ);

    GetBlock(lpcQ, wbits, N, isoBest, alpha, beta, id0, jd0, BlockQ);Pack(quantizebits[ALPHAQ], ialpha, cod);Pack(quantizebits[BETAQ], ibeta, cod);alphaQ=alpha; betaQ=beta;}

    /*for(k=0; k

  • 8/6/2019 Fractal Compression ISAF

    21/25

    IFSAF Theory and Applications 21

    */{WORD I,IB,i,j;switch(k){case 0:for(i=0; I=i*N, IB=i*M, i

  • 8/6/2019 Fractal Compression ISAF

    22/25

    IFSAF Theory and Applications 22

    //No local search on sub-blocks by now//if (!ENC_OVERSTEP) overstep=1;//if (ENC_OVERSTEP && overstep==0) overstep=1;WORD I, BB=B*B, NN=N*N, md1=md/overstep, nd1=nd/overstep;//for (WORD id=0; I=id*md*overstep,id

  • 8/6/2019 Fractal Compression ISAF

    23/25

    IFSAF Theory and Applications 23

    alpha, beta, ialpha, ibeta, E0, E,S.dwDet0, S.dwAlphagt1, S.dwAlphalt0, S.dwAlphaltm1);

    S.dwAlphaCount++;if (E < EBest){idBest = id, jdBest = jd;EBest = E; E0Best = E0;isoBest = iso;

    alphaBest = alpha; betaBest = beta;ialphaBest = ialpha; ibetaBest = ibeta;LSSsuccess=TRUE;

    }} // for each isometry

    } // for each neightbor}

    if (LSSsuccess) S.dwLSSsuccess++;if (isoOldBest==isoBest) S.dwLSSsameiso++;

    */SE[K] = E = fabs(EBest);E0Best = fabs(E0Best);fprintf(rpt, "\tR(%d)

  • 8/6/2019 Fractal Compression ISAF

    24/25

    IFSAF Theory and Applications 24

    How the True-Color Encoding Works

    Background

    To understand the following, you should already know how fractal image compression works on gray-scaleimages, specifically the fractal image compression through IFS with exahustive search in the domain-pool.

    The knowledge of Fisher's paper SIGGRAPH92 course notes, and Jaquin's Fractal Image Compression - areview on IEEE transaction - should suffice.

    Abstract

    We use YIQ color model to exploit human eye different sensitivity to the Y,I,Q channels. So we assign morebits to Y and less bits to I,Q. But this all is trivial.For each range block, we search for the best-matching domain block on the Y channel and we use thecorresponding

    4blocks in I,Q channels as well-matching

    5blocks for the I,Q components. So we have only 1

    geometric transformation (the isometry and the address of the selected domain block) for all the threecomponents YIQ, while we recompute the optimal massic transformation for each of the YIQ channels.This approach can both save compression time and increase the compression ratio; compared to theobvious three separed encoding of each YIQ channel.

    How we can encode true color images

    In the first stage we have to convert the image from the RGB model to another color model that is moresuitable for exploiting the differences in how the human eye perceives the colors. These models can be oneof YIQ, YUV, HLS, HVS. We selected the YIQ model.Since the variance of the chrominance data (I,Q) tend to be less than the corrisponding one of the Ychannel, we can save space reducing the dynamic range of I and Q channels.Once we have selected a color model there are three ways of doing the encoding:

    1) To treat disjointly the 3 channels, doing 3 different encodings with different parameters: we coulddecrease bits for alpha and beta, reducing the 2 images and then restoring the original size in thedecompresion, etc.

    2) To treat the color image as a whole, doing all transformations directly on the color image. Obviously,each single transformation will be splitted in 3 parts, but all decisions are made upon global values - eg. aglobal error - and not on separated values for each channel.

    3) A mixing of these two methods. That's to say to encode jointly and asymmetrically the 3 channels.

    How we do encode true color images

    Let we have a true-color image in RGB format (24bpp) which we split into Y,I,Q color planes. Now we couldapply three times the gray-scale algorithm (such as the prof. Fisher's enc.c) to the Y, I and Q channels(obviously we can use different encoding parameters for each channel, privileging the Y at the expense of Iand Q). In so doing we'll get an algorithm that is almost 3 times slower than the gray-scale encoding. That'swhy we have developed a different approach.

    4- For corresponding blocks we mean those block that have the same coordinates in the image. Or, in other words, those three blocks (one in

    each of the Y,I,Q color planes) that are the color components of a block in the true-color image.5

    - Obviously they are not the best matching blocks, but here is the matter: we allow sub-optimal block mapping on the I and Q channels.

  • 8/6/2019 Fractal Compression ISAF

    25/25

    IFSAF Theory and Applications 25

    In the picture the Y,I,Q color components of the Lena image.

    Examinating several different images and their related YIQ color planes, we noticed that something as the"shape" of the gray-scale image on each channel is preserved - even if not exactly -.

    This pictures show the Y,I,Q components for the Lena image filteredin order to make more evident the shape that is almost exacly preserved.

    We noticed that if in a range block of the component Y there is some sort of a detail, we can distinguish itsshape in the I and Q components too, even if more or less attenuated in gray-level luminance of thatchannel. What is interesting in this all, is that we don't need to change the parameters of the geometrictransform (offset of best block and isometry) across the three channels, while recalculating the massictransform parameters for each channel.

    - Other ways to compress 24 bit images.

    - Results: a comparison between the sub-optimal yiq and the separated optimum.