00001 #ifndef __J2K__Scale_HPP__
00002 #define __J2K__Scale_HPP__
00003
00004 #include <j2k/Fred/Basic.hpp>
00005 #include <math.h>
00006
00007
00008
00009 int Scale( double XMin, double XMax, int N, double* SMin, double* Step )
00010 {
00011 int negScale;
00012 int nbScaleSub;
00013 double initStep;
00014 double scaleStep;
00015 double temp;
00016 double scaleFactor;
00017 double scaleMax;
00018 int t;
00019 int i;
00020
00021
00022 int Steps[] = {
00023 10, 12, 15, 16, 20, 25, 30, 40, 50, 60, 75, 80, 100, 120, 150};
00024
00025 int NS = sizeof(Steps) / sizeof(Steps[0]);
00026
00027
00028 if ( XMin > XMax ) {
00029 temp = XMin;
00030 XMin = XMax;
00031 XMax = temp;
00032 }
00033
00034 if ( XMin == XMax ) {
00035 if ( XMin == 0.0 ) {
00036 XMax = 1.0;
00037 } else {
00038 XMax = XMin + fabs(XMin) / 10.0;
00039 }
00040 }
00041
00042
00043 if ( XMax <= 0 ) {
00044 negScale = 1;
00045 temp = XMin;
00046 XMin = -XMax;
00047 XMax = -temp;
00048 } else {
00049 negScale = 0;
00050 }
00051
00052 if ( N < 2 ) {
00053 N = 2;
00054 }
00055
00056 nbScaleSub = N - 1;
00057
00058 for (t = 0; t < 3; t++) {
00059
00060 intStep = (XMax - XMin) / nbScaleSub;
00061 scaleStep = intStep;
00062
00063 for (; scaleStep < 10.0; scaleStep *= 10.0);
00064 for (; scaleStep > 100.0; scaleStep /= 10.0);
00065
00066
00067 for (i = 0; i < NS && scaleStep > Steps [i]; i++);
00068 scaleFactor = intStep / scaleStep;
00069
00070
00071 do {
00072 *Step = scaleFactor * Steps [i];
00073 *SMin = floor (XMin / *Step) * *Step;
00074 scaleMax = *SMin + nbScaleSub * *Step;
00075
00076 if (XMax <= scaleMax) {
00077 if (negScale) *SMin = -scaleMax;
00078 *Step *= nbScaleSub / (N - 1);
00079 return 1;
00080 }
00081
00082 i++;
00083
00084 } while (i < NS);
00085
00086
00087 nbScaleSub *= 2;
00088 }
00089
00090
00091 return 0;
00092 }
00093
00094 #endif