Umsetzung auf C++ Code bringt absturz :(



  • Hi,

    ich hab versucht C code in C++ umzusetzen, angefangen beim einlesen einer bitmap-Datei, leider bringt das den totalen PC Crash

    hier der code

    unsigned char* CBMPLoader::LoadBitmap(char *file)
    {	BITMAPINFOHEADER bitmapInfoHeader;    // This will hold the info header of the bitmap.
    
    	BITMAPFILEHEADER header;      // This will hold the bitmap header information.
    
    	unsigned char *textureData = 0;  // This will hold the bitmap image itself.
    
       // This will be used to swap the image colors from BGR to RGB.
    	unsigned char textureColors = 0;
    
    //	pFile = fopen(file, "rb");    // Open the file.
    
    //	if(pFile == 0) return 0;      // Check and make sure there are no errors.
    
        std::fstream x;
        x.open (file, std::ios::in);
    
        x.read ((char*)&header, sizeof(BITMAPFILEHEADER));
    
    	// Read in the bitmap header info into the BITMAPFILEHEADER variable.
    //	fread(&header, sizeof(BITMAPFILEHEADER), 1, pFile);
    
    	// Make sure this is a real bitmap by checking the ID.
    	if(header.bfType != BITMAP_ID)
    	   {
    //		   fclose(pFile);
    		   return 0;
    	   }
    
    	// Read in the second header info into the BITMAPINFOHEADER variable.
    //	fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, pFile);
    
        x.read ((char*)&bitmapInfoHeader, sizeof(BITMAPINFOHEADER));
    
       if(bitmapInfoHeader.biSizeImage == 0)
          bitmapInfoHeader.biSizeImage = bitmapInfoHeader.biWidth * bitmapInfoHeader.biHeight * 3;
    
    	// Place the pointer in front of where the image data starts.
    //	fseek(pFile, header.bfOffBits, SEEK_SET);
        x.seekg (header.bfOffBits, std::ios::cur);
    	// Dynamically create enough memory for the image.
    	textureData = (unsigned char*)malloc(bitmapInfoHeader.biSizeImage);
    
    	// Error checking.  Make sure the memory was allocated.
    	if(!textureData)
    	   {
    		   free(textureData);
    //		   fclose(pFile);
    		   return 0;
    	   }
    
    	// Read in the image.
    //	fread(textureData, 1, bitmapInfoHeader->biSizeImage, pFile);
    
        x.read ((char*)&textureData, bitmapInfoHeader.biSizeImage);
    	// Error checking.  Make sure an image was loaded.
    	if(textureData == 0)
    	   {
    	//	   fclose(pFile);
    		   return 0;
    	   }
    
    	// Bitmaps are saved in BGR format so we will make the image RGB by...
    	for(int index = 0; index < (int)bitmapInfoHeader.biSizeImage; index+=3)
    	   {
    		   textureColors = textureData[index];
    		   textureData[index] = textureData[index + 2];
    		   textureData[index + 2] = textureColors;
    	   }
    
       type = GL_RGB;
    
       imageWidth = bitmapInfoHeader.biWidth;
       imageHeight = bitmapInfoHeader.biHeight;
    //	fclose(pFile);     // We are done with the file so close it.
    	return textureData;  // Send the image to the function that called this.
    }
    

    bitte helft mir 😞


  • Mod

    nt



  • Hi,

    bin mit dem debugger drüber gegangen, der sagt mir das "textureData" ein BadPtr ist.
    Wie kann ich das korregieren?


  • Mod

    x.read ((char*)&textureData, bitmapInfoHeader.biSizeImage);
    

    das soll sicher heissen

    x.read ((char*)textureData, bitmapInfoHeader.biSizeImage);
    


  • Hi,

    habs geändert sieht schon besser aus, doch die ausgabe ist nicht die selbe wie mit der C version auf dem Monitor.



  • wie sieht es denn auz ?



  • Grauenhaft 😃

    Nein mal ehrlich:
    In der C Variante sehe ich das Bild in 1:1 Qualität und in der C++ Version nichtmal ansatzweise! Alles grau und die ränder mit etwas farbe bedeckt aber sonst, nix.

    Man erkennt nichtmal das es was mit dem bild zu tun hat!



  • Haste dir zur Kontrolle mal die eingelesenen Werte ausgeben lassen? Geprüft ob die Datei überhaupt geöffnet ist?
    Vermutlich werden die Daten falsch eingelesen.


  • Mod

    //    fseek(pFile, header.bfOffBits, SEEK_SET);
        x.seekg (header.bfOffBits, std::ios::cur);
    

    fseek positioniert relativ zum fileanfang, seekg hier aber relativ zur gegenwärtigen streamposition
    also müsste es wohl heissen:

    x.seekg (header.bfOffBits);
    

Anmelden zum Antworten