int write_16ubit (wvStream * in, U16 out) { guint16 cpy = (guint16) TO_LE_16 (out); int nwr = 0; if (in->kind == GSF_STREAM) { #if 0 nwr = (int) in->stream.gsf_stream->write (in->stream.gsf_stream, (guint8 *) & cpy, 16); #endif } else if (in->kind == FILE_STREAM) { nwr = (int) fwrite (&cpy, sizeof (guint16), 1, in->stream.file_stream); } else{ nwr = 2; *((U16 *) (in->stream.memory_stream->mem + in->stream.memory_stream->current))= cpy; in->stream.memory_stream->current+=2; } return nwr; }
bool Console::cmdRawToWav(int argc, const char **argv) { if (argc != 3) { debugPrintf("Use %s <rawFilePath> <wavFileName> to dump a .RAW file to .WAV\n", argv[0]); return true; } Common::File file; if (!_engine->getSearchManager()->openFile(file, argv[1])) { warning("File not found: %s", argv[1]); return true; } Audio::AudioStream *audioStream = makeRawZorkStream(argv[1], _engine); Common::DumpFile output; output.open(argv[2]); output.writeUint32BE(MKTAG('R', 'I', 'F', 'F')); output.writeUint32LE(file.size() * 2 + 36); output.writeUint32BE(MKTAG('W', 'A', 'V', 'E')); output.writeUint32BE(MKTAG('f', 'm', 't', ' ')); output.writeUint32LE(16); output.writeUint16LE(1); uint16 numChannels; if (audioStream->isStereo()) { numChannels = 2; output.writeUint16LE(2); } else { numChannels = 1; output.writeUint16LE(1); } output.writeUint32LE(audioStream->getRate()); output.writeUint32LE(audioStream->getRate() * numChannels * 2); output.writeUint16LE(numChannels * 2); output.writeUint16LE(16); output.writeUint32BE(MKTAG('d', 'a', 't', 'a')); output.writeUint32LE(file.size() * 2); int16 *buffer = new int16[file.size()]; audioStream->readBuffer(buffer, file.size()); #ifndef SCUMM_LITTLE_ENDIAN for (int i = 0; i < file.size(); ++i) buffer[i] = TO_LE_16(buffer[i]); #endif output.write(buffer, file.size() * 2); delete[] buffer; return true; }
static void deDPCM16(byte *soundBuf, Common::SeekableReadStream &audioStream, uint32 n) { int16 *out = (int16 *) soundBuf; int32 s = 0; for (uint32 i = 0; i < n; i++) { byte b = audioStream.readByte(); if (b & 0x80) s -= tableDPCM16[b & 0x7f]; else s += tableDPCM16[b]; s = CLIP<int32>(s, -32768, 32767); *out++ = TO_LE_16(s); } }
void TalkTable_GFF::readString05(Common::SeekableSubReadStreamEndian &huffTree, Common::SeekableSubReadStreamEndian &bitStream, Entry &entry) const { /* Read a string encoded in a Huffman'd bitstream. * * The Huffman tree itself is made up of signed 32bit nodes: * - Positive values are internal nodes, encoding a child index * - Negative values are leaf nodes, encoding an UTF-16 character value * * Kudos to Rick (gibbed) (<http://gib.me/>). */ std::vector<uint16> utf16Str; const uint32 startOffset = entry.strct->getUint(kGFF4HuffTalkStringBitOffset); uint32 index = startOffset >> 5; uint32 shift = startOffset & 0x1F; do { ptrdiff_t e = (huffTree.size() / 8) - 1; while (e >= 0) { bitStream.seek(index * 4); const ptrdiff_t offset = (bitStream.readUint32() >> shift) & 1; huffTree.seek(((e * 2) + offset) * 4); e = huffTree.readSint32(); shift++; index += (shift >> 5); shift %= 32; } utf16Str.push_back(TO_LE_16(0xFFFF - e)); } while (utf16Str.back() != 0); const byte *data = reinterpret_cast<const byte *>(&utf16Str[0]); const size_t size = utf16Str.size() * 2; entry.text = Common::readString(data, size, Common::kEncodingUTF16LE); }
void File::writeWord(uint16 v) { uint16 vTemp = TO_LE_16(v); write(&vTemp, sizeof(uint16)); }
uint16 AGOSEngine_Feeble::to16Wrapper(uint value) { return TO_LE_16(value); }
BitmapPtr Bitmap::code() { debugC(1, kCGEDebugBitmap, "Bitmap::code()"); if (!_m) return false; uint16 cnt; if (_v) { // old X-map exists, so remove it delete[] _v; _v = NULL; } while (true) { // at most 2 times: for (V == NULL) & for allocated block; uint8 *im = _v + 2; uint16 *cp = (uint16 *) _v; int bpl; if (_v) { // 2nd pass - fill the hide table for (uint16 i = 0; i < _h; i++) { _b[i]._skip = 0xFFFF; _b[i]._hide = 0x0000; } } for (bpl = 0; bpl < 4; bpl++) { // once per each bitplane uint8 *bm = _m; bool skip = (bm[bpl] == kPixelTransp); uint16 j; cnt = 0; for (uint16 i = 0; i < _h; i++) { // once per each line uint8 pix; for (j = bpl; j < _w; j += 4) { pix = bm[j]; if (_v && pix != kPixelTransp) { if (j < _b[i]._skip) _b[i]._skip = j; if (j >= _b[i]._hide) _b[i]._hide = j + 1; } if ((pix == kPixelTransp) != skip || cnt >= 0x3FF0) { // end of block cnt |= (skip) ? kBmpSKP : kBmpCPY; if (_v) *cp = TO_LE_16(cnt); // store block description uint16 cp = (uint16 *) im; im += 2; skip = (pix == kPixelTransp); cnt = 0; } if (!skip) { if (_v) *im = pix; im++; } cnt++; } bm += _w; if (_w < kScrWidth) { if (skip) { cnt += (kScrWidth - j + 3) / 4; } else { cnt |= kBmpCPY; if (_v) *cp = TO_LE_16(cnt); cp = (uint16 *) im; im += 2; skip = true; cnt = (kScrWidth - j + 3) / 4; } } } if (cnt && ! skip) { cnt |= kBmpCPY; if (_v) *cp = TO_LE_16(cnt); cp = (uint16 *) im; im += 2; } if (_v) *cp = TO_LE_16(kBmpEOI); cp = (uint16 *) im; im += 2; } if (_v) break; uint16 sizV = (uint16)(im - 2 - _v); _v = new uint8[sizV + _h * sizeof(*_b)]; assert(_v != NULL); _b = (HideDesc *)(_v + sizV); } cnt = 0; for (uint16 i = 0; i < _h; i++) { if (_b[i]._skip == 0xFFFF) { // whole line is skipped _b[i]._skip = (cnt + kScrWidth) >> 2; cnt = 0; } else {
void SegmentMap::findPath(int16 *pointsArray, int16 destX, int16 destY, int16 sourceX, int16 sourceY) { int16 currentRectIndex, destRectIndex; int16 pointsCount; debug(0, "SegmentMap::findPath(fromX: %d; fromY: %d; toX: %d; toY: %d)", sourceX, sourceY, destX, destY); _deadEndPathRectsCount = 0; _closedPathRectsCount = 0; _pathNodesCount = 0; pointsCount = 2; adjustPathPoint(sourceX, sourceY); currentRectIndex = findPathRectAtPoint(sourceX, sourceY); adjustPathPoint(destX, destY); destRectIndex = findPathRectAtPoint(destX, destY); if (currentRectIndex != -1) { if (destRectIndex != currentRectIndex) { while (1) { do { _closedPathRects[_closedPathRectsCount++] = currentRectIndex; currentRectIndex = findNextPathRect(currentRectIndex, destX, destY); _pathNodesCount++; } while (currentRectIndex != -1 && currentRectIndex != destRectIndex); if (currentRectIndex != -1 && currentRectIndex == destRectIndex) break; _deadEndPathRects[_deadEndPathRectsCount++] = _closedPathRects[--_closedPathRectsCount]; _pathNodesCount -= 2; currentRectIndex = _closedPathRects[--_closedPathRectsCount]; } for (int16 i = 0; i < _pathNodesCount; i++) { pointsArray[pointsCount++] = TO_LE_16(_pathNodes[i].y); pointsArray[pointsCount++] = TO_LE_16(_pathNodes[i].x); } } pointsArray[pointsCount++] = TO_LE_16(destY); pointsArray[pointsCount++] = TO_LE_16(destX); pointsArray[0] = 0; pointsArray[1] = TO_LE_16(_pathNodesCount + 1); } debug(0, "SegmentMap::findPath() count = %d", FROM_LE_16(pointsArray[1])); #if 0 // DEBUG: Draw the path we found int sx = sourceX, sy = sourceY; LineData ld; ld.pitch = _vm->_sceneWidth; ld.surf = _vm->_screen->_backScreen; for (int16 i = 0; i < FROM_LE_16(pointsArray[1]) * 2; i+=2) { const int x = FROM_LE_16(pointsArray[3+i]); const int y = FROM_LE_16(pointsArray[2+1]); debug(0, "x = %d; y = %d", x, y); Graphics::drawLine(sx, sy, x, y, 0xFF, plotProc, &ld); sx = x; sy = y; } #endif }
/** * Create a complete RT_GROUP_ICON resource, that can be written to * an `.ico' file without modifications. Returns an allocated * memory block that should be freed with free() once used. * * `root' is the offset in file that specifies the resource. * `base' is the offset that string pointers are calculated from. * `ressize' should point to an integer variable where the size of * the returned memory block will be placed. * `is_icon' indicates whether resource to be extracted is icon * or cursor group. */ byte *Win32ResExtractor::extract_group_icon_cursor_resource(WinLibrary *fi, WinResource *wr, char *lang, int *ressize, bool is_icon) { Win32CursorIconDir *icondir; Win32CursorIconFileDir *fileicondir; byte *memory; int c, offset, skipped; int size; /* get resource data and size */ icondir = (Win32CursorIconDir *)get_resource_entry(fi, wr, &size); if (icondir == NULL) { /* get_resource_entry will print error */ return NULL; } /* calculate total size of output file */ RETURN_IF_BAD_POINTER(NULL, icondir->count); skipped = 0; for (c = 0 ; c < FROM_LE_16(icondir->count) ; c++) { int level; int iconsize; char name[14]; WinResource *fwr; RETURN_IF_BAD_POINTER(NULL, icondir->entries[c]); /*debug("%d. bytes_in_res=%d width=%d height=%d planes=%d bit_count=%d", c, FROM_LE_32(icondir->entries[c].bytes_in_res), (is_icon ? icondir->entries[c].res_info.icon.width : FROM_LE_16(icondir->entries[c].res_info.cursor.width)), (is_icon ? icondir->entries[c].res_info.icon.height : FROM_LE_16(icondir->entries[c].res_info.cursor.height)), FROM_LE_16(icondir->entries[c].plane_count), FROM_LE_16(icondir->entries[c].bit_count));*/ /* find the corresponding icon resource */ snprintf(name, sizeof(name)/sizeof(char), "-%d", FROM_LE_16(icondir->entries[c].res_id)); fwr = find_resource(fi, (is_icon ? "-3" : "-1"), name, lang, &level); if (fwr == NULL) { error("%s: could not find `%s' in `%s' resource.", _fileName.c_str(), &name[1], (is_icon ? "group_icon" : "group_cursor")); return NULL; } if (get_resource_entry(fi, fwr, &iconsize) != NULL) { if (iconsize == 0) { debugC(DEBUG_RESOURCE, "%s: icon resource `%s' is empty, skipping", _fileName.c_str(), name); skipped++; continue; } if ((uint32)iconsize != FROM_LE_32(icondir->entries[c].bytes_in_res)) { debugC(DEBUG_RESOURCE, "%s: mismatch of size in icon resource `%s' and group (%d != %d)", _fileName.c_str(), name, iconsize, FROM_LE_32(icondir->entries[c].bytes_in_res)); } size += iconsize; /* size += FROM_LE_32(icondir->entries[c].bytes_in_res); */ /* cursor resources have two additional WORDs that contain * hotspot info */ if (!is_icon) size -= sizeof(uint16)*2; } } offset = sizeof(Win32CursorIconFileDir) + (FROM_LE_16(icondir->count)-skipped) * sizeof(Win32CursorIconFileDirEntry); size += offset; *ressize = size; /* allocate that much memory */ memory = (byte *)malloc(size); fileicondir = (Win32CursorIconFileDir *)memory; /* transfer Win32CursorIconDir structure members */ fileicondir->reserved = icondir->reserved; fileicondir->type = icondir->type; fileicondir->count = TO_LE_16(FROM_LE_16(icondir->count) - skipped); /* transfer each cursor/icon: Win32CursorIconDirEntry and data */ skipped = 0; for (c = 0 ; c < FROM_LE_16(icondir->count) ; c++) { int level; char name[14]; WinResource *fwr; byte *data; /* find the corresponding icon resource */ snprintf(name, sizeof(name)/sizeof(char), "-%d", FROM_LE_16(icondir->entries[c].res_id)); fwr = find_resource(fi, (is_icon ? "-3" : "-1"), name, lang, &level); if (fwr == NULL) { error("%s: could not find `%s' in `%s' resource.", _fileName.c_str(), &name[1], (is_icon ? "group_icon" : "group_cursor")); return NULL; } /* get data and size of that resource */ data = (byte *)get_resource_entry(fi, fwr, &size); if (data == NULL) { /* get_resource_entry has printed error */ return NULL; } if (size == 0) { skipped++; continue; } /* copy ICONDIRENTRY (not including last dwImageOffset) */ memcpy(&fileicondir->entries[c-skipped], &icondir->entries[c], sizeof(Win32CursorIconFileDirEntry)-sizeof(uint32)); /* special treatment for cursors */ if (!is_icon) { fileicondir->entries[c-skipped].width = icondir->entries[c].res_info.cursor.width; fileicondir->entries[c-skipped].height = TO_LE_16(FROM_LE_16(icondir->entries[c].res_info.cursor.height) / 2); fileicondir->entries[c-skipped].color_count = 0; fileicondir->entries[c-skipped].reserved = 0; } /* set image offset and increase it */ fileicondir->entries[c-skipped].dib_offset = TO_LE_32(offset); /* transfer resource into file memory */ if (is_icon) { memcpy(&memory[offset], data, FROM_LE_32(icondir->entries[c].bytes_in_res)); } else { fileicondir->entries[c-skipped].hotspot_x = ((uint16 *) data)[0]; fileicondir->entries[c-skipped].hotspot_y = ((uint16 *) data)[1]; memcpy(&memory[offset], data+sizeof(uint16)*2, FROM_LE_32(icondir->entries[c].bytes_in_res)-sizeof(uint16)*2); offset -= sizeof(uint16)*2; } /* increase the offset pointer */ offset += FROM_LE_32(icondir->entries[c].bytes_in_res); } return memory; }