⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsegyendianess.c

📁 segy 显示程序!希望能给正在做这部分朋友提供一部分资料
💻 C
📖 第 1 页 / 共 2 页
字号:
/* 
 * GSEGYLIB - Library for accessing files in SEG-Y format
 *
 * Copyright (C) 2005-2006 Vladimir Bashkardin
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more av.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author:  Vladimir Bashkardin  <vovizmus@users.sourceforge.net>
 */

#include <math.h>
#include "gsegyendianess.h"

G_DEFINE_TYPE (GSEGYEndianess, g_segy_endianess, G_TYPE_OBJECT)

GSEGYEndianess* g_segy_endianess_new_with_default (void) {
    return G_SEGY_ENDIANESS (g_object_new (G_SEGY_TYPE_ENDIANESS, NULL));
}

GSEGYEndianess* g_segy_endianess_new (gboolean swap_bytes, gboolean ibm_to_ieee) {
    return G_SEGY_ENDIANESS (g_object_new (G_SEGY_TYPE_ENDIANESS, "swap_bytes", swap_bytes, "ibm_to_ieee", ibm_to_ieee, NULL));
}

void g_segy_endianess_ibm_to_ieee (GSEGYEndianess *self, guint32 *ibm, guint32 *ieee) {
    guint32 input = *ibm;
    guint32 mantissa, t;

    if (self->ibm_to_ieee) {
        if (input) {
            if (self->swap_bytes)
                input = (input << 24) | ((input >> 24) & 0xff) |
                         ((input & 0xff00) << 8) | ((input & 0xff0000) >> 8);

            mantissa = 0x00ffffff & input;
            if (0 == mantissa) {
                *ieee = 0;
                return;
            }
            t = (guint32) ((0x7f000000 & input) >> 22) - 130;
            while (!(mantissa & 0x00800000)) {
                t = t - 1; 
                mantissa <<= 1;
            }
            if (t > 254)
                input = (0x80000000 & input) | 0x7f7fffff;
            else if (t <= 0)
                input = 0;
            else
                input = (0x80000000 & input) | (t << 23) | (0x007fffff & mantissa);
        }
    } else if (self->swap_bytes && input)
        input = (input << 24) | ((input >> 24) & 0xff) |
                 ((input & 0xff00) << 8) | ((input & 0xff0000) >> 8);

    *ieee = input;
}

void g_segy_endianess_ibm_to_ieee_array (GSEGYEndianess *self, guint32 *ibm, guint32 *ieee, guint64 length) {
    guint64 i;

    if (self->ibm_to_ieee) {
        guint32 input;
        guint32 mantissa, t;
        for (i = 0; i < length; i++) {
            input = ibm[i];
            if (input) {
                if (self->swap_bytes)
                    input = (input << 24) | ((input >> 24) & 0xff) |
                             ((input & 0xff00) << 8) | ((input & 0xff0000) >> 8);

                mantissa = 0x00ffffff & input;
                if (0 == mantissa) {
                    *ieee = 0;
                    return;
                }
                t = (guint32) ((0x7f000000 & input) >> 22) - 130;
                while (!(mantissa & 0x00800000)) {
                    t = t - 1; 
                    mantissa <<= 1;
                }
                if (t > 254)
                    input = (0x80000000 & input) | 0x7f7fffff;
                else if (t <= 0)
                    input = 0;
                else
                    input = (0x80000000 & input) | (t << 23) | (0x007fffff & mantissa);
            }
            ieee[i] = input;
        }
    } else {
        if (ibm != ieee) {
            for (i = 0; i < length; i++)
                ieee[i] = ibm[i];
        }
        if (self->swap_bytes) {
            for (i = 0; i < length; i++) {
                if (ieee[i])
                    ieee[i] = (ieee[i] << 24) | ((ieee[i] >> 24) & 0xff) |
                               ((ieee[i] & 0xff00) << 8) | ((ieee[i] & 0xff0000) >> 8);
            }
        }
    }
}

void g_segy_endianess_ieee_to_ibm (GSEGYEndianess *self, guint32 *ieee, guint32 *ibm) {
    guint32 input = *ieee;
    guint32 mantissa, t;

    if (self->ibm_to_ieee) {
        if (input) {
            mantissa = (0x007fffff & input) | 0x00800000;
            t = (guint32) ((0x7f800000 & input) >> 23) - 126;
            while (t & 0x3) {
                t = t + 1;
                mantissa >>= 1;
            }
            input = (0x80000000 & input) | (((t >> 2) + 64) << 24) | mantissa;

            if (self->swap_bytes)
                input = (input << 24) | ((input >> 24) & 0xff) |
                         ((input & 0xff00) << 8) | ((input & 0xff0000) >> 8);
        }
    } else if (self->swap_bytes && input)
        input = (input << 24) | ((input >> 24) & 0xff) |
                 ((input & 0xff00) << 8) | ((input & 0xff0000) >> 8);

    *ibm = input;
}

void g_segy_endianess_ieee_to_ibm_array (GSEGYEndianess *self, guint32 *ieee, guint32 *ibm, guint64 length) {
    guint64 i;

    if (self->ibm_to_ieee) {
        guint32 input;
        guint32 mantissa, t;
        for (i = 0; i < length; i++) {
            if (input) {
                input = ieee[i];
                mantissa = (0x007fffff & input) | 0x00800000;
                t = (guint32) ((0x7f800000 & input) >> 23) - 126;
                while (t & 0x3) {
                    t = t + 1;
                    mantissa >>= 1;
                }
                input = (0x80000000 & input) | (((t >> 2) + 64) << 24) | mantissa;
            }

            if (self->swap_bytes)
                input = (input << 24) | ((input >> 24) & 0xff) |
                        ((input & 0xff00) << 8) | ((input & 0xff0000) >> 8);
            ibm[i] = input;
        }
    } else {
        if (ieee != ibm) {
            for (i = 0; i < length; i++)
                ibm[i] = ieee[i];
        }
        if (self->swap_bytes) {
            for (i = 0; i < length; i++) {
                if (ibm[i])
                    ibm[i] = (ibm[i] << 24) | ((ibm[i] >> 24) & 0xff) |
                             ((ibm[i] & 0xff00) << 8) | ((ibm[i] & 0xff0000) >> 8);
            }
        }
    }
}

void g_segy_endianess_swap_ieee_float (GSEGYEndianess *self, guint32 *input, guint32 *output) {
    guint32 temp = *input;

    if (self->swap_bytes)
        temp = (((temp >> 24) & 0xff) | ((temp & 0xff) << 24) |
                ((temp >> 8) & 0xff00) | ((temp & 0xff00) << 8));

    *output = temp;
}

void g_segy_endianess_swap_ieee_float_array (GSEGYEndianess *self, guint32 *input, guint32 *output, guint64 length) {
    guint64 i;

    if (self->swap_bytes) {
        for (i = 0; i < length; i++) {
            output[i] = (((input[i] >> 24) & 0xff) | ((input[i] & 0xff) << 24) |
                         ((input[i] >> 8) & 0xff00) | ((input[i] & 0xff00) << 8));
        }
    } else if (input != output) {
        for (i = 0; i < length; i++)
            output[i] = input[i];
    }
}

void g_segy_endianess_gain_code_to_float (GSEGYEndianess *self, guint32 *input, guint32 *output) {
    guint32 temp = *input;
    gint16 num;
    gint8 gain;
    gfloat *result = (gfloat*)output;

    if (self->swap_bytes)
        temp = (temp << 24) | ((temp >> 24) & 0xff) |
                ((temp & 0xff00) << 8) | ((temp & 0xff0000) >> 8);

    num = temp & 0xffff;
    gain = (temp & 0xff0000) >> 16;
    *result = num * pow (2.0, gain);
}

void g_segy_endianess_gain_code_to_float_array (GSEGYEndianess *self, guint32 *input, guint32 *output, guint64 length) {
    guint64 i;
    guint32 temp;
    gint16 num;
    gint8 gain;

    for (i = 0; i < length; i++) {
        temp = input[i];
        if (self->swap_bytes)
            temp = (temp << 24) | ((temp >> 24) & 0xff) |
                    ((temp & 0xff00) << 8) | ((temp & 0xff0000) >> 8);

            num = temp & 0xffff;
            gain = (temp & 0xff0000) >> 16;
            output[i] = num * pow (2.0, gain);
    }
}

void g_segy_endianess_float_to_gain_code (GSEGYEndianess *self, guint32 *input, guint32 *output) {
    gfloat temp = *(gfloat*)input;
    gfloat tmp;
    gint16 num;
    gint8 gain = 0;

    if (fabs (temp) < 1.0) {
        for (gain = 1; gain < G_MAXINT8; gain++) {
            tmp = fabs (temp * pow (2.0, gain));
            if (tmp > G_MAXINT8 && tmp < G_MAXINT16) {
                num = temp * pow (2.0, gain);
                gain = -gain;
                break;
            }
        }
    } else {
        for (gain = 1; gain < G_MAXINT8; gain++) {
            tmp = fabs (temp / pow (2.0, gain));
            if (tmp > G_MAXINT8 && tmp < G_MAXINT16) {
                num = temp / pow (2.0, gain);
                break;
            }
        }
    }

    *output = 0;
    *output = ((guint8)gain << 16) | (guint16)num;

    if (self->swap_bytes)
        *output = (*output << 24) | ((*output >> 24) & 0xff) |
                   ((*output & 0xff00) << 8) | ((*output & 0xff0000) >> 8);
}

void g_segy_endianess_float_to_gain_code_array (GSEGYEndianess *self, guint32 *input, guint32 *output, guint64 length) {
    guint64 i;
    gfloat temp;
    gfloat tmp;
    gint16 num;
    gint8 gain = 0;

    for (i = 0; i < length; i++) {
        temp = *(gfloat*)&input[i];
        if (fabs (temp) < 1.0) {
            for (gain = 1; gain < G_MAXINT8; gain++) {
                tmp = fabs (temp * pow (2.0, gain));
                if (tmp > G_MAXINT8 && tmp < G_MAXINT16) {
                    num = temp * pow (2.0, gain);
                    gain = -gain;
                    break;
                }
            }
        } else {
            for (gain = 1; gain < G_MAXINT8; gain++) {
                tmp = fabs (temp / pow (2.0, gain));
                if (tmp > G_MAXINT8 && tmp < G_MAXINT16) {
                    num = temp / pow (2.0, gain);
                    break;
                }
            }
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -