void MapBackground::CreateMapTileImage(MapTile* pMapTile, void* gResult, uint32 gResultLen, bool isJpg) { if (!IsTileVisible(pMapTile)) { return; } CIwImage image; if (!isJpg) { //pMapTile->pTexture = g_pNotFoundTexture; s3eFile* tempfile = s3eFileOpenFromMemory((void*)gResult, gResultLen); image.ReadFile(tempfile); //image.LoadFromFile("tiles/r0302322130033033130.png"); if (image.GetWidth()) { pMapTile->pTexture = new CIwTexture; pMapTile->pTexture->CopyFromImage(&image); pMapTile->pTexture->Upload(); } s3eFileClose(tempfile); } else { JPEGImage(gResult, gResultLen, image); pMapTile->pTexture = new CIwTexture; pMapTile->pTexture->CopyFromImage(&image); pMapTile->pTexture->Upload(); } }
bool CCImage::_initWithPngData(void * pData, int nDatalen) { IW_CALLSTACK("CCImage::_initWithPngData"); bool bRet = false; s3eFile* pFile = s3eFileOpenFromMemory(pData, nDatalen); IwAssert(GAME, pFile); CIwImage *image = NULL; image = new CIwImage; image->ReadFile( pFile); s3eFileClose(pFile); // init image info m_bPreMulti = true; m_bHasAlpha = image->HasAlpha(); unsigned int bytesPerComponent = 3; if (m_bHasAlpha) { bytesPerComponent = 4; } m_nHeight = (unsigned int)image->GetHeight(); m_nWidth = (unsigned int)image->GetWidth(); m_nBitsPerComponent = (unsigned int)image->GetBitDepth()/bytesPerComponent; tImageSource imageSource; imageSource.data = (unsigned char*)pData; imageSource.size = nDatalen; imageSource.offset = 0; m_pData = new unsigned char[m_nHeight * m_nWidth * bytesPerComponent]; unsigned int bytesPerRow = m_nWidth * bytesPerComponent; if(m_bHasAlpha) { unsigned char *src = NULL; src = (unsigned char *)image->GetTexels(); unsigned char *tmp = (unsigned char *) m_pData; for(unsigned int i = 0; i < m_nHeight*bytesPerRow; i += bytesPerComponent) { *(tmp + i + 0) = (*(src + i + 0) * *(src + i + 3) + 1) >> 8; *(tmp + i + 1) = (*(src + i + 1) * *(src + i + 3) + 1) >> 8; *(tmp + i + 2) = (*(src + i + 2) * *(src + i + 3) + 1) >> 8; *(tmp + i + 3) = *(src + i + 3); } } else { for (int j = 0; j < (m_nHeight); ++j)
bool Texture2DLoader::loadFromMemory(IResource * outResource, uint64 size, const void* data) { Texture2D * texPtr = (Texture2D*)outResource; assert(texPtr != nullptr); CIwImage image; s3eFile * f = s3eFileOpenFromMemory((void*)data, (size_t)size); image.ReadFile(f); s3eFileClose(f); return texPtr->uploadToGPU(image); }
bool CIwGameFile::Open(void* memory_buffer, int memory_buffer_len) { FileAvailable = false; InMemory = true; File = s3eFileOpenFromMemory(memory_buffer, memory_buffer_len); if (File == NULL) { #if defined(_DEBUG) s3eFileGetError(); CIwGameError::LogError("Error: CIwGameFile::Open(memory): ", s3eFileGetErrorString()); #endif // _DEBUG Error = ErrorOpenFailed; return false; } FileAvailable = true; return true; }
bool COggVorbisFileHelper::init( std::string fin_str,bool bResample /*= true*/,int nResQuality/*=0*/, char* pData /*= NULL*/, uint32 iSize /*= 0*/) { cleanup(); #if defined(HAVE_PTHREAD) pthread_mutex_lock(&mutex1); #endif nSoundChannel = s3eSoundGetFreeChannel(); if(nSoundChannel == -1) { m_strLastError.clear(); m_strLastError = "Cannot open a sound channel."; s3eDebugTracePrintf("%s\n",m_strLastError.c_str()); cleanup(); #if defined(HAVE_PTHREAD) pthread_mutex_unlock(&mutex1); #endif return false; } s3eSoundChannelRegister(nSoundChannel, S3E_CHANNEL_GEN_AUDIO, GenerateAudioCallback, this); s3eSoundChannelRegister(nSoundChannel, S3E_CHANNEL_END_SAMPLE, EndSampleCallback, this); ov_callbacks callbacks; callbacks.read_func = read_func; callbacks.seek_func = seek_func; callbacks.close_func = close_func; callbacks.tell_func = tell_func; if (pData != NULL) { oggvorbis_filein = s3eFileOpenFromMemory(pData, iSize); } else { if(false /*oggvorbis_filein != NULL*/) { if(s3eFileClose(oggvorbis_filein) == S3E_RESULT_ERROR) { m_strLastError.clear(); m_strLastError = "Cannot close old file"; s3eDebugTracePrintf("%s\n",m_strLastError.c_str()); cleanup(); #if defined(HAVE_PTHREAD) pthread_mutex_unlock(&mutex1); #endif return false; } } oggvorbis_filein = s3eFileOpen(fin_str.c_str(),"rb"); } if(oggvorbis_filein == NULL) { m_strLastError.clear(); m_strLastError = "Cannot open file " + fin_str; s3eDebugTracePrintf("%s\n",m_strLastError.c_str()); cleanup(); #if defined(HAVE_PTHREAD) pthread_mutex_unlock(&mutex1); #endif return false; } if(ov_open_callbacks(oggvorbis_filein, &vf, NULL, 0, callbacks) < 0) { m_strLastError.clear(); m_strLastError = "Input does not appear to be an Ogg bitstream."; s3eDebugTracePrintf("%s\n",m_strLastError.c_str()); cleanup(); #if defined(HAVE_PTHREAD) pthread_mutex_unlock(&mutex1); #endif return false; } /* Throw the comments plus a few lines about the bitstream we're decoding */ { char **ptr=ov_comment(&vf,-1)->user_comments; vorbis_info *vi=ov_info(&vf,-1); //while(*ptr) //{ // fprintf(stderr,"%s\n",*ptr); // ++ptr; //} total_samples = ov_pcm_total(&vf,-1); time_length = ov_time_total_func(&vf,-1); nChannels = vi->channels; nRate = vi->rate; s3eSoundChannelSetInt(nSoundChannel, S3E_CHANNEL_RATE, nRate); nOutputRate = s3eSoundGetInt(S3E_SOUND_OUTPUT_FREQ); int gcd = GCD(nRate, nOutputRate); nW = nRate / gcd; nL = nOutputRate / gcd; dResampleFactor = (float)nOutputRate / (float)vi->rate; // 0 - 4.0 ? int err; bEnableResampling = bResample; nResampleQuality = nResQuality; if(bEnableResampling) { if(res_contL) speex_resampler_destroy(res_contL); res_contL = speex_resampler_init(1,nRate,nOutputRate,nResampleQuality,&err); if(res_contR) speex_resampler_destroy(res_contR); res_contR = speex_resampler_init(1,nRate,nOutputRate,nResampleQuality,&err); if(err != RESAMPLER_ERR_SUCCESS) { m_strLastError.clear(); m_strLastError = "Cannot start resampler."; s3eDebugTracePrintf("%s\n",m_strLastError.c_str()); cleanup(); #if defined(HAVE_PTHREAD) pthread_mutex_unlock(&mutex1); #endif return false; } } else { int fs = min(nRate, nOutputRate); double fc = (fs/2) / (double)nOutputRate; // half the input sample rate (eg nyquist limit of input) // Generate filter coefficients wsfirLP(dFilterCoefficients, nFilterCoefficients, W_BLACKMAN, fc); if(dResampleFactor != 1) s3eDebugErrorShow(S3E_MESSAGE_CONTINUE,"Resample factor not 1 but resampling disabled"); } s3eDebugTracePrintf("\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate); s3eDebugTracePrintf("\nDecoded length: %ld samples\n",(long)total_samples); s3eDebugTracePrintf("Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor); s3eDebugTracePrintf("Resampling by rational factor %d / %d", nW, nL); } bStopDecoding = false; nStatus = OH_READY; #if defined(HAVE_PTHREAD) pthread_mutex_unlock(&mutex1); #endif return true; }
//------------------------------------------------------------------------- CIwSoundData* CIwSoundWAV::Create(const CIwStringL& pathname, void* buffer, u_int file_size) { IW_CALLSTACK("CIwSoundWAV::Create") CIwSoundData* pData = NULL; // Object to return // Open file s3eFile* pFile = NULL; if (buffer != NULL) pFile = s3eFileOpenFromMemory(buffer, file_size); else pFile = IwFileOpenPrefixed(pathname.c_str(), "rb"); IwAssertMsg(SOUND, pFile, ("Could not load file %s", pathname.c_str())); if (!pFile) return NULL; // Read RIFF header - Gives the file size and checks that this is a WAVE // file as expected IwRIFFHeader riffHeader; if ((s3eFileRead(&riffHeader, sizeof(IwRIFFHeader), 1, pFile) != 1) || (strncmp(riffHeader.typeID, "RIFF", 4) != 0) || (strncmp(riffHeader.subTypeID, "WAVE", 4) != 0)) { IwAssertMsg(SOUND, false, ("Invalid header in %s (RIFF Header)", pathname.c_str())); s3eFileClose(pFile); return NULL; } // Read in RIFF chunks until we reach the end of the file // Read the RIFF chunk header. This tells us what type of chunk follows. IwRIFFChunkHeader chunkHeader; bool readData = false; uint32 fileSize = s3eFileGetSize(pFile); while (ReadChunkHeader(chunkHeader, *(s3eFile*)pFile)) { uint32 chunkStartPos = s3eFileTell(pFile); // Next action depends on chunk type. The order of this is important and we may fail // if an unexpected chunk type is found if (!strncmp(chunkHeader.typeID, "fmt ", 4)) { // Read WAVE info chunk if (!ReadChunkFormat(pathname, chunkHeader, pData, *(s3eFile*)pFile)) { s3eFileClose(pFile); return NULL; } } else if (!strncmp(chunkHeader.typeID, "data", 4)) { if (!ReadChunkData(pathname, chunkHeader, pData, *(s3eFile*)pFile)) { s3eFileClose(pFile); return NULL; } readData = true; } else if (!strncmp(chunkHeader.typeID, "fact", 4)) { if (!ReadChunkFact(pathname, chunkHeader, pData, *(s3eFile*)pFile)) { s3eFileClose(pFile); return NULL; } } else { // Unknown chunk type // Make a proper string from the chunk type info char typeID[5]; strncpy(typeID, chunkHeader.typeID, 4); typeID[4] = 0; // Terminate const char* g_IgnoreTypes = "LIST" //LIST is just copyright info etc. "DISP"; //DISP seems to be info about what package exported it IwAssertMsg(SOUND, strstr(g_IgnoreTypes, typeID), ("Unhandled chunk type '%s' in %s. Ignoring this data.", typeID, pathname.c_str())); } // Exit if at end of file if (chunkStartPos + chunkHeader.length >= fileSize) break; // Move to next chunk s3eFileSeek(pFile, chunkStartPos + chunkHeader.length, S3E_FILESEEK_SET); } // Check that we have read the sample data IwAssertMsg(SOUND, readData, ("No data chunk read in %s", pathname.c_str())); s3eFileClose(pFile); return pData; }
CxFile CIwGamePlatformFileMarm::OpenFromMemory(void* memory, uint num_bytes) { return (CxFile*)s3eFileOpenFromMemory(memory, num_bytes); }
bool CCImage::_initWithPngData(void * pData, int nDatalen) { IW_CALLSTACK("CCImage::_initWithPngData"); bool bRet = false; s3eFile* pFile = s3eFileOpenFromMemory(pData, nDatalen); IwAssert(GAME, pFile); png_byte pngsig[PNGSIGSIZE]; bool is_png = false; s3eFileRead((char*)pngsig, PNGSIGSIZE, 1, pFile); is_png = png_sig_cmp(pngsig, 0, PNGSIGSIZE) == 0 ? true : false; if (!is_png) return false; png_structp pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!pngPtr) return false; png_infop infoPtr = png_create_info_struct(pngPtr); if (!infoPtr) return false; png_bytep* rowPtrs = NULL; m_pData = NULL; if (setjmp(png_jmpbuf(pngPtr))) { png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0); if (rowPtrs != NULL) delete [] rowPtrs; if (m_pData != NULL) delete [] m_pData; CCLog("ERROR: An error occured while reading the PNG file"); return false; } png_set_read_fn(pngPtr, pFile, userReadData); png_set_sig_bytes(pngPtr, PNGSIGSIZE); png_read_info(pngPtr, infoPtr); png_uint_32 bitdepth = png_get_bit_depth(pngPtr, infoPtr); png_uint_32 channels = png_get_channels(pngPtr, infoPtr); png_uint_32 color_type = png_get_color_type(pngPtr, infoPtr); // Convert palette color to true color if (color_type ==PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(pngPtr); // Convert low bit colors to 8 bit colors if (png_get_bit_depth(pngPtr, infoPtr) < 8) { if (color_type==PNG_COLOR_TYPE_GRAY || color_type==PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_1_2_4_to_8(pngPtr); else png_set_packing(pngPtr); } if (png_get_valid(pngPtr, infoPtr, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(pngPtr); // Convert high bit colors to 8 bit colors if (bitdepth == 16) png_set_strip_16(pngPtr); // Convert gray color to true color if (color_type==PNG_COLOR_TYPE_GRAY || color_type==PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(pngPtr); // Update the changes png_read_update_info(pngPtr, infoPtr); // init image info m_bPreMulti = true; unsigned int bytesPerComponent = png_get_channels(pngPtr, infoPtr); m_bHasAlpha = (bytesPerComponent == 4 ? true : false); m_nHeight = (unsigned int)png_get_image_height(pngPtr, infoPtr); m_nWidth = (unsigned int) png_get_image_width(pngPtr, infoPtr); m_nBitsPerComponent = (unsigned int)png_get_bit_depth(pngPtr, infoPtr); m_pData = new unsigned char[m_nHeight * m_nWidth * bytesPerComponent]; unsigned int bytesPerRow = m_nWidth * bytesPerComponent; { unsigned char *ptr = m_pData; rowPtrs = new png_bytep[m_nHeight]; for (int i = 0; i < m_nHeight; i++) { int q = (i) * bytesPerRow; rowPtrs[i] = (png_bytep)m_pData + q; } png_read_image(pngPtr, rowPtrs); delete[] (png_bytep)rowPtrs; png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0); s3eFileClose(pFile); pFile = 0; } // premultiplay if alpha if(m_bHasAlpha) for(unsigned int i = 0; i < m_nHeight*bytesPerRow; i += bytesPerComponent){ *(m_pData + i + 0) = (*(m_pData + i + 0) * *(m_pData + i + 3) + 1) >> 8; *(m_pData + i + 1) = (*(m_pData + i + 1) * *(m_pData + i + 3) + 1) >> 8; *(m_pData + i + 2) = (*(m_pData + i + 2) * *(m_pData + i + 3) + 1) >> 8; *(m_pData + i + 3) = *(m_pData + i + 3); } bRet = true; return bRet; }