pngopt.cpp
上传用户:jnfxsk
上传日期:2022-06-16
资源大小:3675k
文件大小:4k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /*
  2. ** Haaf's Game Engine 1.81
  3. ** Copyright (C) 2003-2008, Relish Games
  4. ** hge.relishgames.com
  5. **
  6. ** PNG Images Optimizer
  7. */
  8. #include "pngopt.h"
  9. HGE *hge = 0;
  10. struct color
  11. {
  12. unsigned char r;
  13. unsigned char g;
  14. unsigned char b;
  15. unsigned char a;
  16. };
  17. struct filelist
  18. {
  19. char filename[256];
  20. filelist* next;
  21. };
  22. filelist *files=0;
  23. extern bool Write32BitPNGWithPitch(FILE* fp, void* pBits, bool bNeedAlpha, int nWidth, int nHeight, int nPitch);
  24. bool convert(char *filename);
  25. int main(int argc, char* argv[])
  26. {
  27. HANDLE hSearch;
  28. WIN32_FIND_DATA SearchData;
  29. int nfiles=0;
  30. bool done=false;
  31. char *buf, filename[256];
  32. filelist *newFile, *nextFile;
  33. printf("nPNG Optimizer v0.2nCopyright (C) 2003-2008, Relish Gamesnn");
  34. if(argc!=2)
  35. {
  36. printf("Usage: PNGOPT.EXE <wildcard>nn");
  37. printf("The image data of all files found by the specified wildcardn");
  38. printf("will be expanded beneath the alpha channel boundary.n");
  39. printf("So images will render without artifacts when scaled, stretched,n");
  40. printf("rotated or rendered into not integer coordinates.nn");
  41. return 0;
  42. }
  43. else
  44. {
  45. hSearch=FindFirstFile(argv[1], &SearchData);
  46. nextFile=0;
  47. for(;;)
  48. {
  49. if(hSearch==INVALID_HANDLE_VALUE || done)
  50. {
  51. FindClose(hSearch);
  52. break;
  53. }
  54. if(!(SearchData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  55. {
  56. strcpy(filename, argv[1]);
  57. buf=strrchr(filename, '\');
  58. if(!buf) buf=filename; else buf++;
  59. strcpy(buf,SearchData.cFileName);
  60. newFile=new filelist;
  61. strcpy(newFile->filename,filename);
  62. newFile->next=0;
  63. if(nextFile) nextFile->next=newFile;
  64. else files=newFile;
  65. nextFile=newFile;
  66. }
  67. done=!FindNextFile(hSearch, &SearchData);
  68. }
  69. hge=hgeCreate(HGE_VERSION);
  70. hge->System_SetState(HGE_USESOUND, false);
  71. hge->System_SetState(HGE_WINDOWED, true);
  72. hge->System_SetState(HGE_SCREENWIDTH, 320);
  73. hge->System_SetState(HGE_SCREENHEIGHT, 200);
  74. hge->System_SetState(HGE_SHOWSPLASH, false);
  75. if(!hge->System_Initiate())
  76. {
  77. hge->Release();
  78. printf("nCan't initiate HGE.nn",nfiles);
  79. return 0;
  80. }
  81. newFile=files;
  82. while(newFile)
  83. {
  84. if(convert(newFile->filename)) nfiles++;
  85. nextFile=newFile->next;
  86. delete newFile;
  87. newFile=nextFile;
  88. }
  89. hge->System_Shutdown();
  90. hge->Release();
  91. printf("n%d image(s) successfully optimized.nn",nfiles);
  92. return 0;
  93. }
  94. }
  95. bool convert(char *filename)
  96. {
  97. HTEXTURE tex;
  98. int width, height, pitch;
  99. color *buf;
  100. int i,j,k,l;
  101. int r,g,b;
  102. int count;
  103. FILE *fp=0;
  104. printf("%s - ", filename);
  105. tex = hge->Texture_Load(filename);
  106. if(!tex) { printf("Can't load texture.n"); return false; }
  107. width  = hge->Texture_GetWidth(tex, true);
  108. height = hge->Texture_GetHeight(tex, true);
  109. pitch  = hge->Texture_GetWidth(tex, false);
  110. buf=(color *)hge->Texture_Lock(tex, false);
  111. if(!buf) { printf("Can't lock texture.n"); return false; }
  112. for(i=0; i<height; i++)
  113. for(j=0; j<width; j++)
  114. if(!buf[i*pitch+j].a)
  115. {
  116. count = 0;
  117. r=g=b = 0;
  118. for(k=-1; k<=1; k++)
  119. for(l=-1; l<=1; l++)
  120. if(i+k >= 0 && i+k < height &&
  121.    j+l >= 0 && j+l < width &&
  122.    buf[(i+k)*pitch + (j+l)].a)
  123. {
  124. r += buf[(i+k)*pitch + (j+l)].r;
  125. g += buf[(i+k)*pitch + (j+l)].g;
  126. b += buf[(i+k)*pitch + (j+l)].b;
  127. count++;
  128. }
  129. if(count)
  130. {
  131. buf[i*pitch+j].r = unsigned char(r / count);
  132. buf[i*pitch+j].g = unsigned char(g / count);
  133. buf[i*pitch+j].b = unsigned char(b / count);
  134. }
  135. }
  136. fp=fopen(filename,"wb");
  137. if(!fp)
  138. {
  139. hge->Texture_Unlock(tex);
  140. printf("Can't write to file.n");
  141. return false;
  142. }
  143. if(!Write32BitPNGWithPitch(fp, buf, true, width, height, pitch))
  144. {
  145. hge->Texture_Unlock(tex);
  146. fclose(fp);
  147. printf("Error writing data.n");
  148. return false;
  149. }
  150. fclose(fp);
  151. hge->Texture_Unlock(tex);
  152. printf("Okn");
  153. return true;
  154. }