예제 #1
0
        public static string CreateBMP(List <PointValue> pointValueList, string imagePath, double tempMax, double tempMin)
        {
            int    r = 0, c = 0;
            double x, y, preX = 0, preY = 0;
            int    insertPoint     = 1;
            bool   needInsertPoint = true;

            if (pointValueList.Count < 1000)
            {
                insertPoint = 100;
            }
            else if (pointValueList.Count < 10000)
            {
                insertPoint = 10;
            }
            else if (pointValueList.Count < 100000)
            {
                insertPoint = 10;
            }
            else
            {
                insertPoint     = 0;
                needInsertPoint = false;
            }

            //int insertPoint = 1;
            int afterInterpR = 1; //还未初始化// r + (r - 1) * insertPoint;
            int afterInterpC = 1; //还未初始化 c + (c - 1) * insertPoint;

            HSL[,] hslArray      = null;
            RgbColor[,] rgbArray = null;
            bool flagY = false;

            try
            {
                for (int it = 0; it < pointValueList.Count; ++it)//对结果点进行遍历
                {
                    if (it == 0)
                    {
                        preX = pointValueList[it].x;
                        preY = pointValueList[it].y;
                        c    = 1; r = 1;
                        continue;
                    }
                    x = pointValueList[it].x;
                    y = pointValueList[it].y;
                    if (y == preY && !flagY)
                    {
                        ++c;
                    }
                    else
                    {
                        flagY = true;
                    }
                    if (x == preX)
                    {
                        ++r;
                    }
                }
                afterInterpR          = r + (r - 1) * insertPoint;
                afterInterpC          = c + (c - 1) * insertPoint;
                double[,] afterInterp = new double[afterInterpR, afterInterpC];
                //如果需要插值

                for (int i = 0; i < r; i++)
                {
                    int j = 0;
                    int tmpR = 0, tmpC = 0, addC = 1;

                    for (; j < c - 1; j++)
                    {
                        tmpR = i * (insertPoint + 1);
                        tmpC = j * (insertPoint + 1);
                        afterInterp[tmpR, tmpC] = GetValueFromList(pointValueList, r, c, i, j);
                        if (needInsertPoint)
                        {
                            //横插
                            double diffC = (GetValueFromList(pointValueList, r, c, i, j + 1) -
                                            GetValueFromList(pointValueList, r, c, i, j)) / (insertPoint + 1);
                            addC = 1;
                            for (; addC <= insertPoint; ++addC)
                            {
                                afterInterp[tmpR, tmpC + addC] = afterInterp[tmpR, tmpC] + diffC * addC;
                            }
                        }
                    }
                    tmpR = i * (insertPoint + 1);
                    tmpC = j * (insertPoint + 1);
                    afterInterp[tmpR, tmpC] = GetValueFromList(pointValueList, r, c, i, j);
                }
                if (needInsertPoint)
                {
                    //纵插
                    for (int i = 0; i < r - 1; i++)
                    {
                        for (int j = 0; j < afterInterpC; j++)
                        {
                            int    tmpR  = i * (insertPoint + 1);
                            double diffR = (afterInterp[(i + 1) * (insertPoint + 1), j] - afterInterp[tmpR, j]) / (insertPoint + 1);
                            for (int addR = 1; addR <= insertPoint; ++addR)
                            {
                                afterInterp[tmpR + addR, j] = afterInterp[tmpR, j] + diffR * addR;
                            }
                        }
                    }
                }

                //转化为 HSL
                hslArray = new HSL[afterInterpR, afterInterpC];
                for (int i = 0; i < afterInterpR; i++)
                {
                    for (int j = 0; j < afterInterpC; j++)
                    {
                        hslArray[i, j]   = new HSL();
                        hslArray[i, j].H = (afterInterp[i, j] * 240) / (360);
                    }
                }

                rgbArray = new RgbColor[afterInterpR, afterInterpC];
                for (int i = 0; i < afterInterpR; i++)
                {
                    for (int j = 0; j < afterInterpC; j++)
                    {
                        rgbArray[i, j] = Hsl2Rgb(hslArray[i, j].H);
                    }
                }
            }
            catch (IndexOutOfRangeException e)
            {
                //LogFileManager.ObjLog.info(e.Message);
            }
            catch (Exception e)
            {
                //LogFileManager.ObjLog.info(e.Message);
            }
            FileStream   fs = null;
            BinaryWriter bw = null;

            try
            {
                fs = new FileStream(imagePath, FileMode.Create | FileMode.Append, FileAccess.Write, FileShare.None);
                bw = new BinaryWriter(fs);

                BITMAPINFOHEADER bih = new BITMAPINFOHEADER();
                ConstructBih(afterInterpC, afterInterpR, ref bih);

                BITMAPFILEHEADER bhh = new BITMAPFILEHEADER();
                ContructBhh(afterInterpC, afterInterpR, ref bhh);
                bhh.bfSize    = bih.biSizeImage + 54;
                bhh.bfOffBits = 54;

                //fwrite(&bhh,1,sizeof(BITMAPFILEHEADER),fpw);
                //fwrite(&bih,1,sizeof(BITMAPINFOHEADER),fpw);
                bhh.Serialize(bw);
                bih.Serialize(bw);

                //保存像素数据
                //左->右 下->上
                int index = 0;
                for (int i = 0; i <= afterInterpR - 1; ++i)
                {
                    for (int j = 0; j < afterInterpC; ++j)
                    {
                        index += 3;
                        byte red, green, blue;
                        red   = (byte)rgbArray[i, j].red;
                        green = (byte)rgbArray[i, j].green;
                        blue  = (byte)rgbArray[i, j].blue;
                        //注意三条语句的顺序:否则颜色会发生变化
                        bw.Write(red);
                        bw.Write(green);
                        bw.Write(blue);

                        //fwrite(&r, 1, sizeof(BYTE), fpw);
                        //fwrite(&g, 1, sizeof(BYTE), fpw);
                        //fwrite(&b, 1, sizeof(BYTE), fpw);
                        int len = afterInterpC % 4;
                        if ((len != 0) && (index % (afterInterpC * 3) == 0))
                        {
                            byte[] blank = new byte[len];
                            for (int b = 0; b < len; ++b)
                            {
                                blank[b] = 0;
                                bw.Write(blank[b]);
                                //sw.Write(blank[b].ToString());
                            }
                            //fwrite(blank, sizeof(BYTE), len, fpw);
                        }
                    }
                }
                bw.Close();
                fs.Close();
            }
            catch (Exception e)
            {
                //if(sw != null)
                //    sw.Close();
                if (fs != null)
                {
                    fs.Close();
                }
                //LogFileManager.ObjLog.info(e.Message);
            }
            return(imagePath);
        }