Changeset 4881 for nanovis/branches/1.1/FlowCmd.cpp
- Timestamp:
- Dec 19, 2014 3:49:27 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
nanovis/branches/1.1/FlowCmd.cpp
r4874 r4881 1065 1065 }; 1066 1066 1067 static int 1068 ppmWriteToFile(Tcl_Interp *interp, const char *path, FlowVideoSwitches *switchesPtr) 1069 { 1070 int f; 1071 1067 static bool 1068 ppmWriteToFile(const char *path, const unsigned char *imgData, 1069 int width, int height) 1070 { 1072 1071 /* Open the named file for writing. */ 1073 f = creat(path, 0600); 1074 if (f < 0) { 1075 Tcl_AppendResult(interp, "can't open temporary image file \"", path, 1076 "\": ", Tcl_PosixError(interp), (char *)NULL); 1077 return TCL_ERROR; 1072 int fdOut = creat(path, 0600); 1073 if (fdOut < 0) { 1074 ERROR("can't open temporary image file \"%s\": %s", path, strerror(errno)); 1075 return false; 1078 1076 } 1079 1077 // Generate the PPM binary file header 1080 1078 char header[200]; 1081 1079 #define PPM_MAXVAL 255 1082 sprintf(header, "P6 %d %d %d\n", switchesPtr->width, switchesPtr->height,1080 sprintf(header, "P6 %d %d %d\n", width, height, 1083 1081 PPM_MAXVAL); 1084 1082 1085 1083 size_t header_length = strlen(header); 1086 size_t wordsPerRow = ( switchesPtr->width * 24 + 31) / 32;1084 size_t wordsPerRow = (width * 24 + 31) / 32; 1087 1085 size_t bytesPerRow = wordsPerRow * 4; 1088 size_t rowLength = switchesPtr->width * 3;1089 size_t numRecords = switchesPtr->height + 1;1086 size_t rowLength = width * 3; 1087 size_t numRecords = height + 1; 1090 1088 1091 1089 struct iovec *iov; … … 1098 1096 // Now add the image data, reversing the order of the rows. 1099 1097 int y; 1100 unsigned char *srcRowPtr = NanoVis::screenBuffer;1098 unsigned char *srcRowPtr = const_cast<unsigned char *>(imgData); 1101 1099 /* Reversing the pointers for the image rows. PPM is top-to-bottom. */ 1102 for (y = switchesPtr->height; y >= 1; y--) {1100 for (y = height; y >= 1; y--) { 1103 1101 iov[y].iov_base = srcRowPtr; 1104 1102 iov[y].iov_len = rowLength; 1105 1103 srcRowPtr += bytesPerRow; 1106 1104 } 1107 if (writev(f, iov, numRecords) < 0) { 1108 Tcl_AppendResult(interp, "writing image to \"", path, "\" failed: ", 1109 Tcl_PosixError(interp), (char *)NULL); 1105 if (writev(fdOut, iov, numRecords) < 0) { 1106 ERROR("writing image to \"%s\" failed: %s", path, strerror(errno)); 1110 1107 free(iov); 1111 close(f );1112 return TCL_ERROR;1113 } 1114 close(f );1108 close(fdOut); 1109 return false; 1110 } 1111 close(fdOut); 1115 1112 free(iov); 1116 return TCL_OK; 1117 } 1118 1119 static int 1120 MakeImageFiles(Tcl_Interp *interp, char *tmpFileName, 1121 FlowVideoSwitches *switchesPtr, bool *cancelPtr) 1113 return true; 1114 } 1115 1116 static bool 1117 MakeImageFiles(char *tmpFileName, 1118 int width, int height, int numFrames, 1119 bool *cancelPtr) 1122 1120 { 1123 1121 struct pollfd pollResults; … … 1131 1129 oldHeight = NanoVis::winHeight; 1132 1130 1133 if ( (switchesPtr->width != oldWidth) ||1134 (switchesPtr->height != oldHeight)) {1131 if (width != oldWidth || 1132 height != oldHeight) { 1135 1133 // Resize to the requested size. 1136 NanoVis::resizeOffscreenBuffer( switchesPtr->width, switchesPtr->height);1134 NanoVis::resizeOffscreenBuffer(width, height); 1137 1135 } 1138 1136 NanoVis::resetFlows(); 1139 1137 *cancelPtr = false; 1140 int result = TCL_OK;1138 bool result = true; 1141 1139 size_t length = strlen(tmpFileName); 1142 for (int i = 1; i <= switchesPtr->numFrames; i++) {1140 for (int i = 1; i <= numFrames; i++) { 1143 1141 if (((i & 0xF) == 0) && (poll(&pollResults, 1, timeout) > 0)) { 1144 1142 /* If there's another command on stdin, that means the client is … … 1162 1160 1163 1161 sprintf(tmpFileName + length, "/image%d.ppm", i); 1164 result = ppmWriteToFile(interp, tmpFileName, switchesPtr); 1165 if (result != TCL_OK) { 1162 result = ppmWriteToFile(tmpFileName, NanoVis::screenBuffer, 1163 width, height); 1164 if (!result) { 1166 1165 break; 1167 1166 } 1168 1167 } 1169 if ( (switchesPtr->width != oldWidth)||1170 (switchesPtr->height != oldHeight)) {1168 if (width != oldWidth || 1169 height != oldHeight) { 1171 1170 NanoVis::resizeOffscreenBuffer(oldWidth, oldHeight); 1172 1171 } … … 1193 1192 switchesPtr->bitRate, switchesPtr->frameRate); 1194 1193 TRACE("Enter: %s", cmd); 1195 FILE *f; 1196 f = popen(cmd, "r"); 1194 FILE *f = popen(cmd, "r"); 1197 1195 if (f == NULL) { 1198 1196 Tcl_AppendResult(interp, "can't run ffmpeg: ", … … 1203 1201 size_t total = 0; 1204 1202 for (;;) { 1205 ssize_t numRead;1206 1203 char buffer[BUFSIZ]; 1207 1204 1208 numRead = fread(buffer, sizeof(unsigned char), BUFSIZ, f);1205 ssize_t numRead = fread(buffer, sizeof(unsigned char), BUFSIZ, f); 1209 1206 total += numRead; 1210 1207 if (numRead == 0) { // EOF … … 1287 1284 size_t length = strlen(tmpFileName); 1288 1285 bool canceled = false; 1289 result = MakeImageFiles(interp, tmpFileName, &switches, &canceled); 1286 if (MakeImageFiles(tmpFileName, 1287 switches.width, switches.height, switches.numFrames, 1288 &canceled)) { 1289 result = TCL_OK; 1290 } else { 1291 result = TCL_ERROR; 1292 } 1290 1293 if ((result == TCL_OK) && (!canceled)) { 1291 1294 result = MakeMovie(interp, tmpFileName, token, &switches);
Note: See TracChangeset
for help on using the changeset viewer.