Ejemplo n.º 1
0
        private void DebugPlotImage(FitsDataModel fits)
        {
            FitsEntry primary = fits.GetPrimary();

            int width  = primary.GetParam <int>("NAXIS1");
            int height = primary.GetParam <int>("NAXIS2");
            int bpp    = primary.GetParam <int>("BITPIX") / 8;

            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    byte b1     = primary.data[(y * width + x) * bpp + 0];
                    byte b2     = primary.data[(y * width + x) * bpp + 1];
                    int  val    = ((int)b1 << 8) | b2;
                    int  rgbVal = 255 - b2;

                    bitmap.SetPixel(x, y, Color.FromArgb(rgbVal, rgbVal, rgbVal));
                }
            }

            pictureBox2.Image = bitmap;
            pictureBox2.Invalidate();
        }
Ejemplo n.º 2
0
        public FitsDataModel(byte [] data)
        {
            if (data.Length < BlockSize)
            {
                return;
            }

            int offset = 0;

            while (offset < data.Length)
            {
                FitsEntry entry = new FitsEntry();
                ParseEntry(data, offset, entry);
                offset += entry.headerSizeBytes + entry.data.Length;
                entries.Add(entry);
            }
        }
Ejemplo n.º 3
0
        private void ParseEntry(byte [] data, int offset, FitsEntry entry)
        {
            // Very first entry is Primary, other are distinguished by XTENSION keyword.

            if (offset == 0)
            {
                entry.type = FitsEntryType.Primary;
            }

            entry.entryOffsetBytes = offset;

            // Parse keywords until END (or end of file)

            while (offset < data.Length)
            {
                char[] paramRaw = new char[80];

                for (int i = 0; i < paramRaw.Length; i++)
                {
                    paramRaw[i] = (char)data[offset + i];
                }

                string    paramAsStr = new string(paramRaw);
                FitsParam param      = new FitsParam(paramAsStr);

                entry.header.Add(param);
                if (param.name == "END")
                {
                    break;
                }

                offset += paramRaw.Length;
            }

            // Align offset to next block

            offset = NextBlock(offset);
            entry.headerSizeBytes = offset - entry.entryOffsetBytes;

            // Deduce entry type.

            if (entry.ParamExists("XTENSION") && entry.type == FitsEntryType.Unknown)
            {
                switch (entry.GetParam <string>("XTENSION"))
                {
                case "IMAGE":
                    entry.type = FitsEntryType.ImageExtension;
                    break;

                case "TABLE":
                    entry.type = FitsEntryType.AsciiTableExtension;
                    break;

                case "BINTABLE":
                    entry.type = FitsEntryType.BinaryTableExtension;
                    break;
                }
            }

            // Deduce data size.

            // If entry type is unknown - skip until next XTENSION keyword (or end of file)

            int dataSize = 0;

            if (entry.type == FitsEntryType.Unknown)
            {
                while (offset < data.Length)
                {
                    string subString = "";
                    for (int i = 0; i < 8; i++)
                    {
                        subString += (char)data[offset + i];
                    }
                    if (subString == "XTENSION")
                    {
                        break;
                    }
                    offset += BlockSize;
                }

                if (offset >= data.Length)
                {
                    dataSize = data.Length - (entry.entryOffsetBytes + entry.headerSizeBytes);
                }
                else
                {
                    dataSize = offset - (entry.entryOffsetBytes + entry.headerSizeBytes);
                }
            }
            else
            {
                if (!entry.ParamExists("BITPIX"))
                {
                    throw new Exception("Missing BITPIX keyword!");
                }
                if (!entry.ParamExists("NAXIS"))
                {
                    throw new Exception("Missing NAXIS keyword!");
                }

                int elementSize = entry.GetParam <int>("BITPIX") / 8;
                int naxis       = entry.GetParam <int>("NAXIS");
                int numElements = naxis != 0 ? 1 : 0;
                for (int i = 0; i < naxis; i++)
                {
                    string paramName = "NAXIS" + (i + 1).ToString();
                    if (!entry.ParamExists(paramName))
                    {
                        throw new Exception("Missing " + paramName + " keyword!");
                    }
                    numElements *= entry.GetParam <int>(paramName);
                }
                dataSize = numElements * elementSize;
            }

            // Round up to block size
            if ((dataSize % BlockSize) != 0)
            {
                dataSize = NextBlock(dataSize);
            }

            // Copy away data[]

            entry.data = new byte[dataSize];

            for (int i = 0; i < dataSize; i++)
            {
                entry.data[i] = data[offset + i];
            }
        }