Ejemplo n.º 1
0
//Load the BMP file
GLubyte* TextureLoadBitmap(char *filename, int *w, int *h)		/* I - Bitmap file to load */
{
	BITMAPINFO	*info;				/* Bitmap information */
	void		*bits;				/* Bitmap pixel bits */
	GLubyte	*rgb;				/* Bitmap RGB pixels */
	GLubyte   err = '0';

	/*
	* Try loading the bitmap and converting it to RGB...
	*/

	bits = LoadDIBitmap(filename, &info);
	if(bits==NULL) 
		return(NULL);
	rgb = ConvertRGB(info, bits);
	if (rgb == NULL)
	{
		free(info);
		free(bits);
	};

	printf("%s: %d %d\n", filename, info->bmiHeader.biWidth, info->bmiHeader.biHeight);
	printf("read %s successfully\n", filename);
	*w = info->bmiHeader.biWidth;
	*h = info->bmiHeader.biHeight;

	/*
	* Free the bitmap and RGB images, then return 0 (no errors).
	*/

	free(info);
	free(bits);
	return (rgb);

}
Ejemplo n.º 2
0
GLubyte *                      /* O - RGBA bitmap data */
LoadRGBA(const char *filename, /* I - Filename */
         BITMAPINFO **info)    /* O - Bitmap information */
    {
    GLubyte    *bgr;           /* BGR bitmap data */
    GLubyte    *rgba;          /* RGBA bitmap data */
    int        x, y;           /* Looping vars */
    int        length;         /* Length of scanline */
    GLubyte    *bgr_ptr;       /* Pointer into BGR data */
    GLubyte    *rgba_ptr;      /* Pointer into RGBA data */

    bgr    = LoadDIBitmap(filename, info);
    rgba   = malloc((*info)->bmiHeader.biWidth *
                    (*info)->bmiHeader.biHeight * 4);
    length = ((*info)->bmiHeader.biWidth * 3 + 3) & ~3;

    for (y = 0; y < (*info)->bmiHeader.biHeight; y ++)
        {
        bgr_ptr  = bgr + y * length;
        rgba_ptr = rgba + y * (*info)->bmiHeader.biWidth * 4;

        for (x = 0; x < (*info)->bmiHeader.biWidth; x ++, bgr_ptr += 3, rgba_ptr += 4)
            {
            rgba_ptr[0] = bgr_ptr[2];
            rgba_ptr[1] = bgr_ptr[1];
            rgba_ptr[2] = bgr_ptr[0];
            rgba_ptr[3] = (bgr_ptr[0] + bgr_ptr[1] + bgr_ptr[2]) / 3;
            }
        }
    free(bgr);
    return (rgba);
    }
Ejemplo n.º 3
0
static void LoadImage(bytemap_t *image, const char *filename)
{
	int i, j;
	unsigned char *image_buffer = NULL;
	BITMAPINFO *image_info = NULL;

	if (image_info) free(image_info);
	image_buffer = LoadDIBitmap(filename, &image_info);
#if 0
	assert(image_info->bmiHeader.biWidth == display_width);
	assert(image_info->bmiHeader.biHeight == display_height);
	assert(image_info->bmiHeader.biBitCount == 8);
	printf("image_info->bmiHeader.biSize %d\n", image_info->bmiHeader.biSize);
	printf("image_info->bmiHeader.biWidth %d\n", image_info->bmiHeader.biWidth);
	printf("image_info->bmiHeader.biHeight %d\n", image_info->bmiHeader.biHeight);
	printf("image_info->bmiHeader.biPlanes %d\n", image_info->bmiHeader.biPlanes);
	printf("image_info->bmiHeader.biBitCount %d\n", image_info->bmiHeader.biBitCount);
	printf("image_info->bmiHeader.biCompression %d\n", image_info->bmiHeader.biCompression);
	printf("image_info->bmiHeader.biSizeImage %d\n", image_info->bmiHeader.biSizeImage);
	printf("image_info->bmiHeader.biXPelsPerMeter %d\n", image_info->bmiHeader.biXPelsPerMeter);
	printf("image_info->bmiHeader.biYPelsPerMeter %d\n", image_info->bmiHeader.biYPelsPerMeter);
	printf("image_info->bmiHeader.biClrUsed %d\n", image_info->bmiHeader.biClrUsed);
	printf("image_info->bmiHeader.biClrImportant %d\n", image_info->bmiHeader.biClrImportant);
#endif
	// read to bytemap
	for (j = 0; j < image->header.height; j++) {
		memcpy(image->buffer+(image->header.height-1-j)*image->header.pitch,
			image_buffer+j*image_info->bmiHeader.biWidth,
			image_info->bmiHeader.biWidth);
	}
	free(image_info);
	free(image_buffer);
}
Ejemplo n.º 4
0
GLubyte * DIBitmap::LoadRGBA(const char *filename, BITMAPINFO **info)
{
    GLubyte    *bgr;           /* BGR bitmap data */
    GLubyte    *rgba;          /* RGBA bitmap data */
    int        x, y;           /* Looping vars */
    int        length;         /* Length of scanline */
    GLubyte    *bgr_ptr;       /* Pointer into BGR data */
    GLubyte    *rgba_ptr;      /* Pointer into RGBA data */

    bgr    = LoadDIBitmap(filename, info);
    rgba   = (unsigned char *) malloc((*info)->bmiHeader.biWidth *
                    (*info)->bmiHeader.biHeight * 4);
    length = ((*info)->bmiHeader.biWidth * 3 + 3) & ~3;

    for (y = 0; y < (*info)->bmiHeader.biHeight; y ++)
        {
        bgr_ptr  = bgr + y * length;
        rgba_ptr = rgba + y * (*info)->bmiHeader.biWidth * 4;

        for (x = 0; x < (*info)->bmiHeader.biWidth; x ++, bgr_ptr += 3, rgba_ptr += 4)
            {
				rgba_ptr[0] = bgr_ptr[2];
				rgba_ptr[1] = bgr_ptr[1];
				rgba_ptr[2] = bgr_ptr[0];
				if(rgba_ptr[0]==_limpidColor[0] && 
				   rgba_ptr[1]==_limpidColor[1] && 
				   rgba_ptr[2]==_limpidColor[2])
				   rgba_ptr[3] = 0;
				else rgba_ptr[3] = 255;
            }
        }
    free(bgr);
    return (rgba);
}
Ejemplo n.º 5
0
bool Texture::Load(const std::string& filename)
{
    unsigned int w = 0;
    unsigned int h = 0;
    unsigned char* data = 0;
    unsigned int bpp = 3; // bytes per pixel

#ifdef AMJU_USE_SDL_IMG

    SDL_Surface* surf = IMG_Load(filename.c_str());
    if (!surf)
    {
        ReportError("Failed to load texture " + filename);
        return false;
    }
    w = surf->w;
    h = surf->h;
    data = (unsigned char*)surf->pixels;
    bpp = surf->format->BytesPerPixel;

#else

    std::string ext = ToLower(GetFileExt(filename));
    if (ext == "bmp")
    {
        data = LoadDIBitmap(filename.c_str(), &w, &h);
    }
    else if (ext == "png")
    {
        data = LoadPng(filename.c_str(), &w, &h, &bpp);
        // TODO I think bmps are upside down, but the rest of the code compensates.. sigh
        FlipBmp(data, w, h, bpp);
    }

    if (!data)
    {
        ReportError("Failed to load texture " + filename);
        return false;
    }

#endif

    Create(data, w, h, bpp);

#ifdef AMJU_USE_SDL_IMAGE
    SDL_FreeSurface(surf);
#else
    delete [] data;
#endif

#ifdef _DEBUG
    m_name = StripPath(filename);
#endif

    return true;
}
Ejemplo n.º 6
0
Resource* BmpALoader(const std::string& resName)
{
  std::string img = resName.substr(0, resName.size() - 1);
  std::string alpha = GetFileNoExt(img) + "-a.bmp";

  unsigned int w = 0;
  unsigned int h = 0;
  
  unsigned char* rgbdata = LoadDIBitmap(img.c_str(), &w, &h);
  
  if (!rgbdata)
  {
//?    ReportError("Failed to load texture " + img);
    return 0;
  }

  // Add space for alpha
  unsigned char* withAlpha = AddAlpha(rgbdata, w, h);
  Assert(withAlpha);
  delete [] rgbdata;
  rgbdata = 0;

  // Load alpha image
  unsigned int aw = 0;
  unsigned int ah = 0;
  unsigned char* alphadata = LoadDIBitmap(alpha.c_str(), &aw, &ah);
  Assert(alphadata);
  Assert(aw == w);
  Assert(ah == h);

  // Copy alpha image to alpha channel of RGB image
  CopyAlpha(alphadata, withAlpha, w, h);
  delete [] alphadata;
  alphadata = 0;

  Texture* pTex = new Texture;
  pTex->Create(withAlpha, w, h, 4); // 4 bytes per pixel
  delete [] withAlpha;

  return (Resource*)pTex;
}
Ejemplo n.º 7
0
void initTexture(char* filename, GLuint *texture, GLenum *type)
{
    BITMAPINFO	*info;           /* Bitmap information */
    GLubyte	    *bits;           /* Bitmap RGB pixels */
    GLubyte     *ptr;            /* Pointer into bit buffer */
    GLubyte	    *rgba;           /* RGBA pixel buffer */
    GLubyte	    *rgbaptr;        /* Pointer into RGBA buffer */

    // Load a texture object (256x256 true color)
    bits = LoadDIBitmap(filename, &info);
    if (bits == (GLubyte *)0)
		return;

    // Figure out the type of texture
    if (info->bmiHeader.biHeight == 1)
        *type = GL_TEXTURE_1D;
    else
        *type = GL_TEXTURE_2D;

    // Create and bind a texture object
    glGenTextures(1, texture);
	glBindTexture(*type, *texture);

    // Create an RGBA image
    rgba = (GLubyte *)malloc(info->bmiHeader.biWidth * info->bmiHeader.biHeight * 4);

    int i = info->bmiHeader.biWidth * info->bmiHeader.biHeight;
    for( rgbaptr = rgba, ptr = bits;  i > 0; i--, rgbaptr += 4, ptr += 3)
    {
            rgbaptr[0] = ptr[2];     // windows BMP = BGR
            rgbaptr[1] = ptr[1];
            rgbaptr[2] = ptr[0];
            rgbaptr[3] = (ptr[0] + ptr[1] + ptr[2]) / 3;
    }

	// Set texture parameters
	glTexParameteri(*type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(*type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(*type, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(*type, GL_TEXTURE_WRAP_T, GL_CLAMP);

    glTexImage2D(*type, 0, 4, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
                  0, GL_RGBA, GL_UNSIGNED_BYTE, rgba );
}
void Texture::initTexture(char *path) {
  //ctor
	//printf ("\nLoading texture..\n");
  // Load a texture object (256x256 true color)
  bits = LoadDIBitmap(path, &info);
  if (bits == (GLubyte *)0) {
		//printf ("Error loading texture!\n\n");
	} else {
    // Figure out the type of texture
    if (info->bmiHeader.biHeight == 1) {
      type = GL_TEXTURE_1D;
    } else {
      type = GL_TEXTURE_2D;
    }

    // Create and bind a texture object
    glGenTextures(1, &texture);
    glBindTexture(type, texture);

    // Create an RGBA image
    rgba = (GLubyte *)malloc(info->bmiHeader.biWidth * info->bmiHeader.biHeight * 4);

    int i;
    i = info->bmiHeader.biWidth * info->bmiHeader.biHeight;
    for(rgbaptr = rgba, ptr = bits;  i > 0; i--, rgbaptr += 4, ptr += 3)
    {
      rgbaptr[0] = ptr[2];     // windows BMP = BGR
      rgbaptr[1] = ptr[1];
      rgbaptr[2] = ptr[0];
      rgbaptr[3] = (ptr[0] + ptr[1] + ptr[2]) / 3;
    }

    // Set texture parameters
    glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_CLAMP);

    glTexImage2D(type, 0, 4, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
                    0, GL_RGBA, GL_UNSIGNED_BYTE, rgba );
    //printf("Textures ok.\n\n", texture);
	}
}
Ejemplo n.º 9
0
int loadBMP(char *filename) {
  
  GLubyte *img;

  BITMAPINFO **img_info = malloc(sizeof(BITMAPINFO *));
  img = LoadDIBitmap(filename, img_info);

  glBindTexture(GL_TEXTURE_2D, g_store.image_store.current);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 0);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

  glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, (**img_info).bmiHeader.biWidth, (**img_info).bmiHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, img);

  return g_store.image_store.current++;
}
Ejemplo n.º 10
0
//************************************************************
// read a BMP file
//************************************************************
void showBmpFile(char *path){

    BITMAPINFO  *info=0;

    GLubyte     *bits = (GLubyte*)LoadDIBitmap(path,&info);
	if (!bits || !info) {
		return;
	};

	GLubyte  *pxls=(GLubyte*)BitmapToPixels(info,bits,3);

	int width=info->bmiHeader.biWidth;
	int height=info->bmiHeader.biHeight;

	if (pxls) {
		writePixels(pxls, width, height);
		FREE(pxls);
	};

	FREE(info);
	FREE(bits);
}
void 
OpenBitmapFile(char *filename )	
		/* I - Bitmap file to open */
{
  char		title[256],		/* Title of file (no path info) */
		name[256],		/* Full pathname of file */
		directory[256];		/* Directory of file */
  OPENFILENAME	ofn;			/* Filename dialog structure */
  void		*bits;			/* Bitmap pixel bits */
  GLubyte		*BitmapBits = NULL;	


 /*
  * Pop up a filename dialog if we don't have a filename...
  */

  if (filename == NULL ||
      filename[0] == '\0')
  {
   /*
    * Fill in the OPENFILENAME dialog info...
    */

    strcpy(directory, ".");
    strcpy(name, "");
    strcpy(title, "");

    memset(&ofn, 0, sizeof(ofn));

    ofn.lStructSize      = sizeof(ofn);
    ofn.hwndOwner        = ViewWindow;
    ofn.lpstrFilter      = "Bitmaps\0*.BMP\0\0";
    ofn.nFilterIndex     = 1;
    ofn.lpstrFile        = name;
    ofn.nMaxFile         = sizeof(name) - 1;
    ofn.lpstrFileTitle   = title;
    ofn.nMaxFileTitle    = sizeof(title) - 1;
    ofn.lpstrInitialDir  = directory;
    ofn.lpstrTitle       = "Open Bitmap File";
    ofn.Flags            = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |
                           OFN_NONETWORKBUTTON | OFN_FILEMUSTEXIST;

    if (!GetOpenFileName(&ofn))
      return;

    filename = name;
  };

 /*
  * Free the old bitmap if there is one...
  */

  if (BitmapInfo != NULL)
  {
    free(BitmapInfo);
    free(BitmapBits);
    BitmapInfo = NULL;
  };

 /*
  * Load the named bitmap from disk.  Display an error message if it
  * doesn't load...
  */

  bits = LoadDIBitmap(filename, &BitmapInfo);

  bmpheight=(*BitmapInfo).bmiHeader.biHeight;
  bmpwidth=(*BitmapInfo).bmiHeader.biWidth;

  printf("bmp dimensions  height %d  width %d ",bmpheight,bmpwidth);

  if (bits == NULL)
    DisplayErrorMessage("Could not open bitmap file \'%s\'!", filename);
  else
  {
    BitmapBits = ConvertRGB(BitmapInfo, bits);
	bmpaddress=BitmapBits;
	free(bits);

    if (BitmapBits == NULL)
    {
      DisplayErrorMessage("Could not convert bitmap to RGB!");
      free(BitmapInfo);
      BitmapInfo = NULL;
    };
  };
} 
Ejemplo n.º 12
0
/**
Initialize the texture
*/
void initTexture(void)
{
    printf ("\nLoading texture..\n");
    // Load a texture object (256x256 true color)
    bits = LoadDIBitmap("..\\res\\tiledbronze.bmp", &info);
    if (bits == (GLubyte *)0) {
        printf ("Error loading texture!\n\n");
        return;
    }


    // Figure out the type of texture
    if (info->bmiHeader.biHeight == 1)
        type = GL_TEXTURE_1D;
    else
        type = GL_TEXTURE_2D;

    // Create and bind a texture object
    glGenTextures(1, &texture);
    glBindTexture(type, texture);



    // Create an RGBA image
    rgba = (GLubyte *)malloc(info->bmiHeader.biWidth * info->bmiHeader.biHeight * 4);

    i = info->bmiHeader.biWidth * info->bmiHeader.biHeight;
    for( rgbaptr = rgba, ptr = bits;  i > 0; i--, rgbaptr += 4, ptr += 3)
    {
        rgbaptr[0] = ptr[2];     // windows BMP = BGR
        rgbaptr[1] = ptr[1];
        rgbaptr[2] = ptr[0];
        rgbaptr[3] = (ptr[0] + ptr[1] + ptr[2]) / 3;
    }

    // Set texture parameters
    glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_REPEAT); // why not GL_REPEAT?
    glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexImage2D(type, 0, 4, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
                 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba );


    printf("Textura %d\n", texture);



    // Now loads the map:
    printf ("\nLoading map..\n");
    // Load a texture object (256x256 true color)
    mapbits = LoadDIBitmap("..\\res\\modelmap.bmp", &mapinfo);
    if (mapbits == (GLubyte *)0) {
        printf ("Error loading map!\n\n");
        return;
    }

    int x, z;

    //printf("Map Height = %d\n", mapinfo->bmiHeader.biHeight);
    int carFound = 0;

    for (x = 0; x < mapinfo->bmiHeader.biWidth; x++) {
        for (z = 0; z < mapinfo->bmiHeader.biHeight; z++) {
            if (((mapbits + x * z)[0] >= (GLubyte)250) && ((mapbits + x * z)[2] == ((GLubyte)000)) && ((mapbits + x * z)[1] == ((GLubyte)000))) { // Blue car
                printf("found blue car at (%d, %d)\n", x, z);
                blueCar_x = (float)x / (float)mapinfo->bmiHeader.biWidth;
                blueCar_z = (float)z / (float)mapinfo->bmiHeader.biHeight;
                carFound++;
            }

            if (((mapbits + x * z)[2] >= ((GLubyte)1)) && ((mapbits + x * z)[0] == ((GLubyte)000)) && ((mapbits + x * z)[1] == ((GLubyte)000)))  { // Red car
                printf("found red car at (%d, %d)\n", x, z);
                redCar_x = (float)x / (float)mapinfo->bmiHeader.biWidth;
                redCar_z = (float)z / (float)mapinfo->bmiHeader.biHeight;
                carFound++;
            }
        }
    }
    if (carFound == 2) //found both cars
        foundCars = true;
    else
    {
        foundCars = false;
        printf("Error loading map: car(s) not found\n");
    }


    printf("Textures ok.\n\n", texture);
}
Ejemplo n.º 13
0
void DIBitmap::loadBitmap(const char *fileName)
{
	_bitmap = LoadDIBitmap(fileName,&_bitMapInfo);
}