📄 imtab.cpp
字号:
// $image\imtab.cpp 1.5 milbo$ routines for reading files into global array// also has image tagging routines// Warning: this is raw research code -- expect it to be quite messy.// milbo durban dec05#include <windows.h>#include <io.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <signal.h>#include <float.h>#include <math.h>#include <new.h>#include <direct.h>#include <sys/stat.h>#include <fstream.h>#include <iostream.h>#include "mcommon.hpp"#include "image.hpp"#include "imutil.hpp"#include "imtab.hpp"#include "imappend.hpp"#include "imfile.hpp"#include "rgbimutil.hpp"#include "jpegutil.hpp"#include "util.hpp"#include "err.hpp"int ngFiles; // number of files in sgFileschar sgFiles[CONF_nMaxFiles][FLEN]; // array of filenames (without paths)static bool fgTaggedImages[CONF_nMaxFiles]; // set to tag an imagechar const sgFalsePositiveDir[] = "falsePos";//-----------------------------------------------------------------------------static int __cdecl CompareFileNames (const void *pArg1, const void *pArg2){return _stricmp((char *)pArg1, (char *)pArg2);}//-----------------------------------------------------------------------------// Put the filenames specified by sFileSpec into the sgFiles array and update ngFilesvoid GetFileNamesIntoGlobalFileArray (const char sFileSpec[]){char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];long hFile;int iFirstFile = ngFiles;struct _finddata_t FileData;// get directory prefix to sFileSpec if any, because findfirst drops it_splitpath(sFileSpec, drive, dir, fname, ext);// findnext returns files in random order. We want alphabetic so we// have to get all the files first and then sort them, and then use them.if ((hFile = _findfirst(sFileSpec, &FileData)) == -1L) { // try to give a helpful message here _splitpath(sFileSpec, drive, dir, fname, ext); if (ext[1] == 'a') Err("No match for \"%s\" (if first file is a not a .ap file, " "then no files can be .ap files)", sFileSpec); else Err("No match for \"%s\" (which should be a wildcarded file spec)", sFileSpec); }if (strlen(FileData.name) > sizeof(sgFiles[0]) - 3) // 3 may be over rigorous Err("File name %s is too long", FileData.name);//logprintf("File %s\n", FileData.name);strcpy(sgFiles[ngFiles++], FileData.name);while (_findnext(hFile, &FileData) == 0 && !fgUserInt) { //logprintf("File %s\n", FileData.name); if (strlen(FileData.name) > sizeof(sgFiles[0]) - 3) Err("File name %s is too long", FileData.name); strcpy(sgFiles[ngFiles++], FileData.name); if (ngFiles == CONF_nMaxFiles) Err("Too many files matching %s (max is %d)", sFileSpec, CONF_nMaxFiles); }_findclose(hFile);qsort((void *)&sgFiles[iFirstFile], (size_t)(ngFiles-iFirstFile), sizeof(sgFiles[0]), CompareFileNames);}//-----------------------------------------------------------------------------// This is used by the trainer to notify us of false positives on non-facesvoid TagImage (int iImage){if (iImage >= ngFiles || iImage < 0) SysErr("TagImage iImage %d ngFiles %d", iImage, ngFiles);fgTaggedImages[iImage] = true;}//-----------------------------------------------------------------------------bool fImageTagged (int iImage){return fgTaggedImages[iImage];}//-----------------------------------------------------------------------------// Convert parts of a path that would be illegal in a fname to '_'//// sAltFileSpec is what to use instead, if the munged result is too short. // This can happen if sFileSpec is something like *.bmp, which munged becomes just "_"// Use NULL for sAltFileSpec if you don't need this mechanismchar *sMungeFileSpec (const char sFileSpec[], const char sAltFileSpec[]){static char *sPath;if (!sPath) sPath = (char *)malloc(PLEN);char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];char *p0;_splitpath(sFileSpec, drive, dir, fname, ext); // remove file extension_makepath(sPath, drive, dir, fname, "");char *p = sPath;// special case: discard leading disk name eg "D:"if (sPath[1] == ':' && strlen(sPath) > 2) p0 = sPath + 2;else p0 = sPath;while (*p) // create the file name by converting bad chars to _ { if (strchr(" ;=!|/&:{}~*?\\`\"", *p)) *p = '_'; p++; }p--;while (*p == '_') // discard trailing _ p--;*++p = 0;while (*p0++ == '_') // discard leading _ ;p0--;if (strlen(p0) < 5 && sAltFileSpec) { p = sMungeFileSpec(sAltFileSpec, NULL); // recursive call return p; }return p0;}//-----------------------------------------------------------------------------// Write a .ap file using the images tagged in fgTaggedImagesvoid WriteTaggedImages (const char sOrigApName[], const char sMsg[], Image aImages[], int iTagArrayStart, int nTagArrayEnd, bool fGenerateUniqueFname){const char sFalsePositiveExt[] = "_fp";if (fgUserInt) return;// first check that there at least one tagged imageint nTaggedImages = 0;for (int iImage = iTagArrayStart; iImage < nTagArrayEnd; iImage++) if (fgTaggedImages[iImage]) nTaggedImages++;if (nTaggedImages == 0) lprintf("No %s recorded for %s\n", sMsg, sOrigApName);else { // Generate a decent file name // For now assume here for file naming that the tagged files are false positives char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT]; char sPath[PLEN]; _splitpath(sOrigApName, drive, dir, fname, ext); char *s = sMungeFileSpec(fname, sOrigApName); sprintf(sPath, "%s/%s%s.ap", sgFalsePositiveDir, s, sFalsePositiveExt); if (fGenerateUniqueFname && _access(sPath, 0) == 0) // _access checks for file existence { int iTag = 0; do sprintf(sPath, "%s/%s%2.2d%s.ap", sgFalsePositiveDir, s, iTag++, sFalsePositiveExt); while (_access(sPath, 02) == 0); } lprintf("Writing %d %s to %s ", nTaggedImages, sMsg, sPath); WriteAppendedImageFile1(sPath, aImages, 0, true, iTagArrayStart, nTagArrayEnd); lprintf("\n"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -