/** Function to create a given directory. @internalComponent @released @param aSrcPath - path of the directory that needs to be created. @param aDelimiter - delimiter. */ void ImageReader::CreateSpecifiedDir(char* aSrcPath,const char* aDelimiter) { char* currWorkingDir = new char[_MAX_BUFFER_SIZE_]; string origPath; origPath.assign(aSrcPath); // get the current working directory and store in buffer. if( GETCWD(currWorkingDir,_MAX_BUFFER_SIZE_) == NULL ) { // throw an exception if unable to get current working directory information. throw ImageReaderException((char*)ImageReader::iImgFileName.c_str(), "Failed to get the current working directory"); } else { char* cPtr = strtok(aSrcPath,aDelimiter); // check whether cPtr is a drive or a directory. if(IsDrive(cPtr)) { // if yes, then change the directory to cPtr. string changeToDrive ; changeToDrive.assign(cPtr); changeToDrive.append(aDelimiter); // change the current working directory to the specified directory. if( CHDIR(changeToDrive.c_str()) ) { // throw an exception if unable to change the directory specified. throw ImageReaderException((char*)ImageReader::iImgFileName.c_str(), "Failed to change to the directory specified"); } } else { // if not,then create a cPtr directory. MKDIR(cPtr); // change the current working directory to cPtr. int r = CHDIR(cPtr); (void)r; } // repeat till cPtr is NULL. while (cPtr!=NULL) { if ((cPtr = strtok(NULL,aDelimiter)) != NULL) { // create the directory. MKDIR(cPtr); // change current working directory. int r = CHDIR(cPtr); (void)r; } } // revert back the working directory. int r = CHDIR(currWorkingDir); (void)r; // replace the source path with the original path information. strcpy(aSrcPath,origPath.c_str()); delete[] currWorkingDir; } }
void PngReader::init() { FILE *fp=fopen(fileName_.c_str(),"r"); if (!fp) throw ImageReaderException("cannot open image file "+fileName_); png_byte header[8]; memset(header,0,8); fread(header,1,8,fp); int is_png=!png_sig_cmp(header,0,8); if (!is_png) { fclose(fp); throw ImageReaderException(fileName_ + " is not a png file"); } png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,0,0,0); if (!png_ptr) { fclose(fp); throw ImageReaderException("failed to allocate png_ptr"); } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr,0,0); fclose(fp); throw ImageReaderException("failed to create info_ptr"); } png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr,8); png_read_info(png_ptr, info_ptr); png_uint_32 width, height; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth_, &color_type_,0,0,0); width_=width; height_=height; std::cout<<"bit_depth="<<bit_depth_<<" color_type="<<color_type_<<std::endl; png_destroy_read_struct(&png_ptr,&info_ptr,0); fclose(fp); }
void PngReader::read(unsigned x0, unsigned y0,ImageData32& image) { FILE *fp=fopen(fileName_.c_str(),"r"); if (!fp) throw ImageReaderException("cannot open image file "+fileName_); png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,0,0,0); if (!png_ptr) { fclose(fp); throw ImageReaderException("failed to allocate png_ptr"); } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr,0,0); fclose(fp); throw ImageReaderException("failed to create info_ptr"); } png_init_io(png_ptr, fp); png_read_info(png_ptr, info_ptr); if (color_type_ == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr); if (color_type_ == PNG_COLOR_TYPE_GRAY && bit_depth_ < 8) png_set_expand(png_ptr); if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr); if (bit_depth_ == 16) png_set_strip_16(png_ptr); if (color_type_ == PNG_COLOR_TYPE_GRAY || color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr); // quick hack -- only work in >=libpng 1.2.7 png_set_add_alpha(png_ptr,1,1); double gamma; if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, 2.2, gamma); png_read_update_info(png_ptr, info_ptr); //START read image rows unsigned w=std::min((unsigned)image.width(),width_); unsigned h=std::min((unsigned)image.height(),height_); unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr); unsigned char* row= new unsigned char[rowbytes]; for (unsigned i=0;i<height_;++i) { png_read_row(png_ptr,row,0); if (i>=y0 && i<h) { image.setRow(i-y0,(unsigned*) &row[x0],w); } } //END delete [] row; png_read_end(png_ptr,0); png_destroy_read_struct(&png_ptr, &info_ptr,0); fclose(fp); }
/** Function to extract specified file from a given image. @internalComponent @released @param aOffset - starting offset of the file in the image. @param aSize - size of the file in the image. @param aFileName - name of the file. @param aPath - full path of the file inside image. @param aFilePath - path where file has to be extracted. */ void ImageReader::ExtractFile(TUint aOffset,TInt aSize,const char* aFileName,const char* aPath,char* aFilePath,char* aData) { // concatenate path where specified file needs to be extracted with // path where file is located in the image. string fullPath( aFilePath ); string delimiter( "\\" ); string appStr( "\\\\" ); fullPath.append( aPath ); // replace all the occurrence of slash with double slash. FindAndInsertString( fullPath, delimiter, delimiter ); // now terminate the string with double slash. fullPath.append( appStr ); // create specified directory where file needs to be extracted. CreateSpecifiedDir( &fullPath[0], appStr.c_str() ); // concatenate path information with the filename fullPath.append( aFileName ); // create an output stream to extract the specified file. ofstream outfile (fullPath.c_str(), ios::out | ios::binary); // create an input stream by opening the specified image file. ifstream infile(ImageReader::iImgFileName.c_str(),ios::in|ios::binary); //declare a buffer to store the data. char* buffer = new char[aSize]; if(aData != NULL) { memcpy(buffer, aData + aOffset, aSize); } else if(infile.is_open()) { // place the get pointer for the current input stream to offset bytes away from origin. infile.seekg(aOffset,ios::beg); //read number of bytes specified by the variable size //from the stream and place it on to buffer. infile.read(buffer,aSize); //close the input stream after reading. infile.close(); } else { throw ImageReaderException(ImageReader::iImgFileName.c_str(), "Failed to open the image file"); } if(outfile.is_open()) { //writes number of bytes specified by the variable size //from buffer to the current output stream. outfile.write(buffer,aSize); //close the output stream after writing. outfile.close(); } else { throw ImageReaderException(aFileName, "Failed to extract the file"); } //delete the buffer. delete[] buffer; }
/** Function to extract individual or a subset of file. @internalComponent @released @param aData - ROM/ROFS image buffer pointer. */ void ImageReader::ExtractFileSet(char* aData) { FILEINFOMAP fileInfoMap; string dirSep(DIR_SEPARATOR), backSlash("\\"), Pattern; TUint extfileCount = 0, noWcardFlag = 0, pos; //Get the filelist map GetFileInfo(fileInfoMap); //Check for wildcards pos = iPattern.rfind("\\"); if(pos == string::npos) { pos = iPattern.rfind("/"); if(pos == string::npos) pos = 0; } pos = iPattern.find_first_of("*?", pos); if(pos == string::npos) { noWcardFlag = 1; } //Process the map if(fileInfoMap.size() > 0) { FILEINFOMAP::iterator begin = fileInfoMap.begin(); FILEINFOMAP::iterator end = fileInfoMap.end(); // Replace all backslashes with forward slashes Pattern = iPattern; FindAndReplaceString(Pattern, backSlash, dirSep); // Insert root directory at the beginning if it is not there pos = Pattern.find_first_not_of(" ", 0); if(pos != string::npos) { if(Pattern.at(pos) != *DIR_SEPARATOR) Pattern.insert(pos, dirSep); } // Assign CWD for destination path if it is empty if(ImageReader::iZdrivePath.empty()) ImageReader::iZdrivePath.assign("."); while(begin != end) { int status = 0; PFILEINFO pInfo = 0; string fileName = (*begin).first; pInfo = (*begin).second; // First match status = FileNameMatch(Pattern, fileName, (iDisplayOptions & RECURSIVE_FLAG)); // If no match if((!status) && noWcardFlag) { string newPattern(Pattern); // Add * at the end if(newPattern.at(Pattern.length()-1) != *DIR_SEPARATOR) { newPattern.append(DIR_SEPARATOR); } newPattern += "*"; status = FileNameMatch(newPattern, fileName, (iDisplayOptions & RECURSIVE_FLAG)); // If it matches update the pattern and reset wildcard flag if(status) { Pattern = newPattern; noWcardFlag = 0; } } if(status) { // Extract the file // Separarate the path and file name string fullPath = fileName.substr(0, fileName.rfind(DIR_SEPARATOR)); string file = fileName.substr(fileName.rfind(DIR_SEPARATOR)+1, fileName.length()); FindAndReplaceString(fullPath, dirSep, backSlash); // Extract only those files exists in the image if(pInfo->iSize && pInfo->iOffset) { ExtractFile(pInfo->iOffset, pInfo->iSize, file.c_str(), fullPath.c_str() , &ImageReader::iZdrivePath[0], aData); extfileCount++; } } if(pInfo) delete pInfo; ++begin; } fileInfoMap.clear(); } // Throw error if the extracted file count is zero if(!extfileCount) { throw ImageReaderException((char*)ImageReader::iImgFileName.c_str(), "No matching files found for the given pattern"); } }