dgl_ScriptBinding.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. /*! @defgroup ImageFileManipulation Image File Manipulation
  23. @ingroup TorqueScriptFunctions
  24. @{
  25. */
  26. /*! Capture a specific area of the screen
  27. */
  28. ConsoleFunctionWithDocs(CaptureScreenArea, bool, 7, 7, (posX, posY, width, height, fileName, fileType))
  29. {
  30. GLint positionX = dAtoi(argv[1]);
  31. GLint positionY = dAtoi(argv[2]);
  32. U32 width = dAtoi(argv[3]);
  33. U32 height = dAtoi(argv[4]);
  34. FileStream fStream;
  35. if(!fStream.open(argv[5], FileStream::Write))
  36. {
  37. Con::printf("Failed to open file '%s'.", argv[5]);
  38. return false;
  39. }
  40. // Read gl pixels here
  41. glReadBuffer(GL_FRONT);
  42. Point2I extent;
  43. extent.x = width;
  44. extent.y = height;
  45. U8 * pixels = new U8[extent.x * extent.y * 4];
  46. glReadPixels(positionX, positionY, extent.x, extent.y, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  47. GBitmap * bitmap = new GBitmap;
  48. bitmap->allocateBitmap(U32(extent.x), U32(extent.y));
  49. // flip the rows
  50. for(U32 y = 0; y < (U32)extent.y; y++)
  51. dMemcpy(bitmap->getAddress(0, extent.y - y - 1), pixels + y * extent.x * 3, U32(extent.x * 3));
  52. if ( dStrcmp( argv[6], "JPEG" ) == 0 )
  53. bitmap->writeJPEG(fStream);
  54. else if( dStrcmp( argv[6], "PNG" ) == 0)
  55. bitmap->writePNG(fStream);
  56. else
  57. bitmap->writePNG(fStream);
  58. fStream.close();
  59. delete [] pixels;
  60. delete bitmap;
  61. return true;
  62. }
  63. /*! Use the png2jpg function to save a PNG file specified by pngFilename as a similarly named JPEG file with the optionally specified quality.
  64. @param pngFilename The path and file name of the PNG file to convert.
  65. @param quality An optional quality between 0 and 100. The default quality is 90.
  66. @return Returns -1 if the file could not be opened, 0 on other failures, and 1 if the conversion worked
  67. */
  68. ConsoleFunctionWithDocs(png2jpg, ConsoleInt, 2, 3, ( pngFilename, [quality ]? ))
  69. {
  70. extern U32 gJpegQuality;
  71. const char * rgbname = NULL;
  72. const char * alphaname = NULL;
  73. const char * bmpname = argv[1];
  74. if(argc == 3)
  75. gJpegQuality = dAtoi(argv[2]);
  76. else
  77. gJpegQuality = 90;
  78. Con::printf("Converting file: %s", argv[1]);
  79. if (!rgbname)
  80. {
  81. char * buf = new char[dStrlen(bmpname)+32];
  82. dStrcpy(buf,bmpname);
  83. char * pos = dStrstr(buf,".png");
  84. if (!pos)
  85. pos = buf + dStrlen(buf);
  86. dStrcpy(pos,".jpg");
  87. rgbname = buf;
  88. }
  89. if (!alphaname)
  90. {
  91. char * buf = new char[dStrlen(bmpname)+32];
  92. dStrcpy(buf,bmpname);
  93. char * pos = dStrstr(buf,".png");
  94. if (!pos)
  95. pos = buf + dStrlen(buf);
  96. dStrcpy(pos,".alpha.jpg");
  97. alphaname = buf;
  98. }
  99. GBitmap bmp;
  100. FileStream fs;
  101. if (fs.open(bmpname, FileStream::Read) == false) {
  102. Con::printf("Error: unable to open file: %s for reading\n", bmpname);
  103. return -1;
  104. }
  105. if (bmp.readPNG(fs) == false) {
  106. Con::printf("Error: unable to read %s as a .PNG\n", bmpname);
  107. return -1;
  108. }
  109. fs.close();
  110. if (bmp.getFormat() != GBitmap::RGB &&
  111. bmp.getFormat() != GBitmap::RGBA) {
  112. Con::printf("Error: %s is not a 24 or 32-bit .PNG\n", bmpname);
  113. return false;
  114. }
  115. GBitmap * outRGB = NULL;
  116. GBitmap * outAlpha = NULL;
  117. GBitmap workRGB, workAlpha;
  118. if (bmp.getFormat() == GBitmap::RGB)
  119. outRGB = &bmp;
  120. else
  121. {
  122. S32 w = bmp.getWidth();
  123. S32 h = bmp.getHeight();
  124. workRGB.allocateBitmap(w,h,false,GBitmap::RGB);
  125. workAlpha.allocateBitmap(w,h,false,GBitmap::Alpha);
  126. U8 * rgbBits = workRGB.getWritableBits();
  127. U8 * alphaBits = workAlpha.getWritableBits();
  128. U8 * bmpBits = bmp.getWritableBits();
  129. for (S32 i=0; i<w; i++)
  130. {
  131. for (S32 j=0; j<h; j++)
  132. {
  133. rgbBits[i*3 + j*3*w + 0] = bmpBits[i*4 + j*4*w + 0];
  134. rgbBits[i*3 + j*3*w + 1] = bmpBits[i*4 + j*4*w + 1];
  135. rgbBits[i*3 + j*3*w + 2] = bmpBits[i*4 + j*4*w + 2];
  136. alphaBits[i + j*w] = bmpBits[i*4 + j*4*w + 3];
  137. }
  138. }
  139. Con::printf("texture: width=%i, height=%i\n",w,h);
  140. outRGB = &workRGB;
  141. outAlpha = &workAlpha;
  142. }
  143. if (outRGB)
  144. {
  145. FileStream fws;
  146. if (fws.open(rgbname, FileStream::Write) == false)
  147. {
  148. Con::printf("Error: unable to open file: %s for writing\n", rgbname);
  149. return -1;
  150. }
  151. if (dStrstr(rgbname,".png"))
  152. {
  153. if (outRGB->writePNG(fws) == false)
  154. {
  155. fws.close();
  156. Con::printf("Error: couldn't write RGB as a png\n");
  157. return -1;
  158. }
  159. }
  160. else if (outRGB->writeJPEG(fws) == false)
  161. {
  162. Con::printf("Error: couldn't write RGB as a jpg\n");
  163. return -1;
  164. }
  165. fws.close();
  166. }
  167. if (outAlpha)
  168. {
  169. gJpegQuality = 60;
  170. FileStream fws;
  171. if (fws.open(alphaname, FileStream::Write) == false)
  172. {
  173. Con::printf("Error: unable to open file: %s for writing\n", alphaname);
  174. return -1;
  175. }
  176. if (dStrstr(alphaname,".png"))
  177. {
  178. if (outAlpha->writePNG(fws) == false)
  179. {
  180. fws.close();
  181. Con::printf("Error: couldn't write alpha as a png\n");
  182. return -1;
  183. }
  184. }
  185. else if (outAlpha->writeJPEG(fws) == false)
  186. {
  187. Con::printf("Error: couldn't write alpha as a jpg\n");
  188. return -1;
  189. }
  190. fws.close();
  191. }
  192. return(0);
  193. }
  194. /*! @} */ // end group ImageFileManipulation