├── tests ├── helper-functions.R ├── testthat.R └── testthat │ ├── test-palette.R │ ├── test-read_pak.R │ ├── test-export.R │ ├── test-read_wad.R │ ├── test-write_wal.R │ ├── test-read_quake_archive.R │ ├── test-read_model_mdl.R │ ├── test-read_model_md2.R │ ├── test-read_wal.R │ └── test-image_to_wal.R ├── .github ├── .gitignore └── workflows │ └── R-CMD-check.yaml ├── .gitattributes ├── inst └── extdata │ ├── bricks.wal │ ├── palette_q1.lmp │ ├── Bricks050_256_Color.jpg │ ├── Bricks050_256_Color.png │ └── Bricks050_256_Color_LICENSE.txt ├── vignettes ├── Bricks050_256_Color.jpg ├── wal.R └── wal.Rmd ├── .Rbuildignore ├── man ├── print.wad.Rd ├── is.quakemodel_md2.Rd ├── is.quakemodel_mdl.Rd ├── plot.wal.Rd ├── is.quakemodel.Rd ├── wad_dir.types.int.Rd ├── wad_dir.types.string.Rd ├── pal_q1.Rd ├── pal_q2.Rd ├── predefined.md2.normals.Rd ├── check.palette.Rd ├── predefined.mdl.normals.Rd ├── half.image.Rd ├── lookup.q1.normals.Rd ├── wad.contents.Rd ├── read.lmp.Rd ├── get.wal.mipmap.widths.Rd ├── wal.template.Rd ├── get.wal.mipmap.heights.Rd ├── read.pak.Rd ├── read.wad.Rd ├── get.wadentry.type.strings.Rd ├── qarchive.type.from.filename.Rd ├── wad_dir.fileext.mapping.Rd ├── unpack.vertex.coords.Rd ├── read.quake.md2.Rd ├── get.mipmap.data.lengths.Rd ├── save.filepart.Rd ├── get.wal.mipmap.data.Rd ├── read.quake.mdl.Rd ├── expand.rawdata.to.mipmaps.Rd ├── writeWAL.Rd ├── apply.palette.to.rawdata.Rd ├── pak.extract.Rd ├── wad.texname.clean.Rd ├── get.mipmap.data.offsets.Rd ├── quakemodel.to.fs.surface.Rd ├── read.quake1miptex.Rd ├── qarchive.extract.Rd ├── wad.extract.Rd ├── wal.export.to.png.Rd ├── plotwal.mipmap.Rd ├── wal.export.to.jpeg.Rd ├── readWAL.Rd ├── read.wal.Rd ├── plotwal.rawdata.Rd ├── closest.color.from.palette.Rd └── img.to.wal.Rd ├── wal.Rproj ├── CHANGES ├── .gitignore ├── DESCRIPTION ├── NAMESPACE ├── web ├── wal_clients │ └── pakextract.R └── c │ └── read_wal.c ├── appveyor.yml ├── R ├── read_model_common.R ├── read_quake_archive.R ├── write_wal.R ├── export.R ├── read_pak.R ├── palette.R ├── image_to_wal.R ├── read_wad.R ├── read_model_md2.R ├── read_wal.R └── read_model_mdl.R ├── README.md └── LICENSE_FULL /tests/helper-functions.R: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(wal) 3 | 4 | test_check("wal") 5 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | inst/* binary 3 | R/* text=lf 4 | tests/* text=lf 5 | -------------------------------------------------------------------------------- /inst/extdata/bricks.wal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfsp-spirit/wal/HEAD/inst/extdata/bricks.wal -------------------------------------------------------------------------------- /inst/extdata/palette_q1.lmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfsp-spirit/wal/HEAD/inst/extdata/palette_q1.lmp -------------------------------------------------------------------------------- /vignettes/Bricks050_256_Color.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfsp-spirit/wal/HEAD/vignettes/Bricks050_256_Color.jpg -------------------------------------------------------------------------------- /inst/extdata/Bricks050_256_Color.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfsp-spirit/wal/HEAD/inst/extdata/Bricks050_256_Color.jpg -------------------------------------------------------------------------------- /inst/extdata/Bricks050_256_Color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfsp-spirit/wal/HEAD/inst/extdata/Bricks050_256_Color.png -------------------------------------------------------------------------------- /inst/extdata/Bricks050_256_Color_LICENSE.txt: -------------------------------------------------------------------------------- 1 | These files are licensed under the CC0 license. 2 | 3 | They have been downloaded from https://cc0textures.com/view?id=Bricks050. 4 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^web/c/read_wal.c$ 3 | web/c/ 4 | web 5 | ^\.Rproj\.user$ 6 | ^Meta$ 7 | ^\.DS_Store$ 8 | .*/\.DS_Store$ 9 | .DS_Store$ 10 | ^README_DEVELOPMENT\.md$ 11 | ^README\.md$ 12 | ^CONTRIBUTING\.md$ 13 | LICENSE_FULL 14 | CHANGES 15 | appveyor\.yml 16 | ^appveyor\.yml$ 17 | ^\.github$ 18 | -------------------------------------------------------------------------------- /man/print.wad.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{print.wad} 4 | \alias{print.wad} 5 | \title{S3 print function for WAD} 6 | \usage{ 7 | \method{print}{wad}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{wad instance} 11 | 12 | \item{...}{extra arguments, ignored} 13 | } 14 | \description{ 15 | S3 print function for WAD 16 | } 17 | -------------------------------------------------------------------------------- /man/is.quakemodel_md2.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_md2.R 3 | \name{is.quakemodel_md2} 4 | \alias{is.quakemodel_md2} 5 | \title{Check whether object is Quake 2 MD2 model} 6 | \usage{ 7 | is.quakemodel_md2(x) 8 | } 9 | \arguments{ 10 | \item{x}{any R object} 11 | } 12 | \description{ 13 | Check whether object is Quake 2 MD2 model 14 | } 15 | -------------------------------------------------------------------------------- /man/is.quakemodel_mdl.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_mdl.R 3 | \name{is.quakemodel_mdl} 4 | \alias{is.quakemodel_mdl} 5 | \title{Check whether object is Quake 1 MDL model} 6 | \usage{ 7 | is.quakemodel_mdl(x) 8 | } 9 | \arguments{ 10 | \item{x}{any R object} 11 | } 12 | \description{ 13 | Check whether object is Quake 1 MDL model 14 | } 15 | -------------------------------------------------------------------------------- /man/plot.wal.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{plot.wal} 4 | \alias{plot.wal} 5 | \title{S3 plot function for wal image.} 6 | \usage{ 7 | \method{plot}{wal}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a wal instance.} 11 | 12 | \item{...}{extra args, not used.} 13 | } 14 | \description{ 15 | S3 plot function for wal image. 16 | } 17 | -------------------------------------------------------------------------------- /man/is.quakemodel.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_common.R 3 | \name{is.quakemodel} 4 | \alias{is.quakemodel} 5 | \title{Check whether object is a Quake 1 or 2 alias model.} 6 | \usage{ 7 | is.quakemodel(x) 8 | } 9 | \arguments{ 10 | \item{x}{any R object} 11 | } 12 | \description{ 13 | Check whether object is a Quake 1 or 2 alias model. 14 | } 15 | -------------------------------------------------------------------------------- /man/wad_dir.types.int.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{wad_dir.types.int} 4 | \alias{wad_dir.types.int} 5 | \title{Get integers representing WAD dir entry types.} 6 | \usage{ 7 | wad_dir.types.int() 8 | } 9 | \description{ 10 | Get integers representing WAD dir entry types. 11 | } 12 | \seealso{ 13 | wad_dir.types.string 14 | } 15 | \keyword{internal} 16 | -------------------------------------------------------------------------------- /man/wad_dir.types.string.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{wad_dir.types.string} 4 | \alias{wad_dir.types.string} 5 | \title{Get strings describing WAD dir entry types.} 6 | \usage{ 7 | wad_dir.types.string() 8 | } 9 | \description{ 10 | Get strings describing WAD dir entry types. 11 | } 12 | \seealso{ 13 | wad_dir.types.int 14 | } 15 | \keyword{internal} 16 | -------------------------------------------------------------------------------- /man/pal_q1.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/palette.R 3 | \name{pal_q1} 4 | \alias{pal_q1} 5 | \title{Get Q1 palette.} 6 | \usage{ 7 | pal_q1() 8 | } 9 | \value{ 10 | 256 x 3 integer matrix, representing the RGB color values for an index into the palette. 11 | } 12 | \description{ 13 | Get Q1 palette. 14 | } 15 | \examples{ 16 | pal = pal_q1(); 17 | dim(pal); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /man/pal_q2.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/palette.R 3 | \name{pal_q2} 4 | \alias{pal_q2} 5 | \title{Get Q2 palette.} 6 | \usage{ 7 | pal_q2() 8 | } 9 | \value{ 10 | 256 x 3 integer matrix, representing the RGB color values for an index into the palette. 11 | } 12 | \description{ 13 | Get Q2 palette. 14 | } 15 | \examples{ 16 | pal = pal_q2(); 17 | dim(pal); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /man/predefined.md2.normals.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_md2.R 3 | \name{predefined.md2.normals} 4 | \alias{predefined.md2.normals} 5 | \title{Predefined MD2 normals from Quake 2.} 6 | \usage{ 7 | predefined.md2.normals() 8 | } 9 | \value{ 10 | 3xn matrix of normals. 11 | } 12 | \description{ 13 | Predefined MD2 normals from Quake 2. 14 | } 15 | \keyword{internal} 16 | -------------------------------------------------------------------------------- /man/check.palette.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{check.palette} 4 | \alias{check.palette} 5 | \title{Check palette, stop on invalid data.} 6 | \usage{ 7 | check.palette(pal) 8 | } 9 | \arguments{ 10 | \item{pal}{a palette, i.e., a 256 x 3 integer matrix, with values in range 0..255L.} 11 | } 12 | \description{ 13 | Check palette, stop on invalid data. 14 | } 15 | \keyword{internal} 16 | -------------------------------------------------------------------------------- /man/predefined.mdl.normals.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_mdl.R 3 | \name{predefined.mdl.normals} 4 | \alias{predefined.mdl.normals} 5 | \title{Return list of pre-defined Quake I normals.} 6 | \usage{ 7 | predefined.mdl.normals() 8 | } 9 | \value{ 10 | n x 3 matrix of doubles, the normals. Hardcoded. 11 | } 12 | \description{ 13 | Return list of pre-defined Quake I normals. 14 | } 15 | \keyword{internal} 16 | -------------------------------------------------------------------------------- /man/half.image.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/image_to_wal.R 3 | \name{half.image} 4 | \alias{half.image} 5 | \title{Reduce image size by 2 along both axes by dropping pixels.} 6 | \usage{ 7 | half.image(image_data, byrow = TRUE) 8 | } 9 | \arguments{ 10 | \item{image_data}{integer matrix, a 1-channel image.} 11 | } 12 | \description{ 13 | Reduce image size by 2 along both axes by dropping pixels. 14 | } 15 | \keyword{internal} 16 | -------------------------------------------------------------------------------- /tests/testthat/test-palette.R: -------------------------------------------------------------------------------- 1 | 2 | testthat::test_that("The Q1 palette is available.", { 3 | pal = wal::pal_q1(); 4 | 5 | testthat::expect_true(is.matrix(pal)); 6 | testthat::expect_true(is.integer(pal)); 7 | testthat::expect_equal(dim(pal), c(256L, 3L)); 8 | }) 9 | 10 | 11 | testthat::test_that("The Q2 palette is available.", { 12 | pal = wal::pal_q2(); 13 | 14 | testthat::expect_true(is.matrix(pal)); 15 | testthat::expect_true(is.integer(pal)); 16 | testthat::expect_equal(dim(pal), c(256L, 3L)); 17 | }) 18 | -------------------------------------------------------------------------------- /man/lookup.q1.normals.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_mdl.R 3 | \name{lookup.q1.normals} 4 | \alias{lookup.q1.normals} 5 | \title{Lookup Quake I normals by index.} 6 | \usage{ 7 | lookup.q1.normals(normal_indices) 8 | } 9 | \arguments{ 10 | \item{normal_indices}{integer vector of length n, the normal indices (0-based).} 11 | } 12 | \value{ 13 | n x 3 matrix of doubles, the normals 14 | } 15 | \description{ 16 | Lookup Quake I normals by index. 17 | } 18 | \keyword{internal} 19 | -------------------------------------------------------------------------------- /man/wad.contents.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{wad.contents} 4 | \alias{wad.contents} 5 | \title{List WAD file contents.} 6 | \usage{ 7 | wad.contents(wad) 8 | } 9 | \arguments{ 10 | \item{wad}{a wad instance, see \code{read.wad}. Alternatively a character string, which will be interpreted as a filepath to a WAD file that should be loaded.} 11 | } 12 | \value{ 13 | data.frame, info on the files inside the wad. 14 | } 15 | \description{ 16 | List WAD file contents. 17 | } 18 | -------------------------------------------------------------------------------- /wal.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageCheckArgs: --as-cran --run-donttest 22 | PackageRoxygenize: rd,collate,namespace,vignette 23 | -------------------------------------------------------------------------------- /man/read.lmp.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/palette.R 3 | \name{read.lmp} 4 | \alias{read.lmp} 5 | \title{Read binary lump, or 'lmp' files.} 6 | \usage{ 7 | read.lmp(filepath, dlength = 768L) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, path to the input file.} 11 | 12 | \item{dlength}{the expected data length, in bytes.} 13 | } 14 | \value{ 15 | vector of dlength unsigned integers in range 0..255. 16 | } 17 | \description{ 18 | Read binary lump, or 'lmp' files. 19 | } 20 | \keyword{internal} 21 | -------------------------------------------------------------------------------- /man/get.wal.mipmap.widths.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{get.wal.mipmap.widths} 4 | \alias{get.wal.mipmap.widths} 5 | \title{Compute widths of the 4 mipimap levels from base width.} 6 | \usage{ 7 | get.wal.mipmap.widths(width_mm) 8 | } 9 | \arguments{ 10 | \item{width_mm}{integer, the base mipmap width.} 11 | } 12 | \value{ 13 | integer vector of length 4, the mipmap widths. 14 | } 15 | \description{ 16 | Compute widths of the 4 mipimap levels from base width. 17 | } 18 | \keyword{internal} 19 | -------------------------------------------------------------------------------- /man/wal.template.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/image_to_wal.R 3 | \name{wal.template} 4 | \alias{wal.template} 5 | \title{Generate a WAL structure template.} 6 | \usage{ 7 | wal.template() 8 | } 9 | \description{ 10 | Generates a WAL instance that can be modified and filled with new data. The template represents a black 32x32 image (if the palette used to display it adhers to the convention that the first color is black). The indices used are 1-based (black is at index 1, not 0). 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/get.wal.mipmap.heights.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{get.wal.mipmap.heights} 4 | \alias{get.wal.mipmap.heights} 5 | \title{Compute widths of the 4 mipimap levels from base width.} 6 | \usage{ 7 | get.wal.mipmap.heights(height_mm) 8 | } 9 | \arguments{ 10 | \item{height_mm}{integer, the base mipmap height.} 11 | } 12 | \value{ 13 | integer vector of length 4, the mipmap heights. 14 | } 15 | \description{ 16 | Compute widths of the 4 mipimap levels from base width. 17 | } 18 | \keyword{internal} 19 | -------------------------------------------------------------------------------- /man/read.pak.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_pak.R 3 | \name{read.pak} 4 | \alias{read.pak} 5 | \title{Read Quake PAK archive.} 6 | \usage{ 7 | read.pak(filepath) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, path to the file including extension.} 11 | } 12 | \value{ 13 | a 'pak' instance. 14 | } 15 | \description{ 16 | Read Quake PAK archive. 17 | } 18 | \examples{ 19 | \dontrun{ 20 | pakf = '~/.steam/steam/steamapps/common/Quake/Id1/PAK0.PAK'; 21 | pak = read.pak(pakf); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /man/read.wad.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{read.wad} 4 | \alias{read.wad} 5 | \title{Read Quake WAD file.} 6 | \usage{ 7 | read.wad(filepath) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, path to the file.} 11 | } 12 | \value{ 13 | a wad instance, can be used to extract data or list contents. 14 | } 15 | \description{ 16 | Read Quake WAD file. 17 | } 18 | \examples{ 19 | \dontrun{ 20 | wadf = '~/knave.wad'; 21 | wad = read.wad(wadf); 22 | wad.contents(wad); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /man/get.wadentry.type.strings.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{get.wadentry.type.strings} 4 | \alias{get.wadentry.type.strings} 5 | \title{Translate wad directory entry types from the integer to the string representation.} 6 | \usage{ 7 | get.wadentry.type.strings(wadentry_type_int) 8 | } 9 | \arguments{ 10 | \item{wadentry_type_int}{integer, WAD entry type code} 11 | } 12 | \value{ 13 | type string 14 | } 15 | \description{ 16 | Translate wad directory entry types from the integer to the string representation. 17 | } 18 | \keyword{internal} 19 | -------------------------------------------------------------------------------- /man/qarchive.type.from.filename.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_quake_archive.R 3 | \name{qarchive.type.from.filename} 4 | \alias{qarchive.type.from.filename} 5 | \title{Determine archive type from file name extension.} 6 | \usage{ 7 | qarchive.type.from.filename(filepath) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, path to existing and readable file in PAK or WAD2 format.} 11 | } 12 | \value{ 13 | character string, one of 'QARCHIVE_TYPE_WAD' or 'QARCHIVE_TYPE_PAK'. 14 | } 15 | \description{ 16 | Determine archive type from file name extension. 17 | } 18 | \keyword{internal} 19 | -------------------------------------------------------------------------------- /man/wad_dir.fileext.mapping.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{wad_dir.fileext.mapping} 4 | \alias{wad_dir.fileext.mapping} 5 | \title{Get file extensions for WAD dir entry type strings.} 6 | \usage{ 7 | wad_dir.fileext.mapping() 8 | } 9 | \value{ 10 | named list, which maps \code{wad_dir.types.string}s to file extensions. Afaik, there are not standard file extensions for these file types, and I made the ones used here up. 11 | } 12 | \description{ 13 | Get file extensions for WAD dir entry type strings. 14 | } 15 | \seealso{ 16 | wad_dir.types.string 17 | } 18 | \keyword{internal} 19 | -------------------------------------------------------------------------------- /CHANGES: -------------------------------------------------------------------------------- 1 | * v0.1.1 2 | - fix a minor docstring issue (I received threat of package removal emails from CRAN) 3 | 4 | 5 | * v0.1.0 (aka 54c966e) 6 | - add read support for WAD archives and option to extract the contents 7 | - add read support for PAK archives and option to extract the contents 8 | - add supprot for reading Quake I models (MDL format) and exporting frames to MD3 9 | - add supprot for reading Quake II models (MD2 format) and exporting frames to MD3 10 | - add support for reading Quake I textures ('textzure lump') and exporting to JPEG or PNG 11 | 12 | 13 | 14 | * v0.0.1 -- Initial Release 15 | - read, write and export support for WAL textures 16 | -------------------------------------------------------------------------------- /man/unpack.vertex.coords.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_mdl.R 3 | \name{unpack.vertex.coords} 4 | \alias{unpack.vertex.coords} 5 | \title{Unpack vertex coords from Q1 0-255 representation.} 6 | \usage{ 7 | unpack.vertex.coords(coords_packed, mdl_header) 8 | } 9 | \arguments{ 10 | \item{coords_packed}{matrix of n x 3 integers in range 0..255, the packed coords from an MDL file.} 11 | 12 | \item{mdl_header}{MDL header or named list, only the fields 'header$scale' and 'header$origin' are used.} 13 | } 14 | \description{ 15 | Unpack vertex coords from Q1 0-255 representation. 16 | } 17 | \keyword{internal} 18 | -------------------------------------------------------------------------------- /tests/testthat/test-read_pak.R: -------------------------------------------------------------------------------- 1 | 2 | testthat::test_that("We can read a PAK file.", { 3 | #pakf = system.file("extdata", "test.pak", package = "wal", mustWork = TRUE); 4 | pakf = file.path("~/data/PAK0.PAK"); 5 | if(! file.exists(pakf)) { 6 | testthat::skip("Test PAK file available"); 7 | } 8 | 9 | pak = read.pak(pakf); 10 | 11 | testthat::expect_false(is.null(pak$header)); 12 | testthat::expect_false(is.null(pak$contents)); 13 | testthat::expect_equal(nrow(pak$contents), 339L); 14 | testthat::expect_true(is.data.frame(pak$contents)); 15 | 16 | # extract PAK file 17 | td = tempdir(); 18 | pak.extract(pakf, outdir = td); 19 | }) 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /man/read.quake.md2.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_md2.R 3 | \name{read.quake.md2} 4 | \alias{read.quake.md2} 5 | \title{Read Quake II model in MD2 format.} 6 | \usage{ 7 | read.quake.md2(filepath, anim = FALSE) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, the path to the MD2 file} 11 | 12 | \item{anim}{logical, whether to load the whole animation (if present). Returns a list of models, the animation frames. If FALSE, only the first frame is returned.} 13 | } 14 | \description{ 15 | Read Quake II model in MD2 format. 16 | } 17 | \note{ 18 | Ignore this function, it will be moved to a different package. 19 | } 20 | -------------------------------------------------------------------------------- /man/get.mipmap.data.lengths.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/image_to_wal.R 3 | \name{get.mipmap.data.lengths} 4 | \alias{get.mipmap.data.lengths} 5 | \title{Compute length of mipmaps in bytes from width and height of largest image (mipmap0).} 6 | \usage{ 7 | get.mipmap.data.lengths(mm0_width, mm0_height) 8 | } 9 | \arguments{ 10 | \item{mm0_width}{integer, width of mipmap 0} 11 | 12 | \item{mm0_height}{integer, height of mipmap 0} 13 | } 14 | \value{ 15 | integer vector of length 4, the lengths. 16 | } 17 | \description{ 18 | Compute length of mipmaps in bytes from width and height of largest image (mipmap0). 19 | } 20 | \keyword{internal} 21 | -------------------------------------------------------------------------------- /man/save.filepart.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{save.filepart} 4 | \alias{save.filepart} 5 | \title{Read part of binary file and save as new file.} 6 | \usage{ 7 | save.filepart(infile, read_from, read_len, outfile) 8 | } 9 | \arguments{ 10 | \item{infile}{for input file, part of it gets read.} 11 | 12 | \item{read_from}{integer, index at which to start reading, from start of file. Used to \code{seek} to the position.} 13 | 14 | \item{read_len}{integer, the number of bytes to read.} 15 | 16 | \item{outfile}{character string, the output filename.} 17 | } 18 | \description{ 19 | Read part of binary file and save as new file. 20 | } 21 | \keyword{internal} 22 | -------------------------------------------------------------------------------- /man/get.wal.mipmap.data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{get.wal.mipmap.data} 4 | \alias{get.wal.mipmap.data} 5 | \title{Retrieve raw data for given mipmap level from WAL instance.} 6 | \usage{ 7 | get.wal.mipmap.data(wal, mip_level) 8 | } 9 | \arguments{ 10 | \item{wal}{a WAL image instance, as returned by \code{read.wal}.} 11 | 12 | \item{mip_level}{integer in range 0..3, the mipmap to plot. Level 0 is the original full-size image, the other ones get smaller and smaller (by factor 2 on each dimension, so 1/4th the size of their predecessor).} 13 | } 14 | \description{ 15 | Retrieve raw data for given mipmap level from WAL instance. 16 | } 17 | \keyword{internal} 18 | -------------------------------------------------------------------------------- /tests/testthat/test-export.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | testthat::test_that("We can convert a WAL file to JPEG format.", { 4 | 5 | walf = system.file("extdata", "bricks.wal", package = "wal", mustWork = TRUE); 6 | 7 | wal = wal::read.wal(walf); 8 | tex_jpg = tempfile(fileext = ".jpg"); 9 | wal::wal.export.to.jpeg(wal, tex_jpg); 10 | 11 | testthat::expect_true(file.exists(tex_jpg)); 12 | }) 13 | 14 | 15 | testthat::test_that("We can convert a WAL file to PNG format.", { 16 | 17 | walf = system.file("extdata", "bricks.wal", package = "wal", mustWork = TRUE); 18 | 19 | wal = wal::read.wal(walf); 20 | tex_png = tempfile(fileext = ".png"); 21 | wal::wal.export.to.png(wal, tex_png); 22 | 23 | testthat::expect_true(file.exists(tex_png)); 24 | }) 25 | 26 | -------------------------------------------------------------------------------- /man/read.quake.mdl.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_mdl.R 3 | \name{read.quake.mdl} 4 | \alias{read.quake.mdl} 5 | \title{Read Quake model in MDL format.} 6 | \usage{ 7 | read.quake.mdl(filepath, do_checks = FALSE) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, the path to the MDL file} 11 | 12 | \item{do_checks}{logical, whether to perform some sanity checks on the data and warn on suspicious results.} 13 | } 14 | \description{ 15 | Read Quake model in MDL format. 16 | } 17 | \note{ 18 | Ignore this function, it will be moved to a different package. 19 | } 20 | \examples{ 21 | \dontrun{ 22 | mdlf = "~/data/q1_pak/progs/quaddama.mdl" 23 | mdl = read.quake.mdl(mdlf); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /man/expand.rawdata.to.mipmaps.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/image_to_wal.R 3 | \name{expand.rawdata.to.mipmaps} 4 | \alias{expand.rawdata.to.mipmaps} 5 | \title{Given the pixel data for the largest image, generate the full data for all mipmaps.} 6 | \usage{ 7 | expand.rawdata.to.mipmaps(raw_data_mip_level0, width, height, byrow = TRUE) 8 | } 9 | \arguments{ 10 | \item{raw_data_mip_level0}{integer vector or matrix, the image data for the largest mipmap.} 11 | 12 | \item{width}{integer, width of image for mip level 0} 13 | 14 | \item{height}{integer, width of image for mip level 0} 15 | } 16 | \description{ 17 | Given the pixel data for the largest image, generate the full data for all mipmaps. 18 | } 19 | \keyword{internal} 20 | -------------------------------------------------------------------------------- /man/writeWAL.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/write_wal.R 3 | \name{writeWAL} 4 | \alias{writeWAL} 5 | \title{Write WAL instance to bitmap file in WAL format.} 6 | \usage{ 7 | writeWAL(filepath, wal) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, path to the file including extension} 11 | 12 | \item{wal}{a wal instance. Note that 1 will be substracted from the data when it is written, as indices are stored 0-based in the file.} 13 | } 14 | \description{ 15 | Write WAL instance to bitmap file in WAL format. 16 | } 17 | \examples{ 18 | \dontrun{ 19 | walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 20 | wal = read.wal(walf); 21 | writeWAL(tempfile(fileext = ".wal"), wal); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /man/apply.palette.to.rawdata.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{apply.palette.to.rawdata} 4 | \alias{apply.palette.to.rawdata} 5 | \title{Apply a palette to index data to create a 2D image.} 6 | \usage{ 7 | apply.palette.to.rawdata(raw_data, apply_palette, img_width, img_height) 8 | } 9 | \arguments{ 10 | \item{raw_data}{integer vector of pixel data, each entry represents an index into the palette.} 11 | 12 | \item{apply_palette}{integer matrix, the palette.} 13 | 14 | \item{img_width}{integer, the width of the image to create.} 15 | 16 | \item{img_height}{integer, the height of the image to create.} 17 | } 18 | \description{ 19 | Apply a palette to index data to create a 2D image. 20 | } 21 | \keyword{internal} 22 | -------------------------------------------------------------------------------- /man/pak.extract.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_pak.R 3 | \name{pak.extract} 4 | \alias{pak.extract} 5 | \title{Extract PAK contents into existing directory.} 6 | \usage{ 7 | pak.extract(pak_filepath, outdir = getwd()) 8 | } 9 | \arguments{ 10 | \item{pak_filepath}{character string, path to input PAK file.} 11 | 12 | \item{outdir}{character string, the output directory in which the files should be created. Must be writeable. The sub directories and filenames are derived from the data in the WAD.} 13 | } 14 | \description{ 15 | Extract PAK contents into existing directory. 16 | } 17 | \note{ 18 | PAK files can contain a directory structure, and new subdirectories will be created under \code{outdir} as needed to preserve it. 19 | } 20 | -------------------------------------------------------------------------------- /man/wad.texname.clean.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{wad.texname.clean} 4 | \alias{wad.texname.clean} 5 | \title{Replace special chars in texture names to turn it into a valid filename.} 6 | \usage{ 7 | wad.texname.clean(texnames) 8 | } 9 | \arguments{ 10 | \item{texnames}{character string, texture names from a WAD file. The textures may contain the special characters '*' and '+', which are used to indicate sequences (textures that change on an event, like a pressed button turning from red to green) and other things.} 11 | } 12 | \value{ 13 | character strings usable as filenames. 14 | } 15 | \description{ 16 | Replace special chars in texture names to turn it into a valid filename. 17 | } 18 | \keyword{internal} 19 | -------------------------------------------------------------------------------- /man/get.mipmap.data.offsets.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/image_to_wal.R 3 | \name{get.mipmap.data.offsets} 4 | \alias{get.mipmap.data.offsets} 5 | \title{Get mipmap offsets for WAL header, based on mipmap sizes and start offset.} 6 | \usage{ 7 | get.mipmap.data.offsets(mm0_width, mm0_height, start_at = 100L) 8 | } 9 | \arguments{ 10 | \item{mm0_width}{integer, width of mipmap 0} 11 | 12 | \item{mm0_height}{integer, height of mipmap 0} 13 | 14 | \item{start_at}{integer, the offset at which the data starts in the file. Must be 100L for WAL format.} 15 | } 16 | \value{ 17 | integer vector of length 4, the offsets. 18 | } 19 | \description{ 20 | Get mipmap offsets for WAL header, based on mipmap sizes and start offset. 21 | } 22 | \keyword{internal} 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # MacOS trash 2 | .DS_Store 3 | 4 | # History files 5 | .Rhistory 6 | .Rapp.history 7 | 8 | # Session Data files 9 | .RData 10 | 11 | # Example code in package build process 12 | *-Ex.R 13 | 14 | # Output files from R CMD build 15 | /*.tar.gz 16 | 17 | # Output files from R CMD check 18 | /*.Rcheck/ 19 | 20 | # RStudio files 21 | .Rproj.user/ 22 | 23 | # produced vignettes 24 | vignettes/*.html 25 | vignettes/*.pdf 26 | 27 | # OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 28 | .httr-oauth 29 | 30 | # knitr and R markdown default cache directories 31 | /*_cache/ 32 | /cache/ 33 | 34 | # Temporary files created by R markdown 35 | *.utf8.md 36 | *.knit.md 37 | 38 | # Shiny token, see https://shiny.rstudio.com/articles/shinyapps.html 39 | rsconnect/ 40 | Meta 41 | .Rproj.user 42 | -------------------------------------------------------------------------------- /man/quakemodel.to.fs.surface.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_model_common.R 3 | \name{quakemodel.to.fs.surface} 4 | \alias{quakemodel.to.fs.surface} 5 | \title{Convert Quake Model to 'fs.surface' instance.} 6 | \usage{ 7 | quakemodel.to.fs.surface(quakemodel, frame_idx = 1L) 8 | } 9 | \arguments{ 10 | \item{quakemodel}{an instance of \code{quakemodel_mdl} or \code{quakemodel_md2}.} 11 | 12 | \item{frame_idx}{integer, the frame to export. Quake models may contain animations made up of several frames. The mesh connectivity is unaltered between frames, but the vertex positions differ.} 13 | } 14 | \value{ 15 | \code{fs.surface} mesh instance, as used by the \code{freesurferformats} package. 16 | } 17 | \description{ 18 | Convert Quake Model to 'fs.surface' instance. 19 | } 20 | -------------------------------------------------------------------------------- /man/read.quake1miptex.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{read.quake1miptex} 4 | \alias{read.quake1miptex} 5 | \title{Read a Quake mipmap texture from a WAD2 file.} 6 | \usage{ 7 | read.quake1miptex(filepath, at_offset = 0L) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, path to WAD file.} 11 | 12 | \item{at_offset}{integer, the index in the WAD file where the texture starts.} 13 | } 14 | \value{ 15 | a 'qmiptex' instance, its like a wall with shorter name field (16 instead of 32) and some fields (anim_name, flags, contents, value) missing. 16 | } 17 | \description{ 18 | Read a Quake mipmap texture from a WAD2 file. 19 | } 20 | \examples{ 21 | \dontrun{ 22 | qm = read.quake1miptex("~/knave.wad", at_offset = 1317632); 23 | plotwal.mipmap(qm, apply_palette = pal_q1()); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /man/qarchive.extract.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_quake_archive.R 3 | \name{qarchive.extract} 4 | \alias{qarchive.extract} 5 | \title{Extract any of the supported Quake archives.} 6 | \usage{ 7 | qarchive.extract(filepath, outdir, format = "auto", do_pre_checks = TRUE) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, path to existing and readable file in PAK or WAD2 format.} 11 | 12 | \item{outdir}{character string, path to an existing and writeable output directory into which to extract the archive.} 13 | 14 | \item{format}{character string, of one 'auto' to detect from filename, 'QARCHIVE_TYPE_WAD' for WAD2, or 'QARCHIVE_TYPE_PAK' for PACK.} 15 | 16 | \item{do_pre_checks}{logical, whether to perform extra sanity checks on the other parameters.} 17 | } 18 | \description{ 19 | Extract any of the supported Quake archives. 20 | } 21 | -------------------------------------------------------------------------------- /man/wad.extract.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wad.R 3 | \name{wad.extract} 4 | \alias{wad.extract} 5 | \title{Extract WAD contents into existing directory.} 6 | \usage{ 7 | wad.extract( 8 | wad_filepath, 9 | outdir = getwd(), 10 | file_ext_mapping = wad_dir.fileext.mapping() 11 | ) 12 | } 13 | \arguments{ 14 | \item{wad_filepath}{character string, path to input WAD file.} 15 | 16 | \item{outdir}{character string, the output directory in which the files should be created. The filenames are derived from the data in the WAD.} 17 | 18 | \item{file_ext_mapping}{named list, with keys corresponding to the type names and values are file extensions, including the dot, to use for them.} 19 | } 20 | \description{ 21 | Extract WAD contents into existing directory. 22 | } 23 | \note{ 24 | One can read extracted textures with \code{read.quake1miptex()}. 25 | } 26 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: wal 2 | Type: Package 3 | Title: Read and Write 'wal' Bitmap Image Files and Other 'Quake' Assets 4 | Version: 0.1.1 5 | Authors@R: person("Tim", "Schäfer", role = c("aut", "cre"), email = "ts+code@rcmd.org", comment = c(ORCID = "0000-0002-3683-8070")) 6 | Maintainer: Tim Schäfer 7 | Description: Read 'Quake' assets including bitmap images and textures in 'wal' file format. This package also provides support for extracting these assets from 'WAD' and 'PAK' file archives. It can also read models in 'MDL' and 'MD2' formats. 8 | License: GPL-2 9 | Encoding: UTF-8 10 | URL: https://github.com/dfsp-spirit/wal 11 | BugReports: https://github.com/dfsp-spirit/wal/issues 12 | Imports: 13 | freesurferformats (>= 0.1.12), 14 | imager, 15 | jpeg, 16 | png, 17 | spacesXYZ 18 | Suggests: 19 | knitr, 20 | rmarkdown, 21 | testthat (>= 2.1.0), 22 | VignetteBuilder: knitr 23 | RoxygenNote: 7.2.3 24 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(plot,wal) 4 | S3method(print,wad) 5 | export(closest.color.from.palette) 6 | export(img.to.wal) 7 | export(is.quakemodel) 8 | export(is.quakemodel_md2) 9 | export(is.quakemodel_mdl) 10 | export(pak.extract) 11 | export(pal_q1) 12 | export(pal_q2) 13 | export(plotwal.mipmap) 14 | export(plotwal.rawdata) 15 | export(qarchive.extract) 16 | export(quakemodel.to.fs.surface) 17 | export(read.pak) 18 | export(read.quake.md2) 19 | export(read.quake.mdl) 20 | export(read.quake1miptex) 21 | export(read.wad) 22 | export(read.wal) 23 | export(readWAL) 24 | export(wad.contents) 25 | export(wad.extract) 26 | export(wal.export.to.jpeg) 27 | export(wal.export.to.png) 28 | export(writeWAL) 29 | importFrom(freesurferformats,flip3D) 30 | importFrom(freesurferformats,rotate3D) 31 | importFrom(grDevices,convertColor) 32 | importFrom(graphics,plot) 33 | importFrom(jpeg,writeJPEG) 34 | importFrom(png,writePNG) 35 | importFrom(spacesXYZ,DeltaE) 36 | -------------------------------------------------------------------------------- /tests/testthat/test-read_wad.R: -------------------------------------------------------------------------------- 1 | 2 | testthat::test_that("We can read a WAD file.", { 3 | #wadf = system.file("extdata", "test.wad", package = "wal", mustWork = TRUE); 4 | wadf = file.path("~/data/knave.wad"); # get it from quaddicted.com 5 | if(! file.exists(wadf)) { 6 | testthat::skip("Test WAD file available"); 7 | } 8 | 9 | wad = read.wad(wadf); 10 | 11 | testthat::expect_false(is.null(wad$header)); 12 | testthat::expect_false(is.null(wad$contents)); 13 | testthat::expect_equal(nrow(wad$contents), 455L); 14 | testthat::expect_true(is.data.frame(wad$contents)); 15 | 16 | # list WAD contents 17 | wc = wad.contents(wad); 18 | testthat::expect_true(is.data.frame(wc)); 19 | 20 | # extract WAD 21 | td = tempdir(); 22 | wad.extract(wadf, outdir = td); 23 | testthat::expect_true(file.exists(file.path(td, 'brimstone2.qrs'))); 24 | 25 | # load Q1 texture from within the WAD 26 | qm = read.quake1miptex(wadf, at_offset = 1317632L); 27 | }) 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /tests/testthat/test-write_wal.R: -------------------------------------------------------------------------------- 1 | 2 | testthat::test_that("We can write and re-read a WAL texture if available.", { 3 | walf = system.file("extdata", "bricks.wal", package = "wal", mustWork = TRUE); 4 | wal = wal::read.wal(walf); 5 | 6 | testthat::expect_equal(wal$header$tex_name, "e1u1/black"); 7 | 8 | walf_written = tempfile(fileext = ".wal"); 9 | writeWAL(walf_written, wal); 10 | wal_reread = read.wal(walf_written); 11 | 12 | testthat::expect_equal(wal$header$tex_name, wal_reread$header$tex_name); 13 | testthat::expect_equal(wal$header$width, wal_reread$header$width); 14 | testthat::expect_equal(wal$header$height, wal_reread$header$height); 15 | testthat::expect_equal(wal$header$flags, wal_reread$header$flags); 16 | testthat::expect_equal(wal$header$contents, wal_reread$header$contents); 17 | testthat::expect_equal(wal$header$value, wal_reread$header$value); 18 | 19 | testthat::expect_equal(wal$file_data_all_mipmaps, wal_reread$file_data_all_mipmaps); 20 | }) 21 | -------------------------------------------------------------------------------- /web/wal_clients/pakextract.R: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env Rscript 2 | # Extract Quake archive in WAL (technically 'WAL2') or PAK format to directory. 3 | # 4 | # Dependencies: 5 | # Requires the 'wal' R package to be installed. 6 | # Note: To install it, start a session in a recent R version and type: install.packages('wal') 7 | # 8 | # USAGE: ./pakextract.R 9 | # 10 | # EXAMPLE: ./pakextract.R knave.wad ~/mydata/knave_extracted/ 11 | # 12 | # Written by Tim Schaefer 13 | # This program is part of 'wal' and covered by its license. See https://github.com/dfsp-spirit/wal for details. 14 | 15 | library("wal"); 16 | args = commandArgs(trailingOnly = TRUE); 17 | 18 | if (length(args) != 2) { 19 | cat(sprintf("=== pakextract -- Extract Quake archives in PAK or WAL format. ===\n")); 20 | stop(sprintf("USAGE: pakextract.R \nEXAMPLES: ./pakextract.R knave.wad ~/mydata/knave_extracted/\n ./pakextract.R pak0.pak .")); 21 | } else { 22 | wal::qarchive.extract(args[1], args[2]); 23 | } 24 | -------------------------------------------------------------------------------- /tests/testthat/test-read_quake_archive.R: -------------------------------------------------------------------------------- 1 | 2 | testthat::test_that("We can extract a WAD file using qarchive.extract", { 3 | #wadf = system.file("extdata", "test.wad", package = "wal", mustWork = TRUE); 4 | wadf = file.path("~/data/knave.wad"); # get it from quaddicted.com 5 | if(! file.exists(wadf)) { 6 | testthat::skip("Test WAD file available"); 7 | } 8 | 9 | # extract WAD 10 | td = tempdir(); 11 | qarchive.extract(wadf, outdir = td); 12 | 13 | testthat::expect_true(file.exists(file.path(td, 'brimstone2.qrs'))); 14 | }) 15 | 16 | 17 | testthat::test_that("We can extract a PAK file using qarchive.extract", { 18 | #pakf = system.file("extdata", "test.pak", package = "wal", mustWork = TRUE); 19 | pakf = file.path("~/data/PAK0.PAK"); 20 | if(! file.exists(pakf)) { 21 | testthat::skip("Test PAK file available"); 22 | } 23 | 24 | # extract PAK 25 | td = tempdir(); 26 | qarchive.extract(pakf, outdir = td); 27 | 28 | testthat::expect_true(file.exists(file.path(td, 'default.cfg'))); 29 | }) 30 | 31 | -------------------------------------------------------------------------------- /man/wal.export.to.png.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/export.R 3 | \name{wal.export.to.png} 4 | \alias{wal.export.to.png} 5 | \title{Export wal instance to PNG format image file.} 6 | \usage{ 7 | wal.export.to.png(wal, filepath, apply_palette = wal::pal_q2(), ...) 8 | } 9 | \arguments{ 10 | \item{wal}{a wal instance, as returned by \code{read.wal}} 11 | 12 | \item{filepath}{character string, path to the PNG file to write, including the file extension.} 13 | 14 | \item{apply_palette}{optional 256 x 3 integer matrix, the palette. Must contain values in range 0..255. Pass NULL if you do not want to apply any palette. The resulting \code{wal} object will not have an 'image' entry then.} 15 | 16 | \item{...}{extra parameters passed to \code{png::writePNG}.} 17 | } 18 | \description{ 19 | Export wal instance to PNG format image file. 20 | } 21 | \examples{ 22 | \dontrun{ 23 | walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 24 | wal = read.wal(walf); 25 | wal.export.to.png(wal, "~/basic1_7.png"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /man/plotwal.mipmap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{plotwal.mipmap} 4 | \alias{plotwal.mipmap} 5 | \title{Plot a mipmap level from a WAL image.} 6 | \usage{ 7 | plotwal.mipmap(wal, mip_level = 0L, apply_palette = wal::pal_q2()) 8 | } 9 | \arguments{ 10 | \item{wal}{a WAL image instance, as returned by \code{read.wal}.} 11 | 12 | \item{mip_level}{integer in range 0..3, the mipmap to plot. Level 0 is the original full-size image, the other ones get smaller and smaller (by factor 2 on each dimension, so 1/4th the size of their predecessor).} 13 | 14 | \item{apply_palette}{optional 256 x 3 integer matrix, the palette. Must contain values in range 0..255. Pass NULL if you do not want to apply any palette. The resulting \code{wal} object will not have an 'image' entry then.} 15 | } 16 | \description{ 17 | Plot a mipmap level from a WAL image. 18 | } 19 | \examples{ 20 | \dontrun{ 21 | walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 22 | wal = read.wal(walf); 23 | plotwal.mipmap(wal, mip_level = 3); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /man/wal.export.to.jpeg.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/export.R 3 | \name{wal.export.to.jpeg} 4 | \alias{wal.export.to.jpeg} 5 | \title{Export wal instance to JPEG format image file.} 6 | \usage{ 7 | wal.export.to.jpeg(wal, filepath, apply_palette = wal::pal_q2(), ...) 8 | } 9 | \arguments{ 10 | \item{wal}{a wal instance, as returned by \code{read.wal}} 11 | 12 | \item{filepath}{character string, path to the JPEG file to write, including the file extension.} 13 | 14 | \item{apply_palette}{optional 256 x 3 integer matrix, the palette. Must contain values in range 0..255. Pass NULL if you do not want to apply any palette. The resulting \code{wal} object will not have an 'image' entry then.} 15 | 16 | \item{...}{extra parameters passed to \code{jpeg::writeJPEG}. Can be used to set JPEG quality.} 17 | } 18 | \description{ 19 | Export wal instance to JPEG format image file. 20 | } 21 | \examples{ 22 | \dontrun{ 23 | walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 24 | wal = read.wal(walf); 25 | wal.export.to.jpeg(wal, "~/basic1_7.jpg"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /man/readWAL.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{readWAL} 4 | \alias{readWAL} 5 | \title{Read bitmap image in WAL format, returning image data only.} 6 | \usage{ 7 | readWAL(filepath, apply_palette = wal::pal_q2()) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, path to the file including extension} 11 | 12 | \item{apply_palette}{optional 256 x 3 integer matrix, the palette. Must contain values in range 0..255. Pass NULL if you do not want to apply any palette. The resulting \code{wal} object will not have an 'image' entry then.} 13 | } 14 | \value{ 15 | numeric matrix with dimension width x height x channels, with all color values in range 0..1. 16 | } 17 | \description{ 18 | Read a bitmap image in WAL format, and return data in the same format as \code{png::readPNG} and \code{jpeg::readJPEG} do. 19 | } 20 | \examples{ 21 | \dontrun{ 22 | walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 23 | wal_image = readWAL(walf); 24 | dim(wal_image); 25 | } 26 | 27 | } 28 | \seealso{ 29 | \code{read.wal} if you want to read the header and have more control. 30 | } 31 | -------------------------------------------------------------------------------- /man/read.wal.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{read.wal} 4 | \alias{read.wal} 5 | \title{Read bitmap file in WAL format.} 6 | \usage{ 7 | read.wal(filepath, hdr = TRUE, hdr_only = FALSE, apply_palette = wal::pal_q2()) 8 | } 9 | \arguments{ 10 | \item{filepath}{character string, path to the file including extension} 11 | 12 | \item{hdr}{logical, whether to return full list with header} 13 | 14 | \item{hdr_only}{logical, whether to read only the header} 15 | 16 | \item{apply_palette}{optional 256 x 3 integer matrix, the palette. Must contain values in range 0..255. Pass NULL if you do not want to apply any palette. The resulting \code{wal} object will not have an 'image' entry then.} 17 | } 18 | \value{ 19 | integer pixel matrix, each pixel value is in range 0-255 and refers to an index in a palette. The palette is NOT included in the file, so you will need to define one or get it from elsewhere to see the final image. 20 | } 21 | \description{ 22 | Read bitmap file in WAL format. 23 | } 24 | \examples{ 25 | \dontrun{ 26 | walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 27 | wal = read.wal(walf); 28 | plot(wal); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /man/plotwal.rawdata.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read_wal.R 3 | \name{plotwal.rawdata} 4 | \alias{plotwal.rawdata} 5 | \title{Plot raw pixel index data as image.} 6 | \usage{ 7 | plotwal.rawdata(raw_data, width, height, apply_palette = wal::pal_q2()) 8 | } 9 | \arguments{ 10 | \item{raw_data}{integer vector in containing width * height values in range 0..255, and optionally additional mipmap data at the end (which will be ignored). The raw image data. Can be a Q2 WAL data, Q1 miptex data, or anything else.} 11 | 12 | \item{width}{positive integer, the image width.} 13 | 14 | \item{height}{positive integer, the image height.} 15 | 16 | \item{apply_palette}{optional 256 x 3 integer matrix, the palette. Must contain values in range 0..255. Pass NULL if you do not want to apply any palette. The resulting \code{wal} object will not have an 'image' entry then.} 17 | } 18 | \description{ 19 | Plot raw pixel index data as image. 20 | } 21 | \examples{ 22 | \dontrun{ 23 | # Plot the Q1 shambler skin: 24 | mdl = read.quake.mdl("~/data/q1_pak/progs/shambler.mdl"); 25 | plotwal.rawdata(mdl$skins$skin_pic, mdl$header$skin_width, 26 | mdl$header$skin_height, apply_palette = pal_q1()); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # DO NOT CHANGE the "init" and "install" sections below 2 | 3 | # Download script file from GitHub 4 | init: 5 | ps: | 6 | $ErrorActionPreference = "Stop" 7 | Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" 8 | Import-Module '..\appveyor-tool.ps1' 9 | 10 | install: 11 | ps: Bootstrap 12 | 13 | cache: 14 | - C:\RLibrary 15 | 16 | environment: 17 | NOT_CRAN: true 18 | # env vars that may need to be set, at least temporarily, from time to time 19 | # see https://github.com/krlmlr/r-appveyor#readme for details 20 | # USE_RTOOLS: true 21 | # R_REMOTES_STANDALONE: true 22 | 23 | # Adapt as necessary starting from here 24 | 25 | build_script: 26 | - travis-tool.sh install_deps 27 | 28 | test_script: 29 | - travis-tool.sh run_tests 30 | 31 | on_failure: 32 | - 7z a failure.zip *.Rcheck\* 33 | - appveyor PushArtifact failure.zip 34 | 35 | artifacts: 36 | - path: '*.Rcheck\**\*.log' 37 | name: Logs 38 | 39 | - path: '*.Rcheck\**\*.out' 40 | name: Logs 41 | 42 | - path: '*.Rcheck\**\*.fail' 43 | name: Logs 44 | 45 | - path: '*.Rcheck\**\*.Rout' 46 | name: Logs 47 | 48 | - path: '\*_*.tar.gz' 49 | name: Bits 50 | 51 | - path: '\*_*.zip' 52 | name: Bits 53 | -------------------------------------------------------------------------------- /tests/testthat/test-read_model_mdl.R: -------------------------------------------------------------------------------- 1 | 2 | testthat::test_that("We can read a Quake I model in MDL format.", { 3 | #mdlf = system.file("extdata", "test.mdl", package = "wal", mustWork = TRUE); 4 | mdlf = file.path("~/data/q1_pak/progs/shambler.mdl"); # extract PAK0.PAK from your Quake CD. 5 | if(! file.exists(mdlf)) { 6 | testthat::skip("Test MDL file available"); 7 | } 8 | 9 | mdl = read.quake.mdl(mdlf); 10 | 11 | testthat::expect_true(is.quakemodel(mdl)); 12 | testthat::expect_false(is.quakemodel_md2(mdl)); 13 | testthat::expect_true(is.quakemodel_mdl(mdl)); 14 | 15 | testthat::expect_false(is.null(mdl$header)); 16 | testthat::expect_false(is.null(mdl$skins)); 17 | testthat::expect_false(is.null(mdl$triangles)); 18 | testthat::expect_false(is.null(mdl$frames)); 19 | 20 | # check model in in first frame (animation position) 21 | testthat::expect_equal(mdl$frames[[1]]$name, "stand1"); 22 | testthat::expect_equal(nrow(mdl$frames[[1]]$vertex_coords), 144L); # vertex count. 23 | testthat::expect_equal(ncol(mdl$frames[[1]]$vertex_coords), 3L); # x,y,z coords are in columns. 24 | 25 | # test conversion to fs.surface 26 | fs_surf_from_mdl = quakemodel.to.fs.surface(mdl); 27 | testthat::expect_false(is.null(fs_surf_from_mdl$faces)); 28 | testthat::expect_false(is.null(fs_surf_from_mdl$vertices)); 29 | }) 30 | -------------------------------------------------------------------------------- /tests/testthat/test-read_model_md2.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | testthat::test_that("We can read a Quake II model in MD2 format.", { 4 | #md2f = system.file("extdata", "test.md2", package = "wal", mustWork = TRUE); 5 | md2f = file.path("~/data/q2_pak/models/items/quaddama/tris.md2"); # extract PAK0.pak from your Quake II CD. 6 | if(! file.exists(md2f)) { 7 | testthat::skip("Test MD2 file available"); 8 | } 9 | 10 | md2 = read.quake.md2(md2f); 11 | 12 | testthat::expect_true(is.quakemodel(md2)); 13 | testthat::expect_true(is.quakemodel_md2(md2)); 14 | testthat::expect_false(is.quakemodel_mdl(md2)); 15 | 16 | testthat::expect_false(is.null(md2$header)); 17 | testthat::expect_false(is.null(md2$skins)); 18 | testthat::expect_false(is.null(md2$triangles)); 19 | testthat::expect_false(is.null(md2$frames)); 20 | testthat::expect_false(is.null(md2$glcmds)); 21 | 22 | # check model in in first frame (animation position) 23 | testthat::expect_equal(nrow(md2$frames[[1]]$vertex_coords), 78L); # vertex count. 24 | testthat::expect_equal(ncol(md2$frames[[1]]$vertex_coords), 3L); # x,y,z coords are in columns. 25 | 26 | # test conversion to fs.surface 27 | fs_surf_from_md2 = quakemodel.to.fs.surface(md2); 28 | testthat::expect_false(is.null(fs_surf_from_md2$faces)); 29 | testthat::expect_false(is.null(fs_surf_from_md2$vertices)); 30 | }) 31 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ${{ matrix.config.os }} 14 | 15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | config: 21 | - {os: windows-latest, r: 'release'} 22 | # - {os: macOS-latest, r: 'release'} 23 | - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} 24 | - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} 25 | env: 26 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 27 | R_KEEP_PKG_SOURCE: yes 28 | steps: 29 | - uses: actions/checkout@v4 30 | - uses: r-lib/actions/setup-pandoc@v2 31 | 32 | - uses: r-lib/actions/setup-r@v2 33 | with: 34 | use-public-rspm: true 35 | 36 | - uses: r-lib/actions/setup-r-dependencies@v2 37 | with: 38 | extra-packages: any::rcmdcheck 39 | needs: check 40 | 41 | - uses: r-lib/actions/check-r-package@v2 42 | -------------------------------------------------------------------------------- /man/closest.color.from.palette.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/image_to_wal.R 3 | \name{closest.color.from.palette} 4 | \alias{closest.color.from.palette} 5 | \title{Find closest color from palette for each RGB color.} 6 | \usage{ 7 | closest.color.from.palette(colors_rgb, fixed_palette_rgb) 8 | } 9 | \arguments{ 10 | \item{colors_rgb}{n x 3 integer matrix, the truecolor (arbitrary) input RGB colors for which you want to find the most similar colors included in the fixed palette. Range 0..255.} 11 | 12 | \item{fixed_palette_rgb}{the fixed palette, an n x 3 matrix of integers, representing the fixed palette colors in RGB values in range 0..255.} 13 | } 14 | \value{ 15 | vector of n integers, the index of the closest color into the palette for each of the \code{colors_rgb}. 16 | } 17 | \description{ 18 | Find closest color from a palette for given colors. The similarity method used to define 'closest' is deltaE, and the input RGB colors are transformed to LAB space for the computation, assuming they are given in sRGB space. 19 | } 20 | \examples{ 21 | colors_rgb = matrix(c(255, 0, 0, 100, 100, 100, 10, 10, 10, 5, 5, 5), 22 | ncol = 3, byrow = TRUE); 23 | fixed_palette_rgb = matrix(c(255, 0, 0, 255, 5, 0, 11, 11, 11, 0, 0, 0, 24 | 255, 255, 255), ncol = 3, byrow = TRUE); 25 | pal_similar_colors = closest.color.from.palette(colors_rgb, 26 | fixed_palette_rgb); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /man/img.to.wal.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/image_to_wal.R 3 | \name{img.to.wal} 4 | \alias{img.to.wal} 5 | \title{Convert image to WAL instance.} 6 | \usage{ 7 | img.to.wal(in_image, apply_palette = wal::pal_q2(), wal = wal.template()) 8 | } 9 | \arguments{ 10 | \item{in_image}{numeric matrix with 3 dimensions: widt, height, channels. Values must be in range 0..1. This is the image format returned by \code{jpeg::readJPEG} and \code{png::readPNG}. The image can have arbitrary colors, but the colors in the final WAL image will be limited to the palette. Both the width and height must be multiples of 8. Typical idtech1/2 textures use 32, 64, ..., 512. The reason is the mipmaps.} 11 | 12 | \item{apply_palette}{n x 3 integer matrix, the palette for the WAL image. This is not saved to the wal image, but still required because the colors from the \code{in_image} will be adapted to the palette colors (replaced with the most similar ones). If the palette does not cover the colors in the source image well, the resulting WAL image will look bad (dissimilar to the source image).} 13 | 14 | \item{wal}{a wal instance. Note that 1 will be substracted from the data when it is written, as indices are stored 0-based in the file.} 15 | } 16 | \value{ 17 | wal instance 18 | } 19 | \description{ 20 | Convert an input RGB image to a WAL instance, re-mapping its colors to the WAL palette in the process and generating the mipmaps. 21 | } 22 | \examples{ 23 | \dontrun{ 24 | wal = img.to.wal(jpeg::readJPEG("~/mytex.jpg")); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /tests/testthat/test-read_wal.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | testthat::test_that("We can read a WAL texture.", { 4 | walf = system.file("extdata", "bricks.wal", package = "wal", mustWork = TRUE); 5 | wal = wal::read.wal(walf); 6 | 7 | testthat::expect_equal(wal$header$tex_name, "e1u1/black"); 8 | testthat::expect_false(is.null(wal$image)); 9 | testthat::expect_true(max(wal$image) > 2L); # data range 0..255 10 | }) 11 | 12 | 13 | testthat::test_that("We can read a WAL texture with readWAL.", { 14 | walf = system.file("extdata", "bricks.wal", package = "wal", mustWork = TRUE); 15 | wal_image = wal::readWAL(walf); 16 | 17 | testthat::expect_true(is.array(wal_image)); 18 | testthat::expect_true(max(wal_image) <= 1.0); # data range 0..1 19 | }) 20 | 21 | 22 | testthat::test_that("We can plot a WAL instance including mipmaps, and with custom palettes.", { 23 | walf = system.file("extdata", "bricks.wal", package = "wal", mustWork = TRUE); 24 | wal = wal::read.wal(walf); 25 | 26 | plotwal.mipmap(wal, apply_palette = wal::pal_q2(), mip_level = 0); 27 | plotwal.mipmap(wal, apply_palette = wal::pal_q2(), mip_level = 1); 28 | plotwal.mipmap(wal, apply_palette = wal::pal_q2(), mip_level = 2); 29 | plotwal.mipmap(wal, apply_palette = wal::pal_q2(), mip_level = 3); 30 | 31 | plotwal.mipmap(wal, apply_palette = wal::pal_q1()); 32 | 33 | plot(wal); 34 | 35 | testthat::expect_error(plotwal.mipmap(wal, apply_palette = wal::pal_q2(), mip_level = 4)); # invalid mip_level 36 | }) 37 | 38 | 39 | 40 | 41 | 42 | # read the Quake 2 palette (requires extracted pak0.pak): 43 | # pcx_palette = pcx::read.pcx("~/data/q2_pak/pics/colormap.pcx") 44 | # pal = pcx$palette_rgb; 45 | 46 | -------------------------------------------------------------------------------- /tests/testthat/test-image_to_wal.R: -------------------------------------------------------------------------------- 1 | 2 | testthat::test_that("We can convert a JPEG image to WAL.", { 3 | 4 | jpgf = system.file("extdata", "Bricks050_256_Color.jpg", package = "wal", mustWork = TRUE); 5 | 6 | wal = wal::img.to.wal(jpeg::readJPEG(jpgf)); 7 | 8 | testthat::expect_equal(wal$header$height, 256L); 9 | testthat::expect_equal(wal$header$width, 256L); 10 | 11 | expected_data_length_full = sum(wal:::get.mipmap.data.lengths(256L, 256L)); 12 | testthat::expect_equal(expected_data_length_full, length(wal$file_data_all_mipmaps)); 13 | testthat::expect_equal(length(wal$raw_data), (256*256)); 14 | }) 15 | 16 | 17 | testthat::test_that("We can convert a PNG image to WAL.", { 18 | 19 | pngf = system.file("extdata", "Bricks050_256_Color.png", package = "wal", mustWork = TRUE); 20 | 21 | wal = wal::img.to.wal(png::readPNG(pngf)); 22 | 23 | testthat::expect_equal(wal$header$height, 256L); 24 | testthat::expect_equal(wal$header$width, 256L); 25 | 26 | expected_data_length_full = sum(wal:::get.mipmap.data.lengths(256L, 256L)); 27 | testthat::expect_equal(expected_data_length_full, length(wal$file_data_all_mipmaps)); 28 | testthat::expect_equal(length(wal$raw_data), (256*256)); 29 | }) 30 | 31 | 32 | testthat::test_that("We can half a single-channel image", { 33 | 34 | pngf = system.file("extdata", "Bricks050_256_Color.png", package = "wal", mustWork = TRUE); 35 | png_img = png::readPNG(pngf); 36 | testthat::expect_equal(dim(png_img), c(256L, 256L, 3L)); 37 | red_channel = png_img[,,1]; 38 | testthat::expect_equal(dim(red_channel), c(256L, 256L)); 39 | 40 | small_red_channel = wal:::half.image(red_channel); 41 | testthat::expect_equal(dim(small_red_channel), c(128L, 128L)); 42 | }) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /vignettes/wal.R: -------------------------------------------------------------------------------- 1 | ## ----------------------------------------------------------------------------- 2 | library("wal"); 3 | wal_file = system.file("extdata", "bricks.wal", package = "wal", mustWork = TRUE); 4 | wal_image = wal::readWAL(wal_file); 5 | 6 | ## ----------------------------------------------------------------------------- 7 | dim(wal_image); 8 | 9 | ## ----------------------------------------------------------------------------- 10 | wal = wal::read.wal(wal_file); 11 | 12 | ## ----------------------------------------------------------------------------- 13 | wal$header$width; 14 | 15 | ## ----------------------------------------------------------------------------- 16 | plot(wal); 17 | 18 | ## ----------------------------------------------------------------------------- 19 | plotwal.mipmap(wal, apply_palette = wal::pal_q2(), mip_level = 1) 20 | 21 | ## ----------------------------------------------------------------------------- 22 | plotwal.mipmap(wal, apply_palette = wal::pal_q1(), mip_level = 3) 23 | 24 | ## ----------------------------------------------------------------------------- 25 | plotwal.mipmap(wal, apply_palette = wal::pal_q2(), mip_level = 3) 26 | 27 | ## ---- eval = FALSE------------------------------------------------------------ 28 | # writeWAL("~/mytexture.wal", wal); 29 | 30 | ## ---- eval = FALSE------------------------------------------------------------ 31 | # wal.export.to.jpeg(wal, "~/mytexture.jpg"); 32 | # wal.export.to.png(wal, "~/mytexture.png"); 33 | 34 | ## ---- eval = FALSE------------------------------------------------------------ 35 | # wal_imported = img.to.wal(png::readPNG("~/mytexture.png")); 36 | # writeWAL("~/mytexture.wal", wal_imported); 37 | # 38 | # wal_imported = img.to.wal(jpeg::readJPEG("~/mytexture.jpg")); 39 | # writeWAL("~/mytexture.wal", wal_imported); 40 | 41 | -------------------------------------------------------------------------------- /R/read_model_common.R: -------------------------------------------------------------------------------- 1 | 2 | #' @title Check whether object is a Quake 1 or 2 alias model. 3 | #' 4 | #' @param x any R object 5 | #' 6 | #' @export 7 | is.quakemodel <- function(x) { 8 | return(inherits(x, 'quakemodel_mdl') | inherits(x, 'quakemodel_md2')); 9 | } 10 | 11 | 12 | #' @title Convert Quake Model to 'fs.surface' instance. 13 | #' 14 | #' @param quakemodel an instance of \code{quakemodel_mdl} or \code{quakemodel_md2}. 15 | #' 16 | #' @param frame_idx integer, the frame to export. Quake models may contain animations made up of several frames. The mesh connectivity is unaltered between frames, but the vertex positions differ. 17 | #' 18 | #' @return \code{fs.surface} mesh instance, as used by the \code{freesurferformats} package. 19 | #' 20 | #' @export 21 | quakemodel.to.fs.surface <- function(quakemodel, frame_idx = 1L) { 22 | sf = list('faces'=(quakemodel$triangles$vertex + 1L), 'vertices'=quakemodel$frames[[frame_idx]]$vertex_coords); 23 | class(sf) = c(class(sf), 'fs.surface'); 24 | return(sf); 25 | } 26 | 27 | 28 | #' #' @title Convert Quake Model to rgl 'tmesh3d' instance. 29 | #' #' 30 | #' #' @param quakemodel an instance of \code{quakemodel_mdl} or \code{quakemodel_md2}. 31 | #' #' 32 | #' #' @param frame_idx integer, the frame to export. Quake models may contain animations made up of several frames. The mesh connectivity is unaltered between frames, but the vertex positions differ. 33 | #' #' 34 | #' #' @return \code{tmesh3d} mesh instance, as used by the \code{rgl} package. You can use \code{rgl::shade3d(your_tmesh3d_instance)} to visualize the model. 35 | #' #' 36 | #' #' @export 37 | #' quakemodel.to.tmesh3d <- function(quakemodel, frame_idx = 1L) { 38 | #' sf = quakemodel.to.fs.surface(quakemodel, frame_idx = frame_idx); 39 | #' tm = rgl::tmesh3d(t(sf$vertices), t(sf$faces), homogeneous = FALSE); 40 | #' return(tm); 41 | #' } 42 | #' 43 | 44 | -------------------------------------------------------------------------------- /R/read_quake_archive.R: -------------------------------------------------------------------------------- 1 | 2 | #' @title Extract any of the supported Quake archives. 3 | #' 4 | #' @param filepath character string, path to existing and readable file in PAK or WAD2 format. 5 | #' 6 | #' @param outdir character string, path to an existing and writeable output directory into which to extract the archive. 7 | #' 8 | #' @param format character string, of one 'auto' to detect from filename, 'QARCHIVE_TYPE_WAD' for WAD2, or 'QARCHIVE_TYPE_PAK' for PACK. 9 | #' 10 | #' @param do_pre_checks logical, whether to perform extra sanity checks on the other parameters. 11 | #' 12 | #' @export 13 | qarchive.extract <- function(filepath, outdir, format = 'auto', do_pre_checks = TRUE) { 14 | 15 | if(do_pre_checks) { 16 | if(! file.exists(filepath)) { 17 | stop(sprintf("File '%s' does not exist or cannot be read. Please check or fix permissions.\n", filepath)); 18 | } 19 | if(! dir.exists(outdir)) { 20 | stop(sprintf("Output directory '%s' does not exist or cannot be read. Please create it or fix permissions.\n", outdir)); 21 | } 22 | } 23 | 24 | qarchive_type = qarchive.type.from.filename(filepath); 25 | if(qarchive_type == 'QARCHIVE_TYPE_WAD') { 26 | wad.extract(filepath, outdir); 27 | } else if(qarchive_type == 'QARCHIVE_TYPE_PAK') { 28 | pak.extract(filepath, outdir); 29 | } else { 30 | stop("Invalid or unsupported Quake archive type."); 31 | } 32 | } 33 | 34 | 35 | #' @title Determine archive type from file name extension. 36 | #' 37 | #' @inheritParams qarchive.extract 38 | #' 39 | #' @return character string, one of 'QARCHIVE_TYPE_WAD' or 'QARCHIVE_TYPE_PAK'. 40 | #' 41 | #' @keywords internal 42 | qarchive.type.from.filename <- function(filepath) { 43 | if(any(endsWith(filepath, c('.wad', '.WAD')))) { 44 | return('QARCHIVE_TYPE_WAD'); 45 | } else if(any(endsWith(filepath, c('.pak', '.PAK')))) { 46 | return('QARCHIVE_TYPE_PAK'); 47 | } else { 48 | warning(sprintf("Cannot guess archive type from file extension, trying 'PAK'.")); 49 | return('QARCHIVE_TYPE_PAK'); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /web/c/read_wal.c: -------------------------------------------------------------------------------- 1 | // read_wal.c -- read a WAL image file in C. 2 | // This file is distributed as part of the 'wal' R package repo and is covered by its license. 3 | // 4 | // Compilation and usage: 5 | // 6 | // gcc read_wal.c 7 | // ./a.out path/to/some_image.wal 8 | // 9 | // Written by Tim Schaefer, 2020-11-14. 10 | 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | int main(int argc, char *argv[]) { 18 | FILE *fh; 19 | 20 | if ((fh = fopen(argv[1], "r")) == NULL) { 21 | printf("Could not open file %s for reading\n", argv[1]); 22 | exit(1); 23 | } 24 | 25 | unsigned char tex_name[32]; 26 | fread(&tex_name, sizeof(tex_name), 1, fh); 27 | 28 | int width, height; 29 | fread(&width, sizeof(int), 1, fh); 30 | fread(&height, sizeof(int), 1, fh); 31 | 32 | int mip_level_offsets[4]; 33 | fread(&mip_level_offsets, sizeof(mip_level_offsets), 1, fh); 34 | 35 | unsigned char anim_name[32]; 36 | fread(&anim_name, sizeof(anim_name), 1, fh); 37 | 38 | int flags, contents, value; 39 | fread(&flags, sizeof(int), 1, fh); 40 | fread(&contents, sizeof(int), 1, fh); 41 | fread(&value, sizeof(int), 1, fh); 42 | 43 | printf("The tex name is %s\n", tex_name); 44 | printf("The width is %d\n", width); 45 | printf("The height is %d\n", height); 46 | printf("The mip level offsets are: %d, %d, %d, %d\n", mip_level_offsets[0], mip_level_offsets[1], mip_level_offsets[2], mip_level_offsets[3]); 47 | printf("The anim name is %s\n", anim_name); 48 | printf("The flags is %d\n", flags); 49 | printf("The contents is %d\n", contents); 50 | printf("The value is %d\n", value); 51 | 52 | // Read image data. Each entry represents a pixel and is an index (in range 0-255) into the color palette, which is not part of the file. 53 | // A WAL file contains several image resolutions (mipmaps), the following code reads the first (i.e., the highest-quality) image version. 54 | int pixel_data[width * height]; 55 | fseek(fh, mip_level_offsets[0], SEEK_SET); 56 | fread(&pixel_data, sizeof(pixel_data), 1, fh); 57 | 58 | // The pixel data should be reshaped to a matrix of width x height to use it. We don't do that here. 59 | 60 | fclose(fh); 61 | } 62 | -------------------------------------------------------------------------------- /R/write_wal.R: -------------------------------------------------------------------------------- 1 | 2 | ## 24 bit JPEG/PNG colors to indexed WAL colors: 3 | # convert RGB colors to LAB space (colorscience::rgb2x or see grDevices::convertColor, https://cran.r-project.org/web/packages/colordistance/vignettes/lab-analyses.html) 4 | # use deltaE metric to compute distances (colorscience::deltaE2000() or spacesXYZ::DeltaE()) 5 | # pick color with 6 | 7 | ## mimap issue: 8 | # find out storage order 9 | # find out width and height in pixels of the 3 smaller mipmap levels (depends on 1st, I guess) 10 | 11 | #' @title Write WAL instance to bitmap file in WAL format. 12 | #' 13 | #' @param filepath character string, path to the file including extension 14 | #' 15 | #' @param wal a wal instance. Note that 1 will be substracted from the data when it is written, as indices are stored 0-based in the file. 16 | #' 17 | #' @examples 18 | #' \dontrun{ 19 | #' walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 20 | #' wal = read.wal(walf); 21 | #' writeWAL(tempfile(fileext = ".wal"), wal); 22 | #' } 23 | #' 24 | #' @export 25 | writeWAL <- function(filepath, wal) { 26 | 27 | fh = file(filepath, "wb", blocking = TRUE); 28 | on.exit({ close(fh) }, add = TRUE); 29 | endian = "little"; 30 | 31 | if(nchar(wal$header$tex_name) > 0L) { 32 | if(nchar(wal$header$tex_name) > 32L) { 33 | stop("Max length for tex_name is 32."); 34 | } 35 | writeChar(wal$header$tex_name, fh, eos = NULL); 36 | } 37 | writeBin(as.raw(rep(0L, (32L - nchar(wal$header$tex_name)))), fh, endian = endian); # fill remaining space up to max 32 bytes with zeroes. 38 | 39 | writeBin(as.integer(wal$header$width), fh, size = 4, endian = endian); 40 | writeBin(as.integer(wal$header$height), fh, size = 4, endian = endian); 41 | writeBin(as.integer(wal$header$mip_level_offsets), fh, size = 4, endian = endian); # these are 4 integers. 42 | 43 | if(nchar(wal$header$anim_name) > 0L) { 44 | if(nchar(wal$header$anim_name) > 32L) { 45 | stop("Max length for anim_name is 32."); 46 | } 47 | writeChar(wal$header$anim_name, fh, eos = NULL); 48 | } 49 | writeBin(as.raw(rep(0L, (32L - nchar(wal$header$anim_name)))), fh, endian = endian); # fill remaining space up to max 32 bytes with zeroes. 50 | 51 | writeBin(as.integer(wal$header$flags), fh, size = 4, endian = endian); 52 | writeBin(as.integer(wal$header$contents), fh, size = 4, endian = endian); 53 | writeBin(as.integer(wal$header$value), fh, size = 4, endian = endian); 54 | 55 | # write data 56 | writeBin(as.integer(wal$file_data_all_mipmaps -1L), fh, size = 1, endian = endian); 57 | } 58 | -------------------------------------------------------------------------------- /R/export.R: -------------------------------------------------------------------------------- 1 | 2 | #' @title Export wal instance to JPEG format image file. 3 | #' 4 | #' @param wal a wal instance, as returned by \code{read.wal} 5 | #' 6 | #' @param filepath character string, path to the JPEG file to write, including the file extension. 7 | #' 8 | #' @param ... extra parameters passed to \code{jpeg::writeJPEG}. Can be used to set JPEG quality. 9 | #' 10 | #' @inheritParams read.wal 11 | #' 12 | #' @examples 13 | #' \dontrun{ 14 | #' walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 15 | #' wal = read.wal(walf); 16 | #' wal.export.to.jpeg(wal, "~/basic1_7.jpg"); 17 | #' } 18 | #' 19 | #' @importFrom jpeg writeJPEG 20 | #' @importFrom freesurferformats rotate3D flip3D 21 | #' @export 22 | wal.export.to.jpeg <- function(wal, filepath, apply_palette = wal::pal_q2(), ...) { 23 | if(! is.character(filepath)) { 24 | stop("Parameter 'filepath' must be a character string."); 25 | } 26 | if(is.null(apply_palette)) { 27 | stop("Paramter 'palette' must not be NULL"); 28 | } 29 | check.palette(apply_palette); 30 | 31 | channel_red = apply_palette[wal$raw_data, 1]; 32 | channel_green = apply_palette[wal$raw_data, 2]; 33 | channel_blue = apply_palette[wal$raw_data, 3]; 34 | 35 | jpeg_img = array( (c( channel_red , channel_green, channel_blue )) , dim = c( wal$header$width, wal$header$height, 3)) / 255.; 36 | jpeg_img = freesurferformats::rotate3D(jpeg_img, 3, 90) 37 | jpeg_img = freesurferformats::flip3D(jpeg_img, 1, "horizontally") 38 | jpeg::writeJPEG(jpeg_img, target = filepath, ...); 39 | } 40 | 41 | #' @title Export wal instance to PNG format image file. 42 | #' 43 | #' @param wal a wal instance, as returned by \code{read.wal} 44 | #' 45 | #' @param filepath character string, path to the PNG file to write, including the file extension. 46 | #' 47 | #' @param ... extra parameters passed to \code{png::writePNG}. 48 | #' 49 | #' @inheritParams read.wal 50 | #' 51 | #' @examples 52 | #' \dontrun{ 53 | #' walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 54 | #' wal = read.wal(walf); 55 | #' wal.export.to.png(wal, "~/basic1_7.png"); 56 | #' } 57 | 58 | #' 59 | #' @importFrom png writePNG 60 | #' @importFrom freesurferformats rotate3D flip3D 61 | #' @export 62 | wal.export.to.png <- function(wal, filepath, apply_palette = wal::pal_q2(), ...) { 63 | if(! is.character(filepath)) { 64 | stop("Parameter 'filepath' must be a character string."); 65 | } 66 | if(is.null(apply_palette)) { 67 | stop("Paramter 'palette' must not be NULL"); 68 | } 69 | check.palette(apply_palette); 70 | 71 | channel_red = apply_palette[wal$raw_data, 1]; 72 | channel_green = apply_palette[wal$raw_data, 2]; 73 | channel_blue = apply_palette[wal$raw_data, 3]; 74 | 75 | png_img = array( (c( channel_red , channel_green, channel_blue )) , dim = c( wal$header$width, wal$header$height, 3)) / 255.; 76 | png_img = freesurferformats::rotate3D(png_img, 3, 90) 77 | png_img = freesurferformats::flip3D(png_img, 1, "horizontally") 78 | png::writePNG(png_img, target = filepath, ...); 79 | } 80 | 81 | -------------------------------------------------------------------------------- /R/read_pak.R: -------------------------------------------------------------------------------- 1 | # Functions to read PAK archives. 2 | 3 | #' @title Read Quake PAK archive. 4 | #' 5 | #' @param filepath character string, path to the file including extension. 6 | #' 7 | #' @return a 'pak' instance. 8 | #' 9 | #' @examples 10 | #' \dontrun{ 11 | #' pakf = '~/.steam/steam/steamapps/common/Quake/Id1/PAK0.PAK'; 12 | #' pak = read.pak(pakf); 13 | #' } 14 | #' 15 | #' @export 16 | read.pak <- function(filepath) { 17 | fh = file(filepath, "rb"); 18 | on.exit({ close(fh) }); 19 | 20 | endian = 'little'; 21 | pak = list('header' = list('derived' = list())); 22 | 23 | pak$header$id = readChar(fh, 4L, useBytes = TRUE); 24 | 25 | if(pak$header$id != "PACK") { 26 | stop(sprintf("File '%s' not in Quake PACK format.\n", filepath)); 27 | } 28 | 29 | pak$header$ft_offset = readBin(fh, integer(), n = 1L, size = 4L, endian = endian); # file table offset. 30 | pak$header$ft_size = readBin(fh, integer(), n = 1, size = 4, endian = endian); # size of file table. 31 | pak$header$derived$num_files = pak$header$ft_size / 64L; 32 | 33 | # read file data. 34 | seek(fh, where = pak$header$ft_offset, origin = "start"); 35 | entry_file_names = rep(NA, pak$header$derived$num_files); 36 | entry_offsets = rep(NA, pak$header$derived$num_files); 37 | entry_sizes = rep(NA, pak$header$derived$num_files); 38 | for(entry_idx in 1:pak$header$derived$num_files) { 39 | entry_file_names[[entry_idx]] = readChar(fh, 56, useBytes = TRUE); 40 | entry_offsets[[entry_idx]] = readBin(fh, integer(), n = 1L, size = 4L, endian = endian); # file data offset. 41 | entry_sizes[[entry_idx]] = readBin(fh, integer(), n = 1L, size = 4L, endian = endian); # file data size. 42 | } 43 | pak$contents = data.frame('name' = entry_file_names, 'offset' = entry_offsets, 'size' = entry_sizes, stringsAsFactors = FALSE); 44 | 45 | class(pak) = c(class(pak), 'pak'); 46 | return(pak); 47 | } 48 | 49 | 50 | #' @title Extract PAK contents into existing directory. 51 | #' 52 | #' @param pak_filepath character string, path to input PAK file. 53 | #' 54 | #' @param outdir character string, the output directory in which the files should be created. Must be writeable. The sub directories and filenames are derived from the data in the WAD. 55 | #' 56 | #' @note PAK files can contain a directory structure, and new subdirectories will be created under \code{outdir} as needed to preserve it. 57 | #' 58 | #' @export 59 | pak.extract <- function(pak_filepath, outdir = getwd()) { 60 | if(! dir.exists(outdir)) { 61 | stop(sprintf("Base output directory '%d' does not exist.\n", outdir)); 62 | } 63 | pak = read.pak(pak_filepath); 64 | if(nrow(pak$contents) > 0L) { 65 | for(row_idx in 1:nrow(pak$contents)) { 66 | out_filename_with_dir_part = pak$contents$name[row_idx]; # something like 'e1u1/metal2_2'. 67 | 68 | out_subdirs = file.path(outdir, dirname(out_filename_with_dir_part)); 69 | if(! dir.exists(out_subdirs)) { 70 | dir.create(out_subdirs, recursive = TRUE); 71 | } 72 | 73 | out_filename = basename(out_filename_with_dir_part); 74 | 75 | out_filepath = file.path(out_subdirs, out_filename); 76 | save.filepart(pak_filepath, pak$contents$offset[row_idx], pak$contents$size[row_idx], out_filepath); 77 | } 78 | } else { 79 | warning("Empty PAK file."); 80 | } 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /vignettes/wal.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Reading and writing Quake WAL textures with wal" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Reading and writing Quake WAL textures with wal} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | \usepackage[utf8]{inputenc} 8 | --- 9 | 10 | In this document, we show how to read, write, import and export WAL textures, which are used by idtech1 and idtech2 games. 11 | 12 | 13 | ## Reading WAL files 14 | 15 | If you only need the pixel data of a WAL file, try this: 16 | 17 | ```{r} 18 | library("wal"); 19 | wal_file = system.file("extdata", "bricks.wal", package = "wal", mustWork = TRUE); 20 | wal_image = wal::readWAL(wal_file); 21 | ``` 22 | 23 | The return value is an array with 3 dimensions, representing image width, height, and channels. It contains RGB color values in range 0..1: 24 | 25 | ```{r} 26 | dim(wal_image); 27 | ``` 28 | 29 | 30 | To read the WAL file and get more detailed data, including the header and all mipmaps, read it into a wal instance instead: 31 | 32 | ```{r} 33 | wal = wal::read.wal(wal_file); 34 | ``` 35 | 36 | This allows you to do more things, like converting to other formats and re-writing to other files. The wal instance is a named list, feel free to explore it. E.g., to see the header information, do this: 37 | 38 | ```{r} 39 | wal$header$width; 40 | ``` 41 | 42 | ### Preview a WAL texture in R 43 | 44 | If you loaded a wal instance, you can plot it: 45 | 46 | ```{r} 47 | plot(wal); 48 | ``` 49 | 50 | This plots the largest mip level with the Quake 2 palette. If you need more control, e.g., you want to plot a different mip level or use a certain palette, use plotwal.mipmap instead: 51 | 52 | ```{r} 53 | plotwal.mipmap(wal, apply_palette = wal::pal_q2(), mip_level = 1) 54 | ``` 55 | 56 | The mipmaps are 0..3, where 0 is the largest (highest quality) version. Let's look at the lowest quality version with the Q1 palette: 57 | 58 | ```{r} 59 | plotwal.mipmap(wal, apply_palette = wal::pal_q1(), mip_level = 3) 60 | ``` 61 | 62 | As you can see, the Q1 palette fits this particular image worse than the Q2 palette: 63 | 64 | ```{r} 65 | plotwal.mipmap(wal, apply_palette = wal::pal_q2(), mip_level = 3) 66 | ``` 67 | 68 | ### Writing WAL image files 69 | 70 | You can write a WAL instance to a file like this: 71 | 72 | ```{r, eval = FALSE} 73 | writeWAL("~/mytexture.wal", wal); 74 | ``` 75 | 76 | 77 | ### Exporting to PNG and JPEG 78 | 79 | Exporting a WAL instance to JPEG or PNG format is straightforward: 80 | 81 | ```{r, eval = FALSE} 82 | wal.export.to.jpeg(wal, "~/mytexture.jpg"); 83 | wal.export.to.png(wal, "~/mytexture.png"); 84 | ``` 85 | 86 | ### Converting JPG or PNG images to WAL format 87 | 88 | This way is tricky for several reasons: WAL files must have certain dimensions and they use a fixed palette with 256 colors. This means that if you convert a JPG or PNG image, which can have 16 million different colors, to a file to WAL format, it will look different (unless, by coincidence, the image only consists of colors which occur in the palette). During the conversion, each color in the source image is replaced with the most similar color from the palette. Of course, different input colors may be mapped to the same palette color, so the quality will be worse. How much worse depends on how well the palette fits the source image. 89 | 90 | That all being said, if you have input files in PNG or JPEG format, you can covert them to WAL like this: 91 | 92 | ```{r, eval = FALSE} 93 | wal_imported = img.to.wal(png::readPNG("~/mytexture.png")); 94 | writeWAL("~/mytexture.wal", wal_imported); 95 | 96 | wal_imported = img.to.wal(jpeg::readJPEG("~/mytexture.jpg")); 97 | writeWAL("~/mytexture.wal", wal_imported); 98 | ``` 99 | 100 | The widths and heights of the input files must be a power of 2. Typical values used in the games are 16, 32, 64, 128, 256, and 512. The width and height for a single file be different. So 32x256 and 64x64 are fine, but 50x50 is not. When importing PNGs, also keep in mind that WAL does not support the alpha channel. 101 | 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wal 2 | Tools to work with Quake game assets in [R](https://www.r-project.org/). Useful for messing with Quake and Quake II game textures. Can read, write, create and export bitmap images in WAL file format and related texture formats. Includes tools to extract WAD and PAK files. Can read Quake and Quake II models in MDL and MD2 formats and allows for exporting to standard mesh formats. 3 | 4 | ![Vis](./vignettes/Bricks050_256_Color.jpg?raw=true "Example Game texture.") 5 | 6 | ## About 7 | 8 | The WAL file format is an old format for storing indexed bitmap images, used as textures in idtech1 and idtech2 games. A single WAL file contains four [mipmaps](https://en.wikipedia.org/wiki/Mipmap) of the same image. WAL format files are indexed but do **not** contain the palette needed to reconstruct the image. You can use your own palette, or use the Q1 or Q2 palettes that come with this package. 9 | 10 | Note that whe WAL textures are stored inside Quake pak file archives (`baseq2/pak0.pak`), so you will not see any WAL files in your Quake 2 directory unless you extracted them for mapping or modding. For Quake I, textures in a very similar format that is also supported are stored directly in the BSP level files (which are inside `Id1/PAK0.PAK`), or in WAD files. 11 | 12 | 13 | ## Package features 14 | 15 | Managing WAL images: 16 | 17 | * Reading WAL format images, including all mipmap levels. By default, the largest version is used. 18 | * Visualization of WAL images in R, including all mipmap levels (requires a palette). 19 | * Export of WAL images to PNG and JPEG format files (requires a palette). 20 | * Two important palettes are included: the Q1 and Q2 palettes. 21 | * Writing of a WAL image instance to a new file in WAL format. 22 | * Converting other images (PNG, JPEG, whatever) to WAL instances and files, including: 23 | * Mapping the colors of the source image to the most similar colors of the target WAL palette. This is done in LAB color space using DeltaE as the similarity measure. 24 | * Generation of the mipmaps. 25 | 26 | Related Formats and Features: 27 | 28 | * Reading WAD files in the 'WAD2' archive format used by Quake I and related games. These are tar-like archive files that hold collections of Quake assets (colormaps, textures in Q1 Mipmap Texture format, console pics, ...). Note that some games like Daikatana use a similar, but not identical format and still name their archives WAD files. These other WAD-like formats are currently not supported. Please open an issue if you need support for them. 29 | * Extracting WAD archives to a directory. 30 | * Reading, visualizing, and exporting Q1 Mipmap format textures. These are the textures used by Quake I, and they are typically stored directly in the binary maps (BSP files) or in WAD texture archives (see above). The Q1 Mipmap Texture format is very similar to the Quake II WAL format. 31 | * Reading and extracting 'PAK' archives. 32 | * Reading and exporting Quake models, including Quake MDL files and Quake II MD2 files. You can visualize them directly in R with 'rgl' or export them to standard model file formats like '.obj' format to open them in standard modeling software. 33 | 34 | 35 | ## Installation 36 | 37 | I recommend to install the [stable wal version from CRAN](https://cran.r-project.org/package=wal). In your R session: 38 | 39 | install.packages("wal"); 40 | 41 | 42 | To install the development version (no complaints or bug reports, please): 43 | 44 | install.packages("devtools"); 45 | devtools::install_github('dfsp-spirit/wal') 46 | 47 | 48 | ## Package API 49 | 50 | ### Reading and displaying WAL textures in R 51 | 52 | wal_file = "~/path/to/q2_pak0_extracted/textures/e1u2/basic1_7.wal"; 53 | wal = wal::read.wal(wal_file); 54 | 55 | plot(wal); # S3 plot method for 'wal' instances, uses preview palette. Alternatively: 56 | wal::plotwal.mipmap(wal, mip_level = 1L, apply_palette = wal::pal_q2()); # Plot with full control. 57 | 58 | ### Exporting WAL textures to other formats 59 | 60 | wal::wal.export.to.jpeg(wal, "~/mytex.jpg", palette = wal::pal_q1(), quality = 0.95); 61 | wal::wal.export.to.png(wal, "~/mytex.png"); # palette defaults to pal_q2(). 62 | 63 | 64 | ### Convert a JPEG or PNG image to WAL format 65 | 66 | jpeg_file = "~/mytextures/bricks_256x256.jpg"; 67 | wal = img.to.wal(jpeg::readJPEG(jpeg_file)); 68 | wal::writeWAL("~/myfile.wal", wal); 69 | 70 | For more information, read the [detailed vignette](https://cran.r-project.org/web/packages/wal/vignettes/wal.html) that comes with the package. Examples for working with models and archives can be found in the [unit tests](./tests/testthat/). 71 | 72 | ## References 73 | 74 | Documentation on the WAL format seems sparse. I did not find any official spec so I used the Quake 2 source code to learn about the format. Relevant files are: 75 | 76 | * [r_image.c](https://github.com/id-Software/Quake-2/blob/master/ref_soft/r_image.c) with WAL loading function `R_LoadWal` 77 | * To understand the latter, you will need to have a look at the `miptex_s` struct in [qfiles.h](https://github.com/id-Software/Quake-2/blob/master/qcommon/qfiles.h) and at the `image_s` struct in [r_local.h](https://github.com/id-Software/Quake-2/blob/master/ref_soft/r_local.h) 78 | 79 | ## Unit tests and CI 80 | 81 | 82 | [![R-CMD-check](https://github.com/dfsp-spirit/wal/workflows/R-CMD-check/badge.svg)](https://github.com/dfsp-spirit/wal/actions) 83 | 84 | [![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/dfsp-spirit/wal?branch=master&svg=true)](https://ci.appveyor.com/project/dfsp-spirit/wal) AppVeyor CI under Windows 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /R/palette.R: -------------------------------------------------------------------------------- 1 | 2 | #' @title Get Q2 palette. 3 | #' 4 | #' @return 256 x 3 integer matrix, representing the RGB color values for an index into the palette. 5 | #' 6 | #' @examples 7 | #' pal = pal_q2(); 8 | #' dim(pal); 9 | #' 10 | #' @export 11 | pal_q2 <- function() { 12 | q2pal = structure(c(0L, 15L, 31L, 47L, 63L, 75L, 91L, 107L, 123L, 139L, 13 | 155L, 171L, 187L, 203L, 219L, 235L, 99L, 91L, 83L, 79L, 71L, 14 | 63L, 59L, 51L, 47L, 43L, 39L, 35L, 27L, 23L, 19L, 15L, 95L, 91L, 15 | 91L, 87L, 83L, 79L, 71L, 63L, 59L, 51L, 47L, 39L, 35L, 27L, 23L, 16 | 19L, 143L, 123L, 115L, 103L, 207L, 167L, 139L, 111L, 235L, 203L, 17 | 175L, 147L, 119L, 91L, 63L, 35L, 167L, 159L, 151L, 139L, 127L, 18 | 115L, 103L, 87L, 75L, 67L, 59L, 51L, 43L, 35L, 27L, 19L, 123L, 19 | 115L, 107L, 103L, 95L, 87L, 83L, 75L, 67L, 63L, 55L, 47L, 39L, 20 | 31L, 23L, 15L, 111L, 95L, 83L, 67L, 55L, 39L, 27L, 15L, 179L, 21 | 191L, 203L, 215L, 203L, 179L, 159L, 135L, 115L, 91L, 71L, 47L, 22 | 23L, 19L, 15L, 11L, 7L, 7L, 7L, 0L, 0L, 0L, 0L, 0L, 139L, 131L, 23 | 123L, 115L, 107L, 99L, 91L, 87L, 75L, 63L, 51L, 43L, 31L, 19L, 24 | 11L, 0L, 151L, 143L, 135L, 127L, 119L, 115L, 107L, 99L, 91L, 25 | 79L, 67L, 55L, 47L, 35L, 23L, 15L, 159L, 147L, 139L, 127L, 119L, 26 | 107L, 99L, 87L, 79L, 67L, 55L, 43L, 31L, 23L, 11L, 0L, 119L, 27 | 111L, 103L, 99L, 91L, 83L, 75L, 71L, 63L, 55L, 47L, 39L, 35L, 28 | 27L, 19L, 11L, 155L, 143L, 135L, 123L, 115L, 103L, 95L, 87L, 29 | 75L, 63L, 55L, 47L, 35L, 27L, 19L, 11L, 0L, 35L, 63L, 83L, 95L, 30 | 95L, 95L, 255L, 255L, 255L, 255L, 255L, 255L, 255L, 255L, 255L, 31 | 255L, 255L, 239L, 227L, 211L, 199L, 183L, 171L, 155L, 143L, 127L, 32 | 115L, 95L, 71L, 47L, 27L, 239L, 55L, 255L, 0L, 43L, 27L, 19L, 33 | 235L, 195L, 159L, 123L, 235L, 199L, 167L, 135L, 159L, 0L, 15L, 34 | 31L, 47L, 63L, 75L, 91L, 107L, 123L, 139L, 155L, 171L, 187L, 35 | 203L, 219L, 235L, 75L, 67L, 63L, 59L, 55L, 47L, 43L, 39L, 35L, 36 | 31L, 27L, 23L, 19L, 15L, 15L, 11L, 95L, 91L, 83L, 79L, 75L, 71L, 37 | 63L, 59L, 55L, 47L, 43L, 39L, 35L, 27L, 23L, 19L, 119L, 99L, 38 | 91L, 79L, 151L, 123L, 103L, 83L, 159L, 139L, 119L, 99L, 79L, 39 | 59L, 39L, 23L, 59L, 47L, 43L, 39L, 31L, 23L, 23L, 19L, 15L, 15L, 40 | 15L, 11L, 11L, 11L, 7L, 7L, 95L, 87L, 83L, 79L, 71L, 67L, 63L, 41 | 55L, 51L, 47L, 39L, 35L, 27L, 23L, 15L, 11L, 59L, 55L, 47L, 43L, 42 | 35L, 27L, 19L, 11L, 91L, 123L, 155L, 187L, 215L, 199L, 183L, 43 | 167L, 151L, 135L, 119L, 103L, 83L, 75L, 67L, 63L, 55L, 47L, 39L, 44 | 31L, 23L, 15L, 7L, 0L, 87L, 79L, 71L, 67L, 59L, 51L, 47L, 43L, 45 | 35L, 31L, 27L, 19L, 15L, 11L, 7L, 0L, 159L, 151L, 139L, 131L, 46 | 123L, 115L, 107L, 99L, 91L, 79L, 67L, 55L, 47L, 35L, 23L, 15L, 47 | 75L, 67L, 59L, 55L, 47L, 43L, 35L, 31L, 27L, 23L, 19L, 15L, 11L, 48 | 7L, 0L, 0L, 123L, 115L, 107L, 99L, 91L, 87L, 79L, 71L, 63L, 55L, 49 | 47L, 39L, 31L, 23L, 15L, 7L, 171L, 159L, 151L, 139L, 131L, 119L, 50 | 111L, 103L, 91L, 79L, 67L, 59L, 47L, 35L, 23L, 15L, 255L, 231L, 51 | 211L, 187L, 167L, 143L, 123L, 255L, 255L, 255L, 255L, 255L, 255L, 52 | 235L, 215L, 191L, 171L, 147L, 127L, 107L, 87L, 71L, 59L, 43L, 53 | 31L, 23L, 15L, 7L, 0L, 0L, 0L, 0L, 0L, 55L, 0L, 0L, 43L, 27L, 54 | 19L, 151L, 115L, 87L, 63L, 211L, 171L, 139L, 107L, 91L, 0L, 15L, 55 | 31L, 47L, 63L, 75L, 91L, 107L, 123L, 139L, 155L, 171L, 187L, 56 | 203L, 219L, 235L, 35L, 31L, 31L, 27L, 27L, 23L, 23L, 19L, 19L, 57 | 19L, 15L, 15L, 11L, 11L, 7L, 7L, 111L, 103L, 95L, 91L, 83L, 75L, 58 | 67L, 59L, 55L, 47L, 43L, 39L, 35L, 27L, 23L, 19L, 83L, 67L, 59L, 59 | 47L, 75L, 59L, 47L, 39L, 39L, 35L, 31L, 27L, 23L, 15L, 11L, 7L, 60 | 43L, 35L, 27L, 19L, 15L, 11L, 7L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 61 | 0L, 0L, 75L, 67L, 63L, 59L, 55L, 51L, 47L, 43L, 39L, 35L, 27L, 62 | 23L, 19L, 15L, 11L, 7L, 23L, 23L, 23L, 23L, 19L, 15L, 11L, 7L, 63 | 79L, 111L, 147L, 183L, 223L, 211L, 195L, 183L, 167L, 155L, 139L, 64 | 127L, 111L, 103L, 91L, 83L, 75L, 63L, 51L, 43L, 31L, 19L, 11L, 65 | 0L, 87L, 79L, 71L, 67L, 59L, 51L, 47L, 43L, 35L, 31L, 27L, 19L, 66 | 15L, 11L, 7L, 0L, 123L, 115L, 107L, 99L, 95L, 87L, 79L, 71L, 67 | 67L, 59L, 51L, 43L, 35L, 27L, 19L, 11L, 63L, 55L, 47L, 39L, 35L, 68 | 27L, 23L, 19L, 15L, 11L, 11L, 7L, 7L, 0L, 0L, 0L, 207L, 195L, 69 | 183L, 167L, 155L, 143L, 127L, 115L, 103L, 87L, 75L, 63L, 47L, 70 | 35L, 23L, 7L, 123L, 111L, 99L, 87L, 75L, 67L, 59L, 51L, 39L, 71 | 27L, 19L, 11L, 7L, 0L, 0L, 0L, 0L, 15L, 27L, 39L, 47L, 51L, 51L, 72 | 255L, 211L, 167L, 127L, 83L, 39L, 31L, 23L, 15L, 7L, 0L, 0L, 73 | 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 255L, 74 | 0L, 255L, 35L, 23L, 15L, 127L, 83L, 51L, 27L, 199L, 155L, 119L, 75 | 87L, 83L), .Dim = c(256L, 3L)); 76 | return(q2pal); 77 | } 78 | 79 | 80 | #' @title Get Q1 palette. 81 | #' 82 | #' @return 256 x 3 integer matrix, representing the RGB color values for an index into the palette. 83 | #' 84 | #' @examples 85 | #' pal = pal_q1(); 86 | #' dim(pal); 87 | #' 88 | #' @export 89 | pal_q1 <- function() { 90 | palette_q1_file = system.file("extdata", "palette_q1.lmp", package = "wal", mustWork = TRUE); 91 | lmp = read.lmp(palette_q1_file); 92 | return(matrix(lmp, ncol = 3L, byrow = TRUE)); 93 | } 94 | 95 | 96 | 97 | #' @title Read binary lump, or 'lmp' files. 98 | #' 99 | #' @param filepath character string, path to the input file. 100 | #' 101 | #' @param dlength the expected data length, in bytes. 102 | #' 103 | #' @return vector of dlength unsigned integers in range 0..255. 104 | #' 105 | #' @keywords internal 106 | read.lmp <- function(filepath, dlength = 768L) { 107 | fh = file(filepath, "rb"); 108 | on.exit({ close(fh) }); 109 | 110 | endian = 'little'; 111 | 112 | raw_data = readBin(fh, integer(), n = dlength, size = 1L, signed = FALSE, endian = endian); 113 | return(raw_data); 114 | } 115 | -------------------------------------------------------------------------------- /R/image_to_wal.R: -------------------------------------------------------------------------------- 1 | 2 | #' @title Convert image to WAL instance. 3 | #' 4 | #' @description Convert an input RGB image to a WAL instance, re-mapping its colors to the WAL palette in the process and generating the mipmaps. 5 | #' 6 | #' @param in_image numeric matrix with 3 dimensions: widt, height, channels. Values must be in range 0..1. This is the image format returned by \code{jpeg::readJPEG} and \code{png::readPNG}. The image can have arbitrary colors, but the colors in the final WAL image will be limited to the palette. Both the width and height must be multiples of 8. Typical idtech1/2 textures use 32, 64, ..., 512. The reason is the mipmaps. 7 | #' 8 | #' @param apply_palette n x 3 integer matrix, the palette for the WAL image. This is not saved to the wal image, but still required because the colors from the \code{in_image} will be adapted to the palette colors (replaced with the most similar ones). If the palette does not cover the colors in the source image well, the resulting WAL image will look bad (dissimilar to the source image). 9 | #' 10 | #' @inheritParams writeWAL 11 | #' 12 | #' @return wal instance 13 | #' 14 | #' @examples 15 | #' \dontrun{ 16 | #' wal = img.to.wal(jpeg::readJPEG("~/mytex.jpg")); 17 | #' } 18 | #' 19 | #' @export 20 | img.to.wal <- function(in_image, apply_palette = wal::pal_q2(), wal = wal.template()) { 21 | if(length(dim(in_image)) != 3L) { 22 | stop("Parameter 'in_image' must have 3 dimensions: image width, height, and channels (R, G, B)."); 23 | } 24 | 25 | check.palette(apply_palette); 26 | 27 | wal$header$width = dim(in_image)[1]; 28 | wal$header$height = dim(in_image)[2]; 29 | num_channels = dim(in_image)[3]; 30 | 31 | if(num_channels != 3L) { 32 | stop("Parameter 'in_image': third dimension must have length 3 (channels R, G, B)."); 33 | } 34 | if(wal$header$width %% 8 != 0L) { 35 | stop(sprintf("Input image has invalid width %d, must be a multiple of 8.", wal$header$width)); 36 | } 37 | if(wal$header$height %% 8 != 0L) { 38 | stop(sprintf("Input image has invalid height %d, must be a multiple of 8.", wal$header$height)); 39 | } 40 | 41 | in_image = in_image * 255L; # convert data from range 0..1 to 0..255. 42 | in_image_matrix = matrix(in_image, c((wal$header$width * wal$header$height), 3L)); 43 | palette_col_indices = closest.color.from.palette(in_image_matrix, apply_palette); 44 | 45 | wal$file_data_all_mipmaps = expand.rawdata.to.mipmaps(palette_col_indices, wal$header$width, wal$header$height); 46 | wal$raw_data = wal$file_data_all_mipmaps[1:(wal$header$width * wal$header$height)]; 47 | 48 | wal$header$mip_level_offsets = get.mipmap.data.offsets(wal$header$width, wal$header$height); 49 | return(wal); 50 | } 51 | 52 | 53 | #' @title Compute length of mipmaps in bytes from width and height of largest image (mipmap0). 54 | #' 55 | #' @param mm0_width integer, width of mipmap 0 56 | #' 57 | #' @param mm0_height integer, height of mipmap 0 58 | #' 59 | #' @return integer vector of length 4, the lengths. 60 | #' 61 | #' @keywords internal 62 | get.mipmap.data.lengths <- function(mm0_width, mm0_height) { 63 | m0_l = mm0_width * mm0_height; 64 | m1_l = (mm0_width/2) * (mm0_height/2); 65 | m2_l = (mm0_width/4) * (mm0_height/4); 66 | m3_l = (mm0_width/8) * (mm0_height/8); 67 | return(c(m0_l, m1_l, m2_l, m3_l)); 68 | } 69 | 70 | 71 | #' @title Get mipmap offsets for WAL header, based on mipmap sizes and start offset. 72 | #' 73 | #' @inheritParams get.mipmap.data.lengths 74 | #' 75 | #' @param start_at integer, the offset at which the data starts in the file. Must be 100L for WAL format. 76 | #' 77 | #' @return integer vector of length 4, the offsets. 78 | #' 79 | #' @keywords internal 80 | get.mipmap.data.offsets <- function(mm0_width, mm0_height, start_at = 100L) { 81 | mipmaps_lengths = get.mipmap.data.lengths(mm0_width, mm0_height); 82 | m0_o = start_at; 83 | m1_o = start_at + mipmaps_lengths[1]; 84 | m2_o = start_at + mipmaps_lengths[1] + mipmaps_lengths[2]; 85 | m3_o = start_at + mipmaps_lengths[1] + mipmaps_lengths[2] + mipmaps_lengths[3]; 86 | return(c(m0_o, m1_o, m2_o, m3_o)); 87 | } 88 | 89 | 90 | 91 | #' @title Generate a WAL structure template. 92 | #' 93 | #' @description Generates a WAL instance that can be modified and filled with new data. The template represents a black 32x32 image (if the palette used to display it adhers to the convention that the first color is black). The indices used are 1-based (black is at index 1, not 0). 94 | #' 95 | #' @keywords internal 96 | wal.template <- function() { 97 | wal = list('header' = list()); 98 | 99 | wal$header$tex_name = "e1u1/black"; 100 | wal$header$width = 32L; 101 | wal$header$height = 32L; 102 | 103 | # the last (4th) offset is the beginning of the data for the last mipmap, the end is the EOF. 104 | wal$header$mip_level_offsets = c(100L, (100L + 32*32), (100L + 32*32 + 16*16), (100L + 32*32 + 16*16 + 8*8)); 105 | wal$header$anim_name = ""; 106 | wal$header$flags = 0L; 107 | wal$header$contents = 0L; 108 | wal$header$value = 0L; 109 | 110 | num_values = 32*32 + 16*16 + 8*8 + 4*4; # for all mipmaps 111 | wal$file_data_all_mipmaps = rep(1L, num_values); # the first value in the palette is black. 112 | 113 | wal$raw_data = wal$file_data_all_mipmaps[1:(32*32)]; 114 | 115 | class(wal) = c('wal', class(wal)); 116 | return(wal); 117 | } 118 | 119 | 120 | #' @title Given the pixel data for the largest image, generate the full data for all mipmaps. 121 | #' 122 | #' @param raw_data_mip_level0 integer vector or matrix, the image data for the largest mipmap. 123 | #' 124 | #' @param width integer, width of image for mip level 0 125 | #' 126 | #' @param height integer, width of image for mip level 0 127 | #' 128 | #' @keywords internal 129 | expand.rawdata.to.mipmaps <- function(raw_data_mip_level0, width, height, byrow = TRUE) { 130 | if(! is.matrix(raw_data_mip_level0)) { 131 | raw_data_mip_level0 = matrix(raw_data_mip_level0, ncol = width, byrow = byrow); 132 | } 133 | 134 | mip_level1 = half.image(raw_data_mip_level0, byrow = byrow); 135 | mip_level2 = half.image(mip_level1, byrow = byrow); 136 | mip_level3 = half.image(mip_level2, byrow = byrow); 137 | return(c(as.integer(raw_data_mip_level0), as.integer(mip_level1), as.integer(mip_level2), as.integer(mip_level3))); 138 | } 139 | 140 | 141 | #' @title Reduce image size by 2 along both axes by dropping pixels. 142 | #' 143 | #' @param image_data integer matrix, a 1-channel image. 144 | #' 145 | #' @keywords internal 146 | half.image <- function(image_data, byrow = TRUE) { 147 | if(! is.matrix(image_data)) { 148 | stop("Parameter 'image_data' must be a matrix."); 149 | } 150 | mip_data = rep(NA, ((ncol(image_data) / 2L) * (nrow(image_data) / 2L))); 151 | 152 | current_index = 1L; 153 | for(row_idx in 1:nrow(image_data)) { 154 | for(col_idx in 1:ncol(image_data)) { 155 | if(row_idx %% 2 == 0L) { 156 | if(col_idx %% 2 == 0L) { 157 | mip_data[current_index] = image_data[row_idx, col_idx]; 158 | current_index = current_index + 1L; 159 | } 160 | } 161 | } 162 | } 163 | 164 | mip_data = matrix(mip_data, ncol = (ncol(image_data) / 2L), byrow = byrow); 165 | return(mip_data); 166 | } 167 | 168 | 169 | #' @title Find closest color from palette for each RGB color. 170 | #' 171 | #' @description Find closest color from a palette for given colors. The similarity method used to define 'closest' is deltaE, and the input RGB colors are transformed to LAB space for the computation, assuming they are given in sRGB space. 172 | #' 173 | #' @param colors_rgb n x 3 integer matrix, the truecolor (arbitrary) input RGB colors for which you want to find the most similar colors included in the fixed palette. Range 0..255. 174 | #' 175 | #' @param fixed_palette_rgb the fixed palette, an n x 3 matrix of integers, representing the fixed palette colors in RGB values in range 0..255. 176 | #' 177 | #' @return vector of n integers, the index of the closest color into the palette for each of the \code{colors_rgb}. 178 | #' 179 | #' @examples 180 | #' colors_rgb = matrix(c(255, 0, 0, 100, 100, 100, 10, 10, 10, 5, 5, 5), 181 | #' ncol = 3, byrow = TRUE); 182 | #' fixed_palette_rgb = matrix(c(255, 0, 0, 255, 5, 0, 11, 11, 11, 0, 0, 0, 183 | #' 255, 255, 255), ncol = 3, byrow = TRUE); 184 | #' pal_similar_colors = closest.color.from.palette(colors_rgb, 185 | #' fixed_palette_rgb); 186 | #' 187 | #' @importFrom spacesXYZ DeltaE 188 | #' @importFrom grDevices convertColor 189 | #' @export 190 | closest.color.from.palette <- function(colors_rgb, fixed_palette_rgb) { 191 | if(max(colors_rgb) <= 1.0) { 192 | warning("Parameters 'colors_rgb': max data value <= 1.0, is the data in range 0-255?"); 193 | } 194 | if(max(fixed_palette_rgb) <= 1.0) { 195 | warning("Parameters 'fixed_palette_rgb': max data value <= 1.0, is the data in range 0-255?"); 196 | } 197 | colors_lab = grDevices::convertColor(colors_rgb, from="sRGB", to="Lab"); 198 | fixed_palette_lab = grDevices::convertColor(fixed_palette_rgb, from="sRGB", to="Lab"); 199 | result_indices = rep(NA, nrow(colors_rgb)); 200 | for(i in 1:nrow(colors_rgb)) { 201 | result_indices[i] = which.min(spacesXYZ::DeltaE(colors_lab[i,], fixed_palette_lab)); 202 | } 203 | return(result_indices); 204 | } 205 | -------------------------------------------------------------------------------- /R/read_wad.R: -------------------------------------------------------------------------------- 1 | # functions to read WAD files, which are archives holding several other files (think tar). 2 | 3 | 4 | #' @title Read Quake WAD file. 5 | #' 6 | #' @param filepath character string, path to the file. 7 | #' 8 | #' @return a wad instance, can be used to extract data or list contents. 9 | #' 10 | #' @examples 11 | #' \dontrun{ 12 | #' wadf = '~/knave.wad'; 13 | #' wad = read.wad(wadf); 14 | #' wad.contents(wad); 15 | #' } 16 | #' 17 | #' @export 18 | read.wad <- function(filepath) { 19 | fh = file(filepath, "rb"); 20 | on.exit({ close(fh) }); 21 | 22 | endian = 'little'; 23 | 24 | wad = list('header' = list()); 25 | 26 | wad$header$magic = readChar(fh, 4L); 27 | 28 | if(wad$header$magic != "WAD2") { 29 | stop("Not a supported WAD file: file magic mismatch."); 30 | } 31 | 32 | wad$header$num_entries = readBin(fh, integer(), n = 1, size = 4, endian = endian); 33 | wad$header$dir_offset = readBin(fh, integer(), n = 1, size = 4, endian = endian); 34 | 35 | seek(fh, where = wad$header$dir_offset, origin = "start"); 36 | 37 | wadentry_offset = c(); 38 | wadentry_dsize = c(); 39 | wadentry_size = c(); 40 | wadentry_type = c(); 41 | wadentry_compression = c(); 42 | wadentry_dummy = c(); 43 | wadentry_dir_name = c(); 44 | 45 | for(entry_idx in 1L:wad$header$num_entries) { 46 | wadentry_offset = c(wadentry_offset, readBin(fh, integer(), n = 1, size = 4, endian = endian)); 47 | wadentry_dsize = c(wadentry_dsize, readBin(fh, integer(), n = 1, size = 4, endian = endian)); 48 | wadentry_size = c(wadentry_size, readBin(fh, integer(), n = 1, size = 4, endian = endian)); 49 | wadentry_type = c(wadentry_type, readBin(fh, integer(), n = 1, size = 1, signed = FALSE, endian = endian)); 50 | wadentry_compression = c(wadentry_compression, readBin(fh, integer(), n = 1, size = 1, signed = FALSE, endian = endian)); 51 | wadentry_dummy = c(wadentry_dummy, readBin(fh, integer(), n = 1, size = 2, signed = FALSE, endian = endian)); 52 | wadentry_dir_name = c(wadentry_dir_name, readChar(fh, 16L)); 53 | } 54 | 55 | wadentry_type_string = get.wadentry.type.strings(wadentry_type); 56 | 57 | wad$contents = data.frame("offset" = wadentry_offset, "dsize" = wadentry_dsize, "size" = wadentry_size, "type" = wadentry_type, "type_string" = wadentry_type_string, "compression" = wadentry_compression, "dummy" = wadentry_dummy, "dir_name" = wadentry_dir_name, stringsAsFactors = FALSE); 58 | 59 | class(wad) = c(class(wad), "wad"); 60 | return(wad); 61 | } 62 | 63 | 64 | #' @title Translate wad directory entry types from the integer to the string representation. 65 | #' 66 | #' @param wadentry_type_int integer, WAD entry type code 67 | #' 68 | #' @return type string 69 | #' 70 | #' @keywords internal 71 | get.wadentry.type.strings <- function(wadentry_type_int) { 72 | wadentry_string = rep("unknown", length(wadentry_type_int)); 73 | cr = 1L; 74 | for(wti in wad_dir.types.int()) { 75 | wadentry_string[which(wadentry_type_int == wti)] = wad_dir.types.string()[cr]; 76 | cr = cr + 1L; 77 | } 78 | return(wadentry_string); 79 | } 80 | 81 | #' @title Get integers representing WAD dir entry types. 82 | #' 83 | #' @keywords internal 84 | #' 85 | #' @seealso wad_dir.types.string 86 | wad_dir.types.int <- function() { 87 | return(c(64L, 66L, 68L, 69L)); 88 | } 89 | 90 | 91 | #' @title Read part of binary file and save as new file. 92 | #' 93 | #' @param infile for input file, part of it gets read. 94 | #' 95 | #' @param read_from integer, index at which to start reading, from start of file. Used to \code{seek} to the position. 96 | #' 97 | #' @param read_len integer, the number of bytes to read. 98 | #' 99 | #' @param outfile character string, the output filename. 100 | #' 101 | #' @keywords internal 102 | save.filepart <- function(infile, read_from, read_len, outfile) { 103 | fh = file(infile, "rb"); 104 | on.exit({ close(fh) }); 105 | endian = "little"; 106 | 107 | read_from = as.integer(read_from); 108 | if(read_from < 0L) { stop("Invalid read_from parameter."); } 109 | read_len = as.integer(read_len); 110 | if(read_len < 0L) { stop("Invalid read_len parameter."); } 111 | 112 | #cat(sprintf("Writing %d bytes starting at %d to new file %s.\n", read_len, read_from, outfile)) 113 | 114 | seek(fh, where = read_from, origin = "start"); 115 | raw_data = readBin(fh, raw(), n = read_len, size = 1L, endian = endian); 116 | close(fh); 117 | 118 | if(length(raw_data) != read_len) { 119 | warning(sprintf("Extracted filepart length mismatch: expected %d, read %d.\n", read_len, length(raw_data))); 120 | } 121 | 122 | fh_out = file(outfile, "wb"); 123 | on.exit({ close(fh_out) }); 124 | writeBin(raw_data, fh_out); 125 | flush(fh_out); 126 | } 127 | 128 | 129 | #' @title S3 print function for WAD 130 | #' 131 | #' @param x wad instance 132 | #' 133 | #' @param ... extra arguments, ignored 134 | #' 135 | #' @export 136 | print.wad <- function(x, ...) { 137 | num_palettes = length(which(x$contents$type == 64L)); 138 | num_pic_statbar = length(which(x$contents$type == 66L)); 139 | num_tex = length(which(x$contents$type == 68L)); 140 | num_pic_console = length(which(x$contents$type == 69L)); 141 | cat(sprintf("WAD file holding %d palettes, %d statbar pics, %d textures and %d console pics.\n", num_palettes, num_pic_statbar, num_tex, num_pic_console)); 142 | } 143 | 144 | 145 | #' @title Get strings describing WAD dir entry types. 146 | #' 147 | #' @keywords internal 148 | #' 149 | #' @seealso wad_dir.types.int 150 | wad_dir.types.string <- function() { 151 | return(c("color_palette", "pic_status_bar", "texture", "pic_console")); 152 | } 153 | 154 | 155 | #' @title Get file extensions for WAD dir entry type strings. 156 | #' 157 | #' @keywords internal 158 | #' 159 | #' @return named list, which maps \code{wad_dir.types.string}s to file extensions. Afaik, there are not standard file extensions for these file types, and I made the ones used here up. 160 | #' 161 | #' @seealso wad_dir.types.string 162 | wad_dir.fileext.mapping <- function() { 163 | ext_mapping = list("color_palette" = '.qpl', "pic_status_bar" = '.psb', "texture" = '.q1t', "pic_console" = '.pco', "default" = '.qrs'); 164 | return(ext_mapping); 165 | } 166 | 167 | 168 | #' @title List WAD file contents. 169 | #' 170 | #' @param wad a wad instance, see \code{read.wad}. Alternatively a character string, which will be interpreted as a filepath to a WAD file that should be loaded. 171 | #' 172 | #' @return data.frame, info on the files inside the wad. 173 | #' @export 174 | wad.contents <- function(wad) { 175 | if(is.character(wad)) { 176 | wad = read.wad(wad); 177 | } 178 | contents = data.frame("type" = wad$contents$type_string, "name" = wad$contents$dir_name, "size" = wad$contents$size, stringsAsFactors = FALSE); 179 | return(contents); 180 | } 181 | 182 | 183 | #' @title Extract WAD contents into existing directory. 184 | #' 185 | #' @param wad_filepath character string, path to input WAD file. 186 | #' 187 | #' @param outdir character string, the output directory in which the files should be created. The filenames are derived from the data in the WAD. 188 | #' 189 | #' @param file_ext_mapping named list, with keys corresponding to the type names and values are file extensions, including the dot, to use for them. 190 | #' 191 | #' @note One can read extracted textures with \code{read.quake1miptex()}. 192 | #' 193 | #' @export 194 | wad.extract <- function(wad_filepath, outdir = getwd(), file_ext_mapping = wad_dir.fileext.mapping()) { 195 | wad = read.wad(wad_filepath); 196 | if(nrow(wad$contents) > 0L) { 197 | for(row_idx in 1:nrow(wad$contents)) { 198 | out_filename_cleaned = wad.texname.clean(wad$contents$dir_name[row_idx]); 199 | 200 | file_ext = file_ext_mapping$default; 201 | if(wad$contents$type_string[row_idx] %in% file_ext_mapping) { 202 | file_ext = file_ext_mapping[[wad$contents$type_string[row_idx]]]; 203 | } 204 | 205 | out_filepath = file.path(outdir, paste(out_filename_cleaned, file_ext, sep="")); 206 | save.filepart(wad_filepath, wad$contents$offset[row_idx], wad$contents$dsize[row_idx], out_filepath); 207 | } 208 | } else { 209 | warning("Empty WAD file."); 210 | } 211 | } 212 | 213 | 214 | #' @title Replace special chars in texture names to turn it into a valid filename. 215 | #' 216 | #' @param texnames character string, texture names from a WAD file. The textures may contain the special characters '*' and '+', which are used to indicate sequences (textures that change on an event, like a pressed button turning from red to green) and other things. 217 | #' 218 | #' @return character strings usable as filenames. 219 | #' 220 | #' @keywords internal 221 | wad.texname.clean <- function(texnames) { 222 | texnames = gsub("\\*", "s__", texnames); 223 | texnames = gsub("\\+", "p__", texnames); 224 | return(texnames); 225 | } 226 | 227 | 228 | #' @title Read a Quake mipmap texture from a WAD2 file. 229 | #' 230 | #' @param filepath character string, path to WAD file. 231 | #' 232 | #' @param at_offset integer, the index in the WAD file where the texture starts. 233 | #' 234 | #' @return a 'qmiptex' instance, its like a wall with shorter name field (16 instead of 32) and some fields (anim_name, flags, contents, value) missing. 235 | #' 236 | #' @examples 237 | #' \dontrun{ 238 | #' qm = read.quake1miptex("~/knave.wad", at_offset = 1317632); 239 | #' plotwal.mipmap(qm, apply_palette = pal_q1()); 240 | #' } 241 | #' 242 | #' @export 243 | read.quake1miptex <- function(filepath, at_offset = 0L) { 244 | fh = file(filepath, "rb"); 245 | on.exit({ close(fh) }); 246 | endian = "little"; 247 | 248 | seek(fh, where = at_offset, origin = "start"); 249 | qtex = list('header' = list()); 250 | class(qtex) = c(class(qtex), 'qmiptex', 'wal'); 251 | 252 | num_mip_levels = 4L; 253 | 254 | qtex$header$name = readChar(fh, 16L); 255 | qtex$header$width = readBin(fh, integer(), n = 1, size = 4, endian = endian); 256 | qtex$header$height = readBin(fh, integer(), n = 1, size = 4, endian = endian); 257 | qtex$header$mip_level_offsets = readBin(fh, integer(), n = num_mip_levels, size = 4, endian = endian); 258 | 259 | if(qtex$header$width <= 0L | qtex$header$height <= 0L) { 260 | stop("Invalid mipmap texture image dimensions"); 261 | } 262 | 263 | data_length_all_mipmaps = sum(get.mipmap.data.lengths(qtex$header$width, qtex$header$height)); 264 | pixel_data = readBin(fh, integer(), n = data_length_all_mipmaps, size = 1L, signed = FALSE, endian = endian); 265 | qtex$file_data_all_mipmaps = pixel_data + 1L; 266 | qtex$raw_data = get.wal.mipmap.data(qtex, mip_level = 0L); 267 | return(qtex); 268 | } 269 | -------------------------------------------------------------------------------- /R/read_model_md2.R: -------------------------------------------------------------------------------- 1 | # Functions for reading Quake II models ('.md2' files). 2 | 3 | 4 | 5 | #' @title Read Quake II model in MD2 format. 6 | #' 7 | #' @param filepath character string, the path to the MD2 file 8 | #' 9 | #' @param anim logical, whether to load the whole animation (if present). Returns a list of models, the animation frames. If FALSE, only the first frame is returned. 10 | #' 11 | #' @note Ignore this function, it will be moved to a different package. 12 | #' 13 | #' @export 14 | read.quake.md2 <- function(filepath, anim = FALSE) { 15 | fh = file(filepath, "rb"); 16 | on.exit({ close(fh) }); 17 | 18 | endian = 'little'; 19 | 20 | md2 = list(); 21 | header = list(); 22 | 23 | header$ident = readBin(fh, integer(), n = 1, size = 4, endian = endian); 24 | header$version = readBin(fh, integer(), n = 1, size = 4, endian = endian); 25 | 26 | if(header$ident != 844121161 | header$version != 8L) { 27 | stop(sprintf("File '%s' not in MD2 format.\n", filepath)); 28 | } 29 | 30 | hdr_data = readBin(fh, integer(), n = 15, size = 4, endian = endian); 31 | header$skinwidth = hdr_data[1]; 32 | header$skinheight = hdr_data[2]; 33 | header$framesize = hdr_data[3]; 34 | header$num_skins = hdr_data[4]; 35 | header$num_vertices = hdr_data[5]; 36 | header$num_st = hdr_data[6]; 37 | header$num_tris = hdr_data[7]; 38 | header$num_glcmds = hdr_data[8]; 39 | header$num_frames = hdr_data[9]; 40 | header$offset_skins = hdr_data[10]; 41 | header$offset_st = hdr_data[11]; 42 | header$offset_tris = hdr_data[12]; 43 | header$offset_frames = hdr_data[13]; 44 | header$offset_glcmds = hdr_data[14]; 45 | header$offset_end = hdr_data[15]; 46 | 47 | # read model data: skins (a.k.a. textures) 48 | seek(fh, where = header$offset_skins, origin = "start"); 49 | md2$skins = list(); 50 | if(header$num_skins > 0L) { 51 | for(i in 1:header$num_skins) { 52 | #md2$skins[[i]] = readBin(fh, character()); 53 | md2$skins[[i]] = readChar(fh, 64L, useBytes = TRUE); 54 | } 55 | } 56 | 57 | # read model data: texture coords 58 | seek(fh, where = header$offset_st, origin = "start"); 59 | md2$texcoords = list(); 60 | md2$texcoords_unscaled = list(); 61 | if(header$num_st > 0L) { 62 | md2$texcoords$s= rep(NA, (header$num_st)); 63 | md2$texcoords$t= rep(NA, (header$num_st)); 64 | md2$texcoords_unscaled$s= rep(NA, (header$num_st)); 65 | md2$texcoords_unscaled$t= rep(NA, (header$num_st)); 66 | for(i in 1:header$num_st) { 67 | md2$texcoords_unscaled$s[[i]] = readBin(fh, integer(), n = 1L, size = 2L); 68 | md2$texcoords_unscaled$t[[i]] = readBin(fh, integer(), n = 1L, size = 2L); 69 | } 70 | md2$texcoords$s = md2$texcoords_unscaled$s / header$skinwidth; 71 | md2$texcoords$t = md2$texcoords_unscaled$t / header$skinheight; 72 | } 73 | 74 | # read model data: triangles (vertex and texture indices) 75 | seek(fh, where = header$offset_tris, origin = "start"); 76 | md2$triangles = list(); 77 | if(header$num_tris > 0L) { 78 | md2$triangles$vertex = matrix(rep(NA, (header$num_tris * 3L)), ncol = 3); # vertex indices 79 | md2$triangles$st = matrix(rep(NA, (header$num_tris * 3L)), ncol = 3); # texture coord indices 80 | for(i in 1:header$num_tris) { 81 | md2$triangles$vertex[i,] = readBin(fh, integer(), n = 3L, size = 2L, signed = FALSE); 82 | md2$triangles$st[i,] = readBin(fh, integer(), n = 3L, size = 2L, signed = FALSE); 83 | } 84 | } 85 | 86 | if(max(md2$triangles$vertex) >= header$num_vertices){ 87 | warning(sprintf("Found triangle referencing 0-based vertex index %d, but there are only %d vertices.\n", max(md2$triangles$vertex) >= header$num_vertices)); 88 | } 89 | 90 | # read model data: openGL commands 91 | seek(fh, where = header$offset_glcmds, origin = "start"); 92 | if(header$num_glcmds > 0L) { 93 | md2$glcmds = readBin(fh, integer(), n = header$num_glcmds, size = 4L); 94 | } 95 | 96 | 97 | ## inside frame loop: 98 | seek(fh, where = header$offset_frames, origin = "start"); 99 | 100 | md2$frames = list(); 101 | if(header$num_frames > 0L) { 102 | pdn = predefined.md2.normals(); 103 | for(i in 1:header$num_frames) { 104 | this_frame = list(); 105 | # read model data: vertex coords (and normal vector indices into pre-defined normal vector) 106 | this_frame$scale = readBin(fh, numeric(), n = 3L, size = 4L); 107 | this_frame$translate = readBin(fh, numeric(), n = 3L, size = 4L); 108 | this_frame$name = readChar(fh, 16L, useBytes = TRUE); 109 | if(header$num_vertices > 0L) { 110 | this_frame$vertex_coords = matrix(rep(NA, (3 * header$num_vertices)), ncol = 3L); 111 | this_frame$vertex_normals = matrix(rep(NA, (3 * header$num_vertices)), ncol = 3L); 112 | for(j in 1:header$num_vertices) { 113 | this_vert_coords_raw = readBin(fh, integer(), n = 3L, size = 1L, signed = FALSE); 114 | this_vert_normal_index = readBin(fh, integer(), n = 1L, size = 1L, signed = FALSE); 115 | 116 | # compute real vertex coords using frame scale and translation 117 | this_frame$vertex_coords[j,] = (this_frame$scale * this_vert_coords_raw) + this_frame$translate; 118 | this_frame$vertex_normals[j,] = pdn[this_vert_normal_index,]; 119 | } 120 | } 121 | md2$frames[[i]] = this_frame; 122 | } 123 | } 124 | 125 | expected_framesize = 12L + 12L + 16L + (4L * header$num_vertices); 126 | if(header$framesize != expected_framesize) { 127 | warning(sprintf("Framesize from header is %d, expected %d.\n", header$framesize, expected_framesize)); 128 | } 129 | 130 | md2$header = header; 131 | class(md2) = c(class(md2), 'quakemodel_md2'); 132 | return(md2); 133 | } 134 | 135 | 136 | #' @title Predefined MD2 normals from Quake 2. 137 | #' 138 | #' @return 3xn matrix of normals. 139 | #' 140 | #' @keywords internal 141 | predefined.md2.normals <- function() { 142 | normals_raw = c( 143 | -0.525731, 0.000000, 0.850651 , 144 | -0.442863, 0.238856, 0.864188 , 145 | -0.295242, 0.000000, 0.955423 , 146 | -0.309017, 0.500000, 0.809017 , 147 | -0.162460, 0.262866, 0.951056 , 148 | 0.000000, 0.000000, 1.000000 , 149 | 0.000000, 0.850651, 0.525731 , 150 | -0.147621, 0.716567, 0.681718 , 151 | 0.147621, 0.716567, 0.681718 , 152 | 0.000000, 0.525731, 0.850651 , 153 | 0.309017, 0.500000, 0.809017 , 154 | 0.525731, 0.000000, 0.850651 , 155 | 0.295242, 0.000000, 0.955423 , 156 | 0.442863, 0.238856, 0.864188 , 157 | 0.162460, 0.262866, 0.951056 , 158 | -0.681718, 0.147621, 0.716567 , 159 | -0.809017, 0.309017, 0.500000 , 160 | -0.587785, 0.425325, 0.688191 , 161 | -0.850651, 0.525731, 0.000000 , 162 | -0.864188, 0.442863, 0.238856 , 163 | -0.716567, 0.681718, 0.147621 , 164 | -0.688191, 0.587785, 0.425325 , 165 | -0.500000, 0.809017, 0.309017 , 166 | -0.238856, 0.864188, 0.442863 , 167 | -0.425325, 0.688191, 0.587785 , 168 | -0.716567, 0.681718, -0.147621 , 169 | -0.500000, 0.809017, -0.309017 , 170 | -0.525731, 0.850651, 0.000000 , 171 | 0.000000, 0.850651, -0.525731 , 172 | -0.238856, 0.864188, -0.442863 , 173 | 0.000000, 0.955423, -0.295242 , 174 | -0.262866, 0.951056, -0.162460 , 175 | 0.000000, 1.000000, 0.000000 , 176 | 0.000000, 0.955423, 0.295242 , 177 | -0.262866, 0.951056, 0.162460 , 178 | 0.238856, 0.864188, 0.442863 , 179 | 0.262866, 0.951056, 0.162460 , 180 | 0.500000, 0.809017, 0.309017 , 181 | 0.238856, 0.864188, -0.442863 , 182 | 0.262866, 0.951056, -0.162460 , 183 | 0.500000, 0.809017, -0.309017 , 184 | 0.850651, 0.525731, 0.000000 , 185 | 0.716567, 0.681718, 0.147621 , 186 | 0.716567, 0.681718, -0.147621 , 187 | 0.525731, 0.850651, 0.000000 , 188 | 0.425325, 0.688191, 0.587785 , 189 | 0.864188, 0.442863, 0.238856 , 190 | 0.688191, 0.587785, 0.425325 , 191 | 0.809017, 0.309017, 0.500000 , 192 | 0.681718, 0.147621, 0.716567 , 193 | 0.587785, 0.425325, 0.688191 , 194 | 0.955423, 0.295242, 0.000000 , 195 | 1.000000, 0.000000, 0.000000 , 196 | 0.951056, 0.162460, 0.262866 , 197 | 0.850651, -0.525731, 0.000000 , 198 | 0.955423, -0.295242, 0.000000 , 199 | 0.864188, -0.442863, 0.238856 , 200 | 0.951056, -0.162460, 0.262866 , 201 | 0.809017, -0.309017, 0.500000 , 202 | 0.681718, -0.147621, 0.716567 , 203 | 0.850651, 0.000000, 0.525731 , 204 | 0.864188, 0.442863, -0.238856 , 205 | 0.809017, 0.309017, -0.500000 , 206 | 0.951056, 0.162460, -0.262866 , 207 | 0.525731, 0.000000, -0.850651 , 208 | 0.681718, 0.147621, -0.716567 , 209 | 0.681718, -0.147621, -0.716567 , 210 | 0.850651, 0.000000, -0.525731 , 211 | 0.809017, -0.309017, -0.500000 , 212 | 0.864188, -0.442863, -0.238856 , 213 | 0.951056, -0.162460, -0.262866 , 214 | 0.147621, 0.716567, -0.681718 , 215 | 0.309017, 0.500000, -0.809017 , 216 | 0.425325, 0.688191, -0.587785 , 217 | 0.442863, 0.238856, -0.864188 , 218 | 0.587785, 0.425325, -0.688191 , 219 | 0.688191, 0.587785, -0.425325 , 220 | -0.147621, 0.716567, -0.681718 , 221 | -0.309017, 0.500000, -0.809017 , 222 | 0.000000, 0.525731, -0.850651 , 223 | -0.525731, 0.000000, -0.850651 , 224 | -0.442863, 0.238856, -0.864188 , 225 | -0.295242, 0.000000, -0.955423 , 226 | -0.162460, 0.262866, -0.951056 , 227 | 0.000000, 0.000000, -1.000000 , 228 | 0.295242, 0.000000, -0.955423 , 229 | 0.162460, 0.262866, -0.951056 , 230 | -0.442863, -0.238856, -0.864188 , 231 | -0.309017, -0.500000, -0.809017 , 232 | -0.162460, -0.262866, -0.951056 , 233 | 0.000000, -0.850651, -0.525731 , 234 | -0.147621, -0.716567, -0.681718 , 235 | 0.147621, -0.716567, -0.681718 , 236 | 0.000000, -0.525731, -0.850651 , 237 | 0.309017, -0.500000, -0.809017 , 238 | 0.442863, -0.238856, -0.864188 , 239 | 0.162460, -0.262866, -0.951056 , 240 | 0.238856, -0.864188, -0.442863 , 241 | 0.500000, -0.809017, -0.309017 , 242 | 0.425325, -0.688191, -0.587785 , 243 | 0.716567, -0.681718, -0.147621 , 244 | 0.688191, -0.587785, -0.425325 , 245 | 0.587785, -0.425325, -0.688191 , 246 | 0.000000, -0.955423, -0.295242 , 247 | 0.000000, -1.000000, 0.000000 , 248 | 0.262866, -0.951056, -0.162460 , 249 | 0.000000, -0.850651, 0.525731 , 250 | 0.000000, -0.955423, 0.295242 , 251 | 0.238856, -0.864188, 0.442863 , 252 | 0.262866, -0.951056, 0.162460 , 253 | 0.500000, -0.809017, 0.309017 , 254 | 0.716567, -0.681718, 0.147621 , 255 | 0.525731, -0.850651, 0.000000 , 256 | -0.238856, -0.864188, -0.442863 , 257 | -0.500000, -0.809017, -0.309017 , 258 | -0.262866, -0.951056, -0.162460 , 259 | -0.850651, -0.525731, 0.000000 , 260 | -0.716567, -0.681718, -0.147621 , 261 | -0.716567, -0.681718, 0.147621 , 262 | -0.525731, -0.850651, 0.000000 , 263 | -0.500000, -0.809017, 0.309017 , 264 | -0.238856, -0.864188, 0.442863 , 265 | -0.262866, -0.951056, 0.162460 , 266 | -0.864188, -0.442863, 0.238856 , 267 | -0.809017, -0.309017, 0.500000 , 268 | -0.688191, -0.587785, 0.425325 , 269 | -0.681718, -0.147621, 0.716567 , 270 | -0.442863, -0.238856, 0.864188 , 271 | -0.587785, -0.425325, 0.688191 , 272 | -0.309017, -0.500000, 0.809017 , 273 | -0.147621, -0.716567, 0.681718 , 274 | -0.425325, -0.688191, 0.587785 , 275 | -0.162460, -0.262866, 0.951056 , 276 | 0.442863, -0.238856, 0.864188 , 277 | 0.162460, -0.262866, 0.951056 , 278 | 0.309017, -0.500000, 0.809017 , 279 | 0.147621, -0.716567, 0.681718 , 280 | 0.000000, -0.525731, 0.850651 , 281 | 0.425325, -0.688191, 0.587785 , 282 | 0.587785, -0.425325, 0.688191 , 283 | 0.688191, -0.587785, 0.425325 , 284 | -0.955423, 0.295242, 0.000000 , 285 | -0.951056, 0.162460, 0.262866 , 286 | -1.000000, 0.000000, 0.000000 , 287 | -0.850651, 0.000000, 0.525731 , 288 | -0.955423, -0.295242, 0.000000 , 289 | -0.951056, -0.162460, 0.262866 , 290 | -0.864188, 0.442863, -0.238856 , 291 | -0.951056, 0.162460, -0.262866 , 292 | -0.809017, 0.309017, -0.500000 , 293 | -0.864188, -0.442863, -0.238856 , 294 | -0.951056, -0.162460, -0.262866 , 295 | -0.809017, -0.309017, -0.500000 , 296 | -0.681718, 0.147621, -0.716567 , 297 | -0.681718, -0.147621, -0.716567 , 298 | -0.850651, 0.000000, -0.525731 , 299 | -0.688191, 0.587785, -0.425325 , 300 | -0.587785, 0.425325, -0.688191 , 301 | -0.425325, 0.688191, -0.587785 , 302 | -0.425325, -0.688191, -0.587785 , 303 | -0.587785, -0.425325, -0.688191 , 304 | -0.688191, -0.587785, -0.425325 305 | ); 306 | return(matrix(normals_raw, ncol = 3L, byrow = TRUE)); 307 | } 308 | 309 | 310 | #quadf = '~/data/q2_pak/models/items/quaddama/tris.md2' 311 | #md2q = read.quake.md2(quadf); 312 | 313 | #' @title Check whether object is Quake 2 MD2 model 314 | #' 315 | #' @param x any R object 316 | #' 317 | #' @export 318 | is.quakemodel_md2 <- function(x) inherits(x, 'quakemodel_md2') 319 | 320 | -------------------------------------------------------------------------------- /R/read_wal.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' @title Read bitmap file in WAL format. 4 | #' 5 | #' @param filepath character string, path to the file including extension 6 | #' 7 | #' @param hdr logical, whether to return full list with header 8 | #' 9 | #' @param hdr_only logical, whether to read only the header 10 | #' 11 | #' @param apply_palette optional 256 x 3 integer matrix, the palette. Must contain values in range 0..255. Pass NULL if you do not want to apply any palette. The resulting \code{wal} object will not have an 'image' entry then. 12 | #' 13 | #' @return integer pixel matrix, each pixel value is in range 0-255 and refers to an index in a palette. The palette is NOT included in the file, so you will need to define one or get it from elsewhere to see the final image. 14 | #' 15 | #' @examples 16 | #' \dontrun{ 17 | #' walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 18 | #' wal = read.wal(walf); 19 | #' plot(wal); 20 | #' } 21 | #' 22 | #' @export 23 | read.wal <- function(filepath, hdr = TRUE, hdr_only = FALSE, apply_palette = wal::pal_q2()) { 24 | fh = file(filepath, "rb"); 25 | on.exit({ close(fh) }); 26 | 27 | endian = 'little'; 28 | num_mip_maps = 4L; 29 | read_mip_maps = TRUE; 30 | 31 | wal = list(); 32 | header = list(); 33 | 34 | header$tex_name = readChar(fh, 32L); 35 | header$width = readBin(fh, integer(), n = 1, size = 4, endian = endian); 36 | header$height = readBin(fh, integer(), n = 1, size = 4, endian = endian); 37 | header$mip_level_offsets = readBin(fh, integer(), n = num_mip_maps, size = 4, endian = endian); 38 | header$anim_name = readChar(fh, 32L); # Next frame name in animation, if any. Empty string if none, which is the most common case. 39 | header$flags = readBin(fh, integer(), n = 1, size = 4, endian = endian); 40 | header$contents = readBin(fh, integer(), n = 1, size = 4, endian = endian); 41 | header$value = readBin(fh, integer(), n = 1, size = 4, endian = endian); 42 | 43 | if(hdr_only) { 44 | return(header); 45 | } 46 | 47 | if(header$width < 1L | header$height < 1L) { 48 | warning("File not in WAL format (or invalid zero-length image dimension)."); 49 | } 50 | 51 | # Read data for all mipmaps. 52 | mip_level0_data_size = header$width * header$height; 53 | 54 | # sanity check: does the data length of the first mipmap match width x height. 55 | if(header$mip_level_offsets[2L] - header$mip_level_offsets[1L] != mip_level0_data_size) { 56 | warning(sprintf("Expected %d pixel values in image based on width %d and height %d, but first mipmap size is %d.\n", mip_level0_data_size, header$width * header$height, (header$mip_level_offsets[2L] - header$mip_level_offsets[1L]))); 57 | } 58 | 59 | header$mipmaps = list(); 60 | header$mipmaps$mip_level0_data_size = mip_level0_data_size; 61 | header$mipmaps$mip_level1_data_size = header$mip_level_offsets[3L] - header$mip_level_offsets[2L]; 62 | header$mipmaps$mip_level2_data_size = header$mip_level_offsets[4L] - header$mip_level_offsets[3L]; 63 | seek(fh, where = 0L, origin = "end"); end_pos = seek(fh, where = NA); # Find file size (last position). 64 | header$mipmaps$mip_level3_data_size = end_pos - header$mip_level_offsets[4L]; 65 | 66 | 67 | seek(fh, where = header$mip_level_offsets[1L], origin = "start"); 68 | raw_data = readBin(fh, integer(), n = mip_level0_data_size, size = 1, signed = FALSE, endian = endian); # vector 69 | raw_data = raw_data + 1L; # R uses 1-based indices. Note that raw_data is for first mipmap only. 70 | 71 | 72 | if(length(raw_data) != mip_level0_data_size) { 73 | warning(sprintf("Expected %d pixel values, but %d read.\n", mip_level0_data_size, length(raw_data))); 74 | } 75 | 76 | pixel_cmap_indices = matrix(data = raw_data, nrow = header$height, ncol = header$width, byrow = TRUE); # reshaped to image matrix 77 | 78 | wal = list('header' = header, 'raw_data' = raw_data, 'pixel_cmap_indices' = pixel_cmap_indices); 79 | class(wal) = c(class(wal), 'wal'); 80 | 81 | if(! is.null(apply_palette)) { 82 | check.palette(apply_palette); 83 | } 84 | 85 | wal$header$mipmaps$mip_level0_dim = c(wal$header$width, wal$header$height); 86 | 87 | if(read_mip_maps) { 88 | seek(fh, where = header$mip_level_offsets[2L], origin = "start"); 89 | wal$raw_data_mip_level1 = readBin(fh, integer(), n = header$mipmaps$mip_level1_data_size, size = 1, signed = FALSE, endian = endian) + 1L; 90 | wal$image_mip_level1 = apply.palette.to.rawdata(wal$raw_data_mip_level1, apply_palette, header$width / 2L, header$height / 2L); 91 | wal$header$mipmaps$mip_level1_dim = c(wal$header$width / 2L, wal$header$height / 2L); 92 | 93 | seek(fh, where = header$mip_level_offsets[3L], origin = "start"); 94 | wal$raw_data_mip_level2 = readBin(fh, integer(), n = header$mipmaps$mip_level2_data_size, size = 1, signed = FALSE, endian = endian) + 1L; 95 | wal$image_mip_level2 = apply.palette.to.rawdata(wal$raw_data_mip_level2, apply_palette, header$width / 4L, header$height / 4L); 96 | wal$header$mipmaps$mip_level2_dim = c(wal$header$width / 4L, wal$header$height / 4L); 97 | 98 | seek(fh, where = header$mip_level_offsets[4L], origin = "start"); 99 | wal$raw_data_mip_level3 = readBin(fh, integer(), n = header$mipmaps$mip_level3_data_size, size = 1, signed = FALSE, endian = endian) + 1L; 100 | wal$image_mip_level3 = apply.palette.to.rawdata(wal$raw_data_mip_level3, apply_palette, header$width / 8L, header$height / 8L); 101 | wal$header$mipmaps$mip_level3_dim = c(wal$header$width / 8L, wal$header$height / 8L); 102 | } 103 | 104 | wal$image = apply.palette.to.rawdata(raw_data, apply_palette, header$width, header$height); 105 | wal$file_data_all_mipmaps = c(wal$raw_data, wal$raw_data_mip_level1, wal$raw_data_mip_level2, wal$raw_data_mip_level3); 106 | 107 | if(hdr) { 108 | return(wal); 109 | } else { 110 | if(is.null(apply_palette)) { 111 | stop("Cannot return image without palette. Set hdr to TRUE or pass non-NULL apply_palette parameter."); 112 | } 113 | return(wal$image); 114 | } 115 | } 116 | 117 | 118 | #' @title Read bitmap image in WAL format, returning image data only. 119 | #' 120 | #' @description Read a bitmap image in WAL format, and return data in the same format as \code{png::readPNG} and \code{jpeg::readJPEG} do. 121 | #' 122 | #' @inheritParams read.wal 123 | #' 124 | #' @seealso \code{read.wal} if you want to read the header and have more control. 125 | #' 126 | #' @return numeric matrix with dimension width x height x channels, with all color values in range 0..1. 127 | #' 128 | #' @examples 129 | #' \dontrun{ 130 | #' walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 131 | #' wal_image = readWAL(walf); 132 | #' dim(wal_image); 133 | #' } 134 | #' 135 | #' @export 136 | readWAL <- function(filepath, apply_palette = wal::pal_q2()) { 137 | return(read.wal(filepath, hdr = FALSE, hdr_only = FALSE, apply_palette = apply_palette) / 255.); 138 | } 139 | 140 | 141 | #' @title Apply a palette to index data to create a 2D image. 142 | #' 143 | #' @param raw_data integer vector of pixel data, each entry represents an index into the palette. 144 | #' 145 | #' @param apply_palette integer matrix, the palette. 146 | #' 147 | #' @param img_width integer, the width of the image to create. 148 | #' 149 | #' @param img_height integer, the height of the image to create. 150 | #' 151 | #' @keywords internal 152 | apply.palette.to.rawdata <- function(raw_data, apply_palette, img_width, img_height) { 153 | if(! is.null(apply_palette)) { 154 | channel_red = apply_palette[raw_data, 1]; 155 | channel_green = apply_palette[raw_data, 2]; 156 | channel_blue = apply_palette[raw_data, 3]; 157 | return(array( c( channel_red , channel_green, channel_blue ) , dim = c( img_width , img_height , 3))); 158 | } else { 159 | return(NULL); 160 | } 161 | } 162 | 163 | 164 | #' @title Check palette, stop on invalid data. 165 | #' 166 | #' @param pal a palette, i.e., a 256 x 3 integer matrix, with values in range 0..255L. 167 | #' 168 | #' @keywords internal 169 | check.palette <- function(pal) { 170 | if(! is.matrix(pal)) { 171 | stop("Palette must be a matrix."); 172 | } 173 | if( ! all.equal(dim(pal), c(256, 3))) { 174 | stop("Palette must be a 256 x 3 matrix."); 175 | } 176 | if(min(pal) < 0L | max(pal) > 255L) { 177 | stop("All palette values must be in range 0..255.") 178 | } 179 | if(max(pal) <= 1L) { 180 | warning(sprintf("Palette max value is %d, but the values must be in range 0..255.\n", max(pal))); 181 | } 182 | return(invisible(NULL)); 183 | } 184 | 185 | 186 | 187 | #' @title S3 plot function for wal image. 188 | #' 189 | #' @param x a wal instance. 190 | #' 191 | #' @param ... extra args, not used. 192 | #' 193 | #' @export 194 | #' @importFrom graphics plot 195 | plot.wal <- function(x, ...) { 196 | if(requireNamespace('imager', quietly = TRUE)) { 197 | if(! is.null(x$image)) { 198 | graphics::plot(imager::as.cimg(array(x$image, dim=c(x$header$width, x$header$height, 1, 3)))); 199 | } else { 200 | warning("The wal instance contains no final image, using grayscale preview palette. Use 'plotwal.mipmap()' to set palette for viewing."); 201 | apply_palette = cbind(0L:255L, 0L:255L, 0L:255L); 202 | check.palette(apply_palette); 203 | img = apply.palette.to.rawdata(x$raw_data, apply_palette, x$header$width , x$header$height); 204 | graphics::plot(imager::as.cimg(array(img, dim=c(x$header$width, x$header$height, 1, 3)))); 205 | } 206 | } else { 207 | stop("The 'imager' package must be installed to plot PCX images."); 208 | } 209 | } 210 | 211 | 212 | #' @title Plot a mipmap level from a WAL image. 213 | #' 214 | #' @param wal a WAL image instance, as returned by \code{read.wal}. 215 | #' 216 | #' @param mip_level integer in range 0..3, the mipmap to plot. Level 0 is the original full-size image, the other ones get smaller and smaller (by factor 2 on each dimension, so 1/4th the size of their predecessor). 217 | #' 218 | #' @inheritParams read.wal 219 | #' 220 | #' @examples 221 | #' \dontrun{ 222 | #' walf = '~/data/q2_pak0_extracted/textures/e1u2/basic1_7.wal'; 223 | #' wal = read.wal(walf); 224 | #' plotwal.mipmap(wal, mip_level = 3); 225 | #' } 226 | #' 227 | #' @export 228 | plotwal.mipmap <- function(wal, mip_level = 0L, apply_palette = wal::pal_q2()) { 229 | if(mip_level < 0L | mip_level > 3L) { 230 | stop("Paramter 'mip_level' must be an integer in range 0..3."); 231 | } 232 | 233 | raw_data = get.wal.mipmap.data(wal, mip_level); 234 | img_width = get.wal.mipmap.widths(wal$header$width)[mip_level+1L]; 235 | img_height = get.wal.mipmap.heights(wal$header$height)[mip_level+1L]; 236 | 237 | if(is.null(apply_palette)) { 238 | warning("The wal instance contains no final image and none supplied in parameter 'apply_palette'. Using grayscale preview palette."); 239 | apply_palette = cbind(0L:255L, 0L:255L, 0L:255L); 240 | } 241 | check.palette(apply_palette); 242 | img = apply.palette.to.rawdata(raw_data, apply_palette, img_width , img_height); 243 | graphics::plot(imager::as.cimg(array(img, dim=c(img_width, img_height, 1, 3)))); 244 | } 245 | 246 | 247 | #' @title Plot raw pixel index data as image. 248 | #' 249 | #' @param raw_data integer vector in containing width * height values in range 0..255, and optionally additional mipmap data at the end (which will be ignored). The raw image data. Can be a Q2 WAL data, Q1 miptex data, or anything else. 250 | #' 251 | #' @param width positive integer, the image width. 252 | #' 253 | #' @param height positive integer, the image height. 254 | #' 255 | #' @examples 256 | #' \dontrun{ 257 | #' # Plot the Q1 shambler skin: 258 | #' mdl = read.quake.mdl("~/data/q1_pak/progs/shambler.mdl"); 259 | #' plotwal.rawdata(mdl$skins$skin_pic, mdl$header$skin_width, 260 | #' mdl$header$skin_height, apply_palette = pal_q1()); 261 | #' } 262 | #' 263 | #' @inheritParams read.wal 264 | #' @export 265 | plotwal.rawdata <- function(raw_data, width, height, apply_palette = wal::pal_q2()) { 266 | img_size = width * height; 267 | if(length(raw_data) > img_size) { 268 | raw_data = raw_data[1L:img_size]; # cut off mipmap data, if any. 269 | } 270 | 271 | if(length(raw_data) == img_size) { 272 | if(is.null(apply_palette)) { 273 | warning("The wal instance contains no final image and none supplied in parameter 'apply_palette'. Using grayscale preview palette."); 274 | apply_palette = cbind(0L:255L, 0L:255L, 0L:255L); 275 | } 276 | check.palette(apply_palette); 277 | img = apply.palette.to.rawdata(raw_data, apply_palette, width , height); 278 | graphics::plot(imager::as.cimg(array(img, dim=c(width, height, 1, 3)))); 279 | } else { 280 | stop(sprintf("Image raw_data too small (%d) for given width %d and height %d, expected at least %d.\n", length(raw_data), width, height, img_size)); 281 | } 282 | } 283 | 284 | 285 | #' @title Retrieve raw data for given mipmap level from WAL instance. 286 | #' 287 | #' @inheritParams plotwal.mipmap 288 | #' 289 | #' @keywords internal 290 | get.wal.mipmap.data <- function(wal, mip_level) { 291 | mm_offset = get.mipmap.data.offsets(wal$header$width, wal$header$height, start_at = 0L); 292 | mm_len = get.mipmap.data.lengths(wal$header$width, wal$header$height); 293 | mm0 = wal$file_data_all_mipmaps[mm_offset[1]:mm_offset[2]]; 294 | mm1 = wal$file_data_all_mipmaps[mm_offset[2]:mm_offset[3]]; 295 | mm2 = wal$file_data_all_mipmaps[mm_offset[3]:mm_offset[4]] 296 | mm3 = wal$file_data_all_mipmaps[mm_offset[4]:(mm_offset[4]+mm_len[4])]; 297 | if(mip_level == 0L) { 298 | return(mm0); 299 | } else if(mip_level == 1L) { 300 | return(mm1); 301 | } else if(mip_level == 2L) { 302 | return(mm2); 303 | } else if(mip_level == 3L) { 304 | return(mm3); 305 | } else { 306 | stop("Invalid mip_level, must be 0..3."); 307 | } 308 | } 309 | 310 | 311 | #' @title Compute widths of the 4 mipimap levels from base width. 312 | #' 313 | #' @param width_mm integer, the base mipmap width. 314 | #' 315 | #' @return integer vector of length 4, the mipmap widths. 316 | #' @keywords internal 317 | get.wal.mipmap.widths <- function(width_mm) { 318 | return(c(width_mm, width_mm/2, width_mm/4, width_mm/8)); 319 | } 320 | 321 | 322 | #' @title Compute widths of the 4 mipimap levels from base width. 323 | #' 324 | #' @param height_mm integer, the base mipmap height. 325 | #' 326 | #' @return integer vector of length 4, the mipmap heights. 327 | #' 328 | #' @keywords internal 329 | get.wal.mipmap.heights <- function(height_mm) { 330 | return(get.wal.mipmap.widths(height_mm)); 331 | } 332 | 333 | -------------------------------------------------------------------------------- /R/read_model_mdl.R: -------------------------------------------------------------------------------- 1 | # Functions for reading Quake alias models (aka procs). 2 | # This includes entity models (weapons, pickups) and enemy models. The file extension is '.mdl'. 3 | 4 | #' @title Read Quake model in MDL format. 5 | #' 6 | #' @param filepath character string, the path to the MDL file 7 | #' 8 | #' @param do_checks logical, whether to perform some sanity checks on the data and warn on suspicious results. 9 | #' 10 | #' @note Ignore this function, it will be moved to a different package. 11 | #' 12 | #' @examples 13 | #' \dontrun{ 14 | #' mdlf = "~/data/q1_pak/progs/quaddama.mdl" 15 | #' mdl = read.quake.mdl(mdlf); 16 | #' } 17 | #' 18 | #' @export 19 | read.quake.mdl <- function(filepath, do_checks = FALSE) { 20 | fh = file(filepath, "rb"); 21 | on.exit({ close(fh) }); 22 | 23 | endian = 'little'; 24 | 25 | mdl = list('header' = list()); 26 | 27 | int_size = 4L; 28 | 29 | mdl$header$id = readBin(fh, integer(), n = 1, size = int_size, endian = endian); 30 | mdl$header$version = readBin(fh, integer(), n = 1, size = int_size, endian = endian); 31 | 32 | if(mdl$header$id != 1330660425L | mdl$header$version != 6L) { 33 | stop(sprintf("File '%s' not in MDL format.\n", filepath)); 34 | } 35 | 36 | mdl$header$scale = readBin(fh, numeric(), n = 3, size = 4, endian = endian); 37 | mdl$header$origin = readBin(fh, numeric(), n = 3, size = 4, endian = endian); 38 | mdl$header$radius = readBin(fh, numeric(), n = 1, size = 4, endian = endian); # bbox radius 39 | mdl$header$offsets = readBin(fh, numeric(), n = 3, size = 4, endian = endian); # eye pos 40 | mdl$header$num_skins = readBin(fh, integer(), n = 1, size = int_size, endian = endian); # number of skin textures 41 | mdl$header$skin_width = readBin(fh, integer(), n = 1, size = int_size, endian = endian); 42 | mdl$header$skin_height = readBin(fh, integer(), n = 1, size = int_size, endian = endian); 43 | mdl$header$num_verts = readBin(fh, integer(), n = 1, size = int_size, endian = endian); 44 | mdl$header$num_tris = readBin(fh, integer(), n = 1, size = int_size, endian = endian); 45 | mdl$header$num_frames = readBin(fh, integer(), n = 1, size = int_size, endian = endian); 46 | mdl$header$sync_type = readBin(fh, integer(), n = 1, size = int_size, endian = endian); # 0=synchron, 1=random 47 | mdl$header$flags = readBin(fh, integer(), n = 1, size = int_size, endian = endian); # 0 48 | mdl$header$size = readBin(fh, numeric(), n = 1, size = 4, endian = endian); # average tris size 49 | 50 | if(do_checks) { 51 | # some sanity checks 52 | if((mdl$header$skin_width %% 4) != 0L) { 53 | warning(sprintf("Invalid skin texture width %d, must be multiple of 4.\n", mdl$header$skin_width)); 54 | } 55 | if((mdl$header$skin_height %% 4) != 0L) { 56 | warning(sprintf("Invalid skin texture height %d, must be multiple of 4.\n", mdl$header$skin_height)); 57 | } 58 | if(! mdl$header$sync_type %in% c(0L, 1L)) { 59 | warning("Invalid sync type, must be 0 or 1."); 60 | } 61 | if(mdl$header$flags != 0L) { 62 | warning(sprintf("Invalid flags %d, must be 0.\n", mdl$header$flags)); 63 | } 64 | } 65 | 66 | # next follow model skins. Could be one or a group. 67 | mdl$skins = list(); 68 | mdl$skins$skin_type = readBin(fh, integer(), n = 1, size = 4, endian = endian); 69 | if(mdl$skins$skin_type == 0L) { # single picture 70 | mdl$skins$skin_pic = readBin(fh, integer(), n = (mdl$header$skin_width * mdl$header$skin_height) , size = 1, signed = FALSE, endian = endian); 71 | } else { 72 | mdl$skins$num_skins_in_group = readBin(fh, integer(), n = 1, size = 4, endian = endian); 73 | mdl$skins$time_per_skin = readBin(fh, numeric(), n = mdl$skins$num_skins_in_group, size = 4, endian = endian); 74 | mdl$skins$skin_pics = list(); 75 | if(mdl$skins$num_skins_in_group > 0L) { 76 | for(skin_idx in 1:mdl$skins$num_skins_in_group) { 77 | mdl$skins$skin_pics[[skin_idx]] = readBin(fh, integer(), n = (mdl$header$skin_width * mdl$header$skin_height) , size = 1, signed = FALSE, endian = endian); 78 | } 79 | } 80 | } 81 | 82 | # skin texture coords 83 | if(mdl$header$num_verts > 0L) { 84 | mdl$skins$skinverts = matrix(rep(NA, (mdl$header$num_verts * 3L)), ncol = 3L); 85 | for(skin_vert_idx in 1:mdl$header$num_verts) { 86 | # The 3 values per vertex are: onseam (whether vertex is on seam between model front and back), s (horizontal texture coord in range [0, skinwidth[), t (vertical texture coord in range [0, skinheight[). 87 | # The first value (onseam) must be 0 or 32L. 88 | mdl$skins$skinverts[skin_vert_idx,] = readBin(fh, integer(), n = 3L, size = 4, endian = endian); 89 | } 90 | } 91 | 92 | # triangles (as indices into vertex list) 93 | mdl$triangles = list(); 94 | if(mdl$header$num_tris > 0L) { 95 | mdl$triangles$raw = matrix(rep(NA, (mdl$header$num_tris * 4L)), ncol = 4L); 96 | # the 4 values are: flag face_is_front (0=FALSE, 1s=TRUE), and the 3 vertex indices of the triangle. 97 | for(triangle_idx in 1:mdl$header$num_tris) { 98 | mdl$triangles$raw[triangle_idx,] = readBin(fh, integer(), n = 4L, size = 4, endian = endian); 99 | } 100 | mdl$triangles$triangle_is_front = mdl$triangles$raw[, 1L]; 101 | mdl$triangles$vertex = mdl$triangles$raw[, 2:4]; 102 | mdl$triangles$raw = NULL; 103 | } 104 | 105 | if(any(!(mdl$triangles$triangle_is_front %in% c(0L, 1L)))) { 106 | warning("Found triangles with invalid 'triangle_is_front' value (expected 0 or 1)."); 107 | } 108 | if(max(mdl$triangles$vertex) >= mdl$header$num_verts){ 109 | warning(sprintf("Found triangle referencing 0-based vertex index %d, but there are only %d vertices.\n", max(mdl$triangles$vertex) >= mdl$header$num_verts)); 110 | } 111 | 112 | # next follow model frames. Each frame contains vertex positions (a model in a certain orientation). 113 | mdl$frames = list(); 114 | if(mdl$header$num_frames > 0L) { 115 | for(frame_idx in 1:mdl$header$num_frames) { 116 | this_frame = list(); 117 | this_frame$frame_type = readBin(fh, integer(), n = 1, size = 4, endian = endian); # 0 = simple frame, everything else = full frame. 118 | if(this_frame$frame_type == 0L) { # single simple frame 119 | # min vertex position 120 | this_frame$min_vertex = readBin(fh, integer(), n = 4, size = 1, signed = FALSE, endian = endian); 121 | # same for max vertex position. 122 | this_frame$max_vertex = readBin(fh, integer(), n = 4, size = 1, signed = FALSE, endian = endian); 123 | this_frame$name = readChar(fh, 16L, useBytes = TRUE); # frame name. 124 | 125 | # the 4 values are: 1-3=packed position 255 (x,y,z), 4=index into normal list. 126 | this_frame$vertex_coords_raw = matrix(readBin(fh, integer(), n = (mdl$header$num_verts * 4L), size = 1, signed = FALSE, endian = endian), ncol = 4L, byrow = TRUE); 127 | this_frame$vertex_coords = unpack.vertex.coords(this_frame$vertex_coords_raw[,1:3], mdl$header); 128 | this_frame$vertex_normals = lookup.q1.normals(this_frame$vertex_coords_raw[,4]); 129 | #this_frame$vertex_coords_raw = NULL; 130 | } else { # full frame: group of simple frames and extra data. 131 | # min vertex position over all following frames. The 4 values are: 1..3=packed position in range 0..255. 4=normal index (into list of pre-defined normals, approximate value for Gouroud Shading). 132 | this_frame$min_vertex = readBin(fh, integer(), n = 4, size = 1, signed = FALSE, endian = endian); 133 | # same for max vertex position. 134 | this_frame$max_vertex = readBin(fh, integer(), n = 4, size = 1, signed = FALSE, endian = endian); 135 | this_frame$num_simple_frames = this_frame$frame_type; # TODO: where to get this? the current value this_frame$frame_type is a guess. 136 | this_frame$frame_timings = readBin(fh, numeric(), n = this_frame$num_simple_frames, size = 4, endian = endian); 137 | this_frame$simple_frames = list(); 138 | for(simple_frame_idx in 1:this_frame$num_simple_frames) { 139 | this_simple_frame = list(); 140 | this_simple_frame$min_vertex = readBin(fh, integer(), n = 4, size = 1, signed = FALSE, endian = endian); 141 | # same for max vertex position. 142 | this_simple_frame$max_vertex = readBin(fh, integer(), n = 4, size = 1, signed = FALSE, endian = endian); 143 | this_simple_frame$name = readChar(fh, 16L, useBytes = TRUE); # frame name. 144 | this_simple_frame$vertex_coords_raw = matrix(readBin(fh, integer(), n = (mdl$header$num_verts * 4L), size = 1, signed = FALSE, endian = endian), ncol = 4L, byrow = TRUE); 145 | this_simple_frame$vertex_coords = unpack.vertex.coords(this_simple_frame$vertex_coords_raw[,1:3], mdl$header); 146 | this_simple_frame$vertex_normals = lookup.q1.normals(this_simple_frame$vertex_coords_raw[,4]); 147 | this_simple_frame$vertex_coords_raw = NULL; 148 | this_frame$simple_frames[[simple_frame_idx]] = this_simple_frame; 149 | } 150 | } 151 | mdl$frames[[frame_idx]] = this_frame; 152 | } 153 | } 154 | class(mdl) = c(class(mdl), 'quakemodel_mdl'); 155 | return(mdl); 156 | } 157 | 158 | 159 | #' @title Unpack vertex coords from Q1 0-255 representation. 160 | #' 161 | #' @param coords_packed matrix of n x 3 integers in range 0..255, the packed coords from an MDL file. 162 | #' 163 | #' @param mdl_header MDL header or named list, only the fields 'header$scale' and 'header$origin' are used. 164 | #' 165 | #' @keywords internal 166 | unpack.vertex.coords <- function(coords_packed, mdl_header) { 167 | if(ncol(coords_packed) != 3L) { 168 | stop("Parameter 'coords_packed' must be a matrix with 3 columns."); 169 | } 170 | if(is.null(mdl_header$origin) | is.null(mdl_header$scale)) { 171 | stop("Parameter 'mdl_header' must have 'origin' and 'scale' entries.") 172 | } 173 | 174 | nc = ncol(coords_packed); 175 | coords_unpacked = matrix(rep(NA, (nc * nrow(coords_packed))), ncol = nc); 176 | for(row_idx in 1:nrow(coords_packed)) { 177 | coords_unpacked[row_idx,] = (coords_packed[row_idx,] * mdl_header$scale) + mdl_header$origin; 178 | } 179 | return(coords_unpacked); 180 | } 181 | 182 | 183 | #' @title Return list of pre-defined Quake I normals. 184 | #' 185 | #' @return n x 3 matrix of doubles, the normals. Hardcoded. 186 | #' 187 | #' @keywords internal 188 | predefined.mdl.normals <- function() { 189 | q1_norms = c( -0.525731, 0.000000, 0.850651 , 190 | -0.442863, 0.238856, 0.864188 , 191 | -0.295242, 0.000000, 0.955423 , 192 | -0.309017, 0.500000, 0.809017 , 193 | -0.162460, 0.262866, 0.951056 , 194 | 0.000000, 0.000000, 1.000000 , 195 | 0.000000, 0.850651, 0.525731 , 196 | -0.147621, 0.716567, 0.681718 , 197 | 0.147621, 0.716567, 0.681718 , 198 | 0.000000, 0.525731, 0.850651 , 199 | 0.309017, 0.500000, 0.809017 , 200 | 0.525731, 0.000000, 0.850651 , 201 | 0.295242, 0.000000, 0.955423 , 202 | 0.442863, 0.238856, 0.864188 , 203 | 0.162460, 0.262866, 0.951056 , 204 | -0.681718, 0.147621, 0.716567 , 205 | -0.809017, 0.309017, 0.500000 , 206 | -0.587785, 0.425325, 0.688191 , 207 | -0.850651, 0.525731, 0.000000 , 208 | -0.864188, 0.442863, 0.238856 , 209 | -0.716567, 0.681718, 0.147621 , 210 | -0.688191, 0.587785, 0.425325 , 211 | -0.500000, 0.809017, 0.309017 , 212 | -0.238856, 0.864188, 0.442863 , 213 | -0.425325, 0.688191, 0.587785 , 214 | -0.716567, 0.681718, -0.147621 , 215 | -0.500000, 0.809017, -0.309017 , 216 | -0.525731, 0.850651, 0.000000 , 217 | 0.000000, 0.850651, -0.525731 , 218 | -0.238856, 0.864188, -0.442863 , 219 | 0.000000, 0.955423, -0.295242 , 220 | -0.262866, 0.951056, -0.162460 , 221 | 0.000000, 1.000000, 0.000000 , 222 | 0.000000, 0.955423, 0.295242 , 223 | -0.262866, 0.951056, 0.162460 , 224 | 0.238856, 0.864188, 0.442863 , 225 | 0.262866, 0.951056, 0.162460 , 226 | 0.500000, 0.809017, 0.309017 , 227 | 0.238856, 0.864188, -0.442863 , 228 | 0.262866, 0.951056, -0.162460 , 229 | 0.500000, 0.809017, -0.309017 , 230 | 0.850651, 0.525731, 0.000000 , 231 | 0.716567, 0.681718, 0.147621 , 232 | 0.716567, 0.681718, -0.147621 , 233 | 0.525731, 0.850651, 0.000000 , 234 | 0.425325, 0.688191, 0.587785 , 235 | 0.864188, 0.442863, 0.238856 , 236 | 0.688191, 0.587785, 0.425325 , 237 | 0.809017, 0.309017, 0.500000 , 238 | 0.681718, 0.147621, 0.716567 , 239 | 0.587785, 0.425325, 0.688191 , 240 | 0.955423, 0.295242, 0.000000 , 241 | 1.000000, 0.000000, 0.000000 , 242 | 0.951056, 0.162460, 0.262866 , 243 | 0.850651, -0.525731, 0.000000 , 244 | 0.955423, -0.295242, 0.000000 , 245 | 0.864188, -0.442863, 0.238856 , 246 | 0.951056, -0.162460, 0.262866 , 247 | 0.809017, -0.309017, 0.500000 , 248 | 0.681718, -0.147621, 0.716567 , 249 | 0.850651, 0.000000, 0.525731 , 250 | 0.864188, 0.442863, -0.238856 , 251 | 0.809017, 0.309017, -0.500000 , 252 | 0.951056, 0.162460, -0.262866 , 253 | 0.525731, 0.000000, -0.850651 , 254 | 0.681718, 0.147621, -0.716567 , 255 | 0.681718, -0.147621, -0.716567 , 256 | 0.850651, 0.000000, -0.525731 , 257 | 0.809017, -0.309017, -0.500000 , 258 | 0.864188, -0.442863, -0.238856 , 259 | 0.951056, -0.162460, -0.262866 , 260 | 0.147621, 0.716567, -0.681718 , 261 | 0.309017, 0.500000, -0.809017 , 262 | 0.425325, 0.688191, -0.587785 , 263 | 0.442863, 0.238856, -0.864188 , 264 | 0.587785, 0.425325, -0.688191 , 265 | 0.688191, 0.587785, -0.425325 , 266 | -0.147621, 0.716567, -0.681718 , 267 | -0.309017, 0.500000, -0.809017 , 268 | 0.000000, 0.525731, -0.850651 , 269 | -0.525731, 0.000000, -0.850651 , 270 | -0.442863, 0.238856, -0.864188 , 271 | -0.295242, 0.000000, -0.955423 , 272 | -0.162460, 0.262866, -0.951056 , 273 | 0.000000, 0.000000, -1.000000 , 274 | 0.295242, 0.000000, -0.955423 , 275 | 0.162460, 0.262866, -0.951056 , 276 | -0.442863, -0.238856, -0.864188 , 277 | -0.309017, -0.500000, -0.809017 , 278 | -0.162460, -0.262866, -0.951056 , 279 | 0.000000, -0.850651, -0.525731 , 280 | -0.147621, -0.716567, -0.681718 , 281 | 0.147621, -0.716567, -0.681718 , 282 | 0.000000, -0.525731, -0.850651 , 283 | 0.309017, -0.500000, -0.809017 , 284 | 0.442863, -0.238856, -0.864188 , 285 | 0.162460, -0.262866, -0.951056 , 286 | 0.238856, -0.864188, -0.442863 , 287 | 0.500000, -0.809017, -0.309017 , 288 | 0.425325, -0.688191, -0.587785 , 289 | 0.716567, -0.681718, -0.147621 , 290 | 0.688191, -0.587785, -0.425325 , 291 | 0.587785, -0.425325, -0.688191 , 292 | 0.000000, -0.955423, -0.295242 , 293 | 0.000000, -1.000000, 0.000000 , 294 | 0.262866, -0.951056, -0.162460 , 295 | 0.000000, -0.850651, 0.525731 , 296 | 0.000000, -0.955423, 0.295242 , 297 | 0.238856, -0.864188, 0.442863 , 298 | 0.262866, -0.951056, 0.162460 , 299 | 0.500000, -0.809017, 0.309017 , 300 | 0.716567, -0.681718, 0.147621 , 301 | 0.525731, -0.850651, 0.000000 , 302 | -0.238856, -0.864188, -0.442863 , 303 | -0.500000, -0.809017, -0.309017 , 304 | -0.262866, -0.951056, -0.162460 , 305 | -0.850651, -0.525731, 0.000000 , 306 | -0.716567, -0.681718, -0.147621 , 307 | -0.716567, -0.681718, 0.147621 , 308 | -0.525731, -0.850651, 0.000000 , 309 | -0.500000, -0.809017, 0.309017 , 310 | -0.238856, -0.864188, 0.442863 , 311 | -0.262866, -0.951056, 0.162460 , 312 | -0.864188, -0.442863, 0.238856 , 313 | -0.809017, -0.309017, 0.500000 , 314 | -0.688191, -0.587785, 0.425325 , 315 | -0.681718, -0.147621, 0.716567 , 316 | -0.442863, -0.238856, 0.864188 , 317 | -0.587785, -0.425325, 0.688191 , 318 | -0.309017, -0.500000, 0.809017 , 319 | -0.147621, -0.716567, 0.681718 , 320 | -0.425325, -0.688191, 0.587785 , 321 | -0.162460, -0.262866, 0.951056 , 322 | 0.442863, -0.238856, 0.864188 , 323 | 0.162460, -0.262866, 0.951056 , 324 | 0.309017, -0.500000, 0.809017 , 325 | 0.147621, -0.716567, 0.681718 , 326 | 0.000000, -0.525731, 0.850651 , 327 | 0.425325, -0.688191, 0.587785 , 328 | 0.587785, -0.425325, 0.688191 , 329 | 0.688191, -0.587785, 0.425325 , 330 | -0.955423, 0.295242, 0.000000 , 331 | -0.951056, 0.162460, 0.262866 , 332 | -1.000000, 0.000000, 0.000000 , 333 | -0.850651, 0.000000, 0.525731 , 334 | -0.955423, -0.295242, 0.000000 , 335 | -0.951056, -0.162460, 0.262866 , 336 | -0.864188, 0.442863, -0.238856 , 337 | -0.951056, 0.162460, -0.262866 , 338 | -0.809017, 0.309017, -0.500000 , 339 | -0.864188, -0.442863, -0.238856 , 340 | -0.951056, -0.162460, -0.262866 , 341 | -0.809017, -0.309017, -0.500000 , 342 | -0.681718, 0.147621, -0.716567 , 343 | -0.681718, -0.147621, -0.716567 , 344 | -0.850651, 0.000000, -0.525731 , 345 | -0.688191, 0.587785, -0.425325 , 346 | -0.587785, 0.425325, -0.688191 , 347 | -0.425325, 0.688191, -0.587785 , 348 | -0.425325, -0.688191, -0.587785 , 349 | -0.587785, -0.425325, -0.688191 , 350 | -0.688191, -0.587785, -0.425325 ); 351 | return(matrix(q1_norms, ncol = 3L, byrow = TRUE)); 352 | } 353 | 354 | #' @title Lookup Quake I normals by index. 355 | #' 356 | #' @param normal_indices integer vector of length n, the normal indices (0-based). 357 | #' 358 | #' @return n x 3 matrix of doubles, the normals 359 | #' 360 | #' @keywords internal 361 | lookup.q1.normals <- function(normal_indices) { 362 | if( ! is.vector(normal_indices)) { 363 | stop("Parameter 'normal_indices' must be an integer vector."); 364 | } 365 | return(predefined.mdl.normals()[(normal_indices + 1L)]); 366 | } 367 | 368 | 369 | #' @title Check whether object is Quake 1 MDL model 370 | #' 371 | #' @param x any R object 372 | #' 373 | #' @export 374 | is.quakemodel_mdl <- function(x) inherits(x, 'quakemodel_mdl') 375 | 376 | -------------------------------------------------------------------------------- /LICENSE_FULL: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------