summaryrefslogtreecommitdiffstats
path: root/menu_demo/bitmap.c
blob: 1739c11bb54634bde328152d88a66d84df1c7b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifdef G_OS_WIN32
  #define WIN32_LEAN_AND_MEAN 1
  #include <windows.h>
  #define GL_BGR  0x80E0
  #define GL_BGRA 0x80E1
#endif

#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>

#include "bitmap.h"

#define BITMAP_FILESIZE 0x02
#define BITMAP_OFFSET 0x0a
#define BITMAP_HEADERSIZE 0x0e
#define BITMAP_WIDTH 0x12
#define BITMAP_HEIGHT 0x16
#define BITMAP_DEPTH 0x1c
#define BITMAP_IMAGE_SIZE 0x22


struct BitmapStruct BitmapLoad(char *filename)
{
  struct BitmapStruct l_sImage;
  GLuint texture;
  FILE *bitmap;

  // try to open the file
  bitmap = fopen(filename, "r");

  // does the bitmap exist?
  if (bitmap > 0)
  {
    unsigned int dataoffset, filesize, imagesize;
    short depth;
    GLsizei width, height;
    unsigned char *imagedata;

    fseek(bitmap, BITMAP_FILESIZE, SEEK_SET);
    fread(&filesize, 4, 1, bitmap);
    fseek(bitmap, BITMAP_OFFSET, SEEK_SET);
    fread(&dataoffset, 4, 1, bitmap);
    fseek(bitmap, BITMAP_WIDTH, SEEK_SET);
    fread(&width, 4, 1, bitmap);
    fread(&height, 4, 1, bitmap);
    fseek(bitmap, BITMAP_DEPTH, SEEK_SET);
    fread(&depth, 2, 1, bitmap);
    fseek(bitmap, BITMAP_IMAGE_SIZE, SEEK_SET);
    fread(&imagesize, 4, 1, bitmap);

    imagedata = (unsigned char *)malloc((size_t)imagesize);

    fseek(bitmap, dataoffset, SEEK_SET);
    fread(imagedata, (size_t)imagesize, 1, bitmap);

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    if (depth == 24) // 24 bits
    {
      gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_BGR, GL_UNSIGNED_BYTE, imagedata);
    }
    else // depth == 32 bits
    {
      gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_BGRA, GL_UNSIGNED_BYTE, imagedata);;
    }

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    l_sImage.m_iImageId = texture;
    l_sImage.m_iWidth   = width;
    l_sImage.m_iHeight  = height;

    free(imagedata);
    fclose(bitmap);

    return l_sImage;
  }
  else
  {
    // couldn't find bitmap
    exit(0);
  }
}



void BitmapConvertWidth(struct BitmapStruct *f_sImage, double f_dHeight)
{
  double l_dRatio;

  l_dRatio = (double)f_sImage->m_iHeight / f_dHeight;
  f_sImage->m_iWidth = (int)(f_sImage->m_iWidth / l_dRatio);

} // ConvertButton